Python linecache.getlines() Examples
The following are 30
code examples of linecache.getlines().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
linecache
, or try the search function
.
Example #1
Source File: test_inspect.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec compile(source, fn, 'single') in ns inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
Example #2
Source File: debugger.py From pycopia with Apache License 2.0 | 6 votes |
def search(self, argv): """search <pattern> Search the source file for the regular expression pattern.""" patt = re.compile(" ".join(argv[1:])) filename = self._dbg.curframe.f_code.co_filename if self._dbg.lineno is None: start = 0 else: start = max(0, self._dbg.lineno - 9) lines = linecache.getlines(filename)[start:] for lineno, line in enumerate(lines): #line = linecache.getline(filename, lineno) mo = patt.search(line) if mo: self._print_source(filename, lineno+start-10, lineno+start+10) return else: self._print("Pattern not found.")
Example #3
Source File: views.py From pycopia with Apache License 2.0 | 6 votes |
def search(self, argv): """search <pattern> Search the source file for the regular expression pattern.""" patt = re.compile(" ".join(argv[1:])) filename = self._dbg.curframe.f_code.co_filename if self._dbg.lineno is None: start = 0 else: start = max(0, self._dbg.lineno - 9) lines = linecache.getlines(filename)[start:] for lineno, line in enumerate(lines): #line = linecache.getline(filename, lineno) mo = patt.search(line) if mo: self._print_source(filename, lineno+start-10, lineno+start+10) return else: self._print("Pattern not found.")
Example #4
Source File: test_inspect.py From oss-ftp with MIT License | 6 votes |
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec compile(source, fn, 'single') in ns inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
Example #5
Source File: debugger.py From pycopia with Apache License 2.0 | 6 votes |
def search(self, argv): """search <pattern> Search the source file for the regular expression pattern.""" patt = re.compile(" ".join(argv[1:])) filename = self._dbg.curframe.f_code.co_filename if self._dbg.lineno is None: start = 0 else: start = max(0, self._dbg.lineno - 9) lines = linecache.getlines(filename)[start:] for lineno, line in enumerate(lines): mo = patt.search(line) if mo: self._print_source(filename, lineno+start-10, lineno+start+10) return else: self._print("Pattern not found.")
Example #6
Source File: test_inspect.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(keepends=True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec(compile(source, fn, 'single'), ns) inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
Example #7
Source File: ulinecache.py From Computable with MIT License | 6 votes |
def getlines(filename, module_globals=None): """Get the lines (as unicode) for a file from the cache. Update the cache if it doesn't contain an entry for this file already.""" filename = py3compat.cast_bytes(filename, sys.getfilesystemencoding()) lines = linecache.getlines(filename, module_globals=module_globals) # The bits we cache ourselves can be unicode. if (not lines) or isinstance(lines[0], unicode): return lines readline = openpy._list_readline(lines) try: encoding, _ = openpy.detect_encoding(readline) except SyntaxError: encoding = 'ascii' return [l.decode(encoding, 'replace') for l in lines] # This is a straight copy of linecache.getline
Example #8
Source File: test_inspect.py From BinderFilter with MIT License | 6 votes |
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec compile(source, fn, 'single') in ns inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
Example #9
Source File: test_inspect.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec compile(source, fn, 'single') in ns inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
Example #10
Source File: executing.py From executing with MIT License | 6 votes |
def for_filename(cls, filename, module_globals=None, use_cache=True): source_cache = cls._class_local('__source_cache', {}) if use_cache: try: return source_cache[filename] except KeyError: pass if isinstance(filename, Path): filename = str(filename) if not use_cache: linecache.checkcache(filename) lines = tuple(linecache.getlines(filename, module_globals)) result = source_cache[filename] = cls._for_filename_and_lines(filename, lines) return result
Example #11
Source File: test_inspect.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(keepends=True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec(compile(source, fn, 'single'), ns) inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
Example #12
Source File: test_inspect.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec compile(source, fn, 'single') in ns inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
Example #13
Source File: test_linecache.py From android_universal with MIT License | 6 votes |
def test_getline(self): getline = linecache.getline # Bad values for line number should return an empty string self.assertEqual(getline(FILENAME, 2**15), EMPTY) self.assertEqual(getline(FILENAME, -1), EMPTY) # Float values currently raise TypeError, should it? self.assertRaises(TypeError, getline, FILENAME, 1.1) # Bad filenames should return an empty string self.assertEqual(getline(EMPTY, 1), EMPTY) self.assertEqual(getline(INVALID_NAME, 1), EMPTY) # Check module loading for entry in MODULES: filename = os.path.join(MODULE_PATH, entry) + '.py' with open(filename) as file: for index, line in enumerate(file): self.assertEqual(line, getline(filename, index + 1)) # Check that bogus data isn't returned (issue #1309567) empty = linecache.getlines('a/b/c/__init__.py') self.assertEqual(empty, [])
Example #14
Source File: test_inspect.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_proceed_with_fake_filename(self): '''doctest monkeypatches linecache to enable inspection''' fn, source = '<test>', 'def x(): pass\n' getlines = linecache.getlines def monkey(filename, module_globals=None): if filename == fn: return source.splitlines(keepends=True) else: return getlines(filename, module_globals) linecache.getlines = monkey try: ns = {} exec(compile(source, fn, 'single'), ns) inspect.getsource(ns["x"]) finally: linecache.getlines = getlines
Example #15
Source File: pdbpp.py From pdbpp with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _patch_linecache_for_source_highlight(self): orig = pdb.linecache.getlines def wrapped_getlines(filename, globals): """Wrap linecache.getlines to highlight source (for do_list).""" old_cache = pdb.linecache.cache.pop(filename, None) try: lines = orig(filename, globals) finally: if old_cache: pdb.linecache.cache[filename] = old_cache source = self.format_source("".join(lines)) if sys.version_info < (3,): source = self.try_to_encode(source) return source.splitlines(True) pdb.linecache.getlines = wrapped_getlines try: yield finally: pdb.linecache.getlines = orig
Example #16
Source File: format_stack.py From abu with GNU General Public License v3.0 | 5 votes |
def format_outer_frames(context=5, stack_start=None, stack_end=None, ignore_ipython=True): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = inspect.getouterframes(inspect.currentframe()) output = list() for i, (frame, filename, line_no, func_name, lines, index) \ in enumerate(records): # Look inside the frame's globals dictionary for __file__, which should # be better. better_fn = frame.f_globals.get('__file__', None) if isinstance(better_fn, str): # Check the type just in case someone did something weird with # __file__. It might also be None if the error occurred during # import. filename = better_fn if filename.endswith('.pyc'): filename = filename[:-4] + '.py' if ignore_ipython: # Hack to avoid printing the internals of IPython if (os.path.basename(filename) == 'iplib.py' and func_name in ('safe_execfile', 'runcode')): break maybeStart = line_no - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = linecache.getlines(filename)[start:end] # pad with empty lines if necessary if maybeStart < 0: lines = (['\n'] * -maybeStart) + lines if len(lines) < context: lines += ['\n'] * (context - len(lines)) buf = list(records[i]) buf[LNUM_POS] = line_no buf[INDEX_POS] = line_no - 1 - start buf[LINES_POS] = lines output.append(tuple(buf)) return '\n'.join(format_records(output[stack_end:stack_start:-1]))
Example #17
Source File: test_linecache.py From android_universal with MIT License | 5 votes |
def test_lazycache_provide_after_failed_lookup(self): linecache.clearcache() lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() linecache.getlines(NONEXISTENT_FILENAME) linecache.lazycache(NONEXISTENT_FILENAME, globals()) self.assertEqual(lines, linecache.updatecache(NONEXISTENT_FILENAME))
Example #18
Source File: format_stack.py From abu with GNU General Public License v3.0 | 5 votes |
def _fixed_getframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in enumerate(aux): maybeStart = lnum - 1 - context // 2 start = max(maybeStart, 0) end = start + context lines = linecache.getlines(file)[start:end] # pad with empty lines if necessary if maybeStart < 0: lines = (['\n'] * -maybeStart) + lines if len(lines) < context: lines += ['\n'] * (context - len(lines)) buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:]
Example #19
Source File: test_linecache.py From android_universal with MIT License | 5 votes |
def test_getlines(self): self.assertRaises((SyntaxError, UnicodeDecodeError), linecache.getlines, self.file_name)
Example #20
Source File: test_linecache.py From android_universal with MIT License | 5 votes |
def test_getlines(self): lines = linecache.getlines(self.file_name) self.assertEqual(lines, self.file_list)
Example #21
Source File: test_linecache.py From android_universal with MIT License | 5 votes |
def test_lazycache_smoke(self): lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) linecache.clearcache() self.assertEqual( True, linecache.lazycache(NONEXISTENT_FILENAME, globals())) self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME])) # Note here that we're looking up a nonexistent filename with no # globals: this would error if the lazy value wasn't resolved. self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME))
Example #22
Source File: test_linecache.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_memoryerror(self): lines = linecache.getlines(FILENAME) self.assertTrue(lines) def raise_memoryerror(*args, **kwargs): raise MemoryError with support.swap_attr(linecache, 'updatecache', raise_memoryerror): lines2 = linecache.getlines(FILENAME) self.assertEqual(lines2, lines) linecache.clearcache() with support.swap_attr(linecache, 'updatecache', raise_memoryerror): lines3 = linecache.getlines(FILENAME) self.assertEqual(lines3, []) self.assertEqual(linecache.getlines(FILENAME), lines)
Example #23
Source File: format_stack.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def format_outer_frames(context=5, stack_start=None, stack_end=None, ignore_ipython=True): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = inspect.getouterframes(inspect.currentframe()) output = list() for i, (frame, filename, line_no, func_name, lines, index) \ in enumerate(records): # Look inside the frame's globals dictionary for __file__, which should # be better. better_fn = frame.f_globals.get('__file__', None) if isinstance(better_fn, str): # Check the type just in case someone did something weird with # __file__. It might also be None if the error occurred during # import. filename = better_fn if filename.endswith('.pyc'): filename = filename[:-4] + '.py' if ignore_ipython: # Hack to avoid printing the internals of IPython if (os.path.basename(filename) in ('iplib.py', 'py3compat.py') and func_name in ('execfile', 'safe_execfile', 'runcode')): break maybe_start = line_no - 1 - context // 2 start = max(maybe_start, 0) end = start + context lines = linecache.getlines(filename)[start:end] buf = list(records[i]) buf[LNUM_POS] = line_no buf[INDEX_POS] = line_no - 1 - start buf[LINES_POS] = lines output.append(tuple(buf)) return '\n'.join(format_records(output[stack_end:stack_start:-1]))
Example #24
Source File: format_stack.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _fixed_getframes(etb, context=1, tb_offset=0): LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) # If the error is at the console, don't build any context, since it would # otherwise produce 5 blank lines printed out (there is no file at the # console) rec_check = records[tb_offset:] try: rname = rec_check[0][1] if rname == '<ipython console>' or rname.endswith('<string>'): return rec_check except IndexError: pass aux = traceback.extract_tb(etb) assert len(records) == len(aux) for i, (file, lnum, _, _) in enumerate(aux): maybe_start = lnum - 1 - context // 2 start = max(maybe_start, 0) end = start + context lines = linecache.getlines(file)[start:end] buf = list(records[i]) buf[LNUM_POS] = lnum buf[INDEX_POS] = lnum - 1 - start buf[LINES_POS] = lines records[i] = tuple(buf) return records[tb_offset:]
Example #25
Source File: compat.py From pth-toolkit with BSD 2-Clause "Simplified" License | 5 votes |
def _get_source_encoding(filename): """Detect, cache and return the encoding of Python source at filename""" try: return linecache.cache[filename].encoding except (AttributeError, KeyError): encoding = _detect_encoding(linecache.getlines(filename)) if filename in linecache.cache: newtuple = _EncodingTuple(linecache.cache[filename]) newtuple.encoding = encoding linecache.cache[filename] = newtuple return encoding
Example #26
Source File: test_linecache.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_no_ending_newline(self): self.addCleanup(support.unlink, support.TESTFN) with open(support.TESTFN, "w") as fp: fp.write(SOURCE_3) lines = linecache.getlines(support.TESTFN) self.assertEqual(lines, ["\n", "def f():\n", " return 3\n"])
Example #27
Source File: test_linecache.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_getline(self): getline = linecache.getline # Bad values for line number should return an empty string self.assertEqual(getline(FILENAME, 2**15), EMPTY) self.assertEqual(getline(FILENAME, -1), EMPTY) # Float values currently raise TypeError, should it? self.assertRaises(TypeError, getline, FILENAME, 1.1) # Bad filenames should return an empty string self.assertEqual(getline(EMPTY, 1), EMPTY) self.assertEqual(getline(INVALID_NAME, 1), EMPTY) # Check whether lines correspond to those from file iteration for entry in TESTS: filename = os.path.join(TEST_PATH, entry) + '.py' for index, line in enumerate(open(filename)): self.assertEqual(line, getline(filename, index + 1)) # Check module loading for entry in MODULES: filename = os.path.join(MODULE_PATH, entry) + '.py' for index, line in enumerate(open(filename)): self.assertEqual(line, getline(filename, index + 1)) # Check that bogus data isn't returned (issue #1309567) empty = linecache.getlines('a/b/c/__init__.py') self.assertEqual(empty, [])
Example #28
Source File: executing.py From executing with MIT License | 5 votes |
def for_filename(cls, filename, module_globals=None): source_cache = cls._class_local('__source_cache', {}) try: return source_cache[filename] except KeyError: pass lines = linecache.getlines(filename, module_globals) result = source_cache[filename] = cls(filename, ''.join(lines)) return result
Example #29
Source File: event.py From python-hunter with BSD 2-Clause "Simplified" License | 5 votes |
def yield_lines(filename, module_globals, start, collector, limit=10, leading_whitespace_re=LEADING_WHITESPACE_RE): dedent = None amount = 0 for line in linecache.getlines(filename, module_globals)[start:start + limit]: if dedent is None: dedent = leading_whitespace_re.findall(line) dedent = dedent[0] if dedent else '' amount = len(dedent) elif not line.startswith(dedent): break collector(line) yield line[amount:]
Example #30
Source File: test_linecache.py From android_universal with MIT License | 5 votes |
def test_lazycache_already_cached(self): linecache.clearcache() lines = linecache.getlines(NONEXISTENT_FILENAME, globals()) self.assertEqual( False, linecache.lazycache(NONEXISTENT_FILENAME, globals())) self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME]))