Python linecache.updatecache() Examples

The following are 22 code examples of linecache.updatecache(). 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: parse-large-file.py    From miscellaneous with Apache License 2.0 6 votes vote down vote up
def parse_large_file_via_line_cache(file_path):
    file_lines = get_file_lines(file_path)
    number_of_threads = multiprocessing.cpu_count()/2
    slice_lines = file_lines / number_of_threads

    cached_lines = linecache.updatecache(file_path)
    threads = []
    for i in range(number_of_threads):
        start_line = i * slice_lines
        stop_line = max((i+1)*slice_lines, file_lines) if i+1 == number_of_threads else (i+1)*slice_lines
        t_name = 'Line cache thread {}'.format(i)
        print('{} {} -> {}'.format(t_name, start_line, stop_line))
        t = threading.Thread(target=parse_line_range, name=t_name,
                             args=(cached_lines, start_line, stop_line))
        threads.append(t)
        t.start()

    [t.join() for t in threads] 
Example #2
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_extract_stack_lookup_lines(self):
        linecache.clearcache()
        linecache.updatecache('/foo.py', globals())
        c = test_code('/foo.py', 'method')
        f = test_frame(c, None, None)
        s = traceback.StackSummary.extract(iter([(f, 6)]), lookup_lines=True)
        linecache.clearcache()
        self.assertEqual(s[0].line, "import sys") 
Example #3
Source File: test_linecache.py    From android_universal with MIT License 5 votes vote down vote up
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 #4
Source File: test_linecache.py    From android_universal with MIT License 5 votes vote down vote up
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 #5
Source File: formattrace.py    From epdb with MIT License 5 votes vote down vote up
def formatCode(frame, stream):
    _updatecache = linecache.updatecache

    def updatecache(*args):
        # linecache.updatecache looks in the module search path for
        # files that match the module name. This is a problem if you
        # have a file without source with the same name as a python
        # standard library module. We'll just check to see if the file
        # exists first and require exact path matches.
        if not os.access(args[0], os.R_OK):
            return []
        return _updatecache(*args)
    linecache.updatecache = updatecache
    try:
        try:
            frameInfo = inspect.getframeinfo(frame, context=1)
        except:  # noqa
            frameInfo = inspect.getframeinfo(frame, context=0)
        fileName, lineNo, funcName, text, idx = frameInfo

        stream.write('  File "%s", line %d, in %s\n' %
                     (fileName, lineNo, funcName))
        if text is not None and len(text) > idx:
            # If the source file is not available, we may not be able to get
            # the line
            stream.write('    %s\n' % text[idx].strip())
    finally:
        linecache.updatecache = _updatecache 
Example #6
Source File: formattrace.py    From conary with Apache License 2.0 5 votes vote down vote up
def formatCode(frame, stream):
    _updatecache = linecache.updatecache
    def updatecache(*args):
        # linecache.updatecache looks in the module search path for
        # files that match the module name. This is a problem if you
        # have a file without source with the same name as a python
        # standard library module. We'll just check to see if the file
        # exists first and require exact path matches.
        if not os.access(args[0], os.R_OK):
            return []
        return _updatecache(*args)
    linecache.updatecache = updatecache
    try:
        try:
            frameInfo = inspect.getframeinfo(frame, context=1)
        except:
            frameInfo = inspect.getframeinfo(frame, context=0)
        fileName, lineNo, funcName, text, idx = frameInfo

        stream.write('  File "%s", line %d, in %s\n' %
            (fileName, lineNo, funcName))
        if text is not None and len(text) > idx:
            # If the source file is not available, we may not be able to get
            # the line
            stream.write('    %s\n' % text[idx].strip())
    finally:
        linecache.updatecache = _updatecache 
Example #7
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_no_locals(self):
        linecache.updatecache('/foo.py', globals())
        e = Exception("uh oh")
        c = test_code('/foo.py', 'method')
        f = test_frame(c, globals(), {'something': 1})
        tb = test_tb(f, 6, None)
        exc = traceback.TracebackException(Exception, e, tb)
        self.assertEqual(exc.stack[0].locals, None) 
Example #8
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_locals(self):
        linecache.updatecache('/foo.py', globals())
        e = Exception("uh oh")
        c = test_code('/foo.py', 'method')
        f = test_frame(c, globals(), {'something': 1, 'other': 'string'})
        tb = test_tb(f, 6, None)
        exc = traceback.TracebackException(
            Exception, e, tb, capture_locals=True)
        self.assertEqual(
            exc.stack[0].locals, {'something': '1', 'other': "'string'"}) 
Example #9
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_lookup_lines(self):
        linecache.clearcache()
        e = Exception("uh oh")
        c = test_code('/foo.py', 'method')
        f = test_frame(c, None, None)
        tb = test_tb(f, 6, None)
        exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False)
        self.assertEqual({}, linecache.cache)
        linecache.updatecache('/foo.py', globals())
        self.assertEqual(exc.stack[0].line, "import sys") 
Example #10
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_locals(self):
        linecache.updatecache('/foo.py', globals())
        c = test_code('/foo.py', 'method')
        f = test_frame(c, globals(), {'something': 1})
        s = traceback.StackSummary.extract(iter([(f, 6)]), capture_locals=True)
        self.assertEqual(s[0].locals, {'something': '1'}) 
Example #11
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_extract_stackup_deferred_lookup_lines(self):
        linecache.clearcache()
        c = test_code('/foo.py', 'method')
        f = test_frame(c, None, None)
        s = traceback.StackSummary.extract(iter([(f, 6)]), lookup_lines=False)
        self.assertEqual({}, linecache.cache)
        linecache.updatecache('/foo.py', globals())
        self.assertEqual(s[0].line, "import sys") 
Example #12
Source File: test_linecache.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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 #13
Source File: test_linecache.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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 #14
Source File: test_linecache.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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 #15
Source File: gateway.py    From execnet with MIT License 5 votes vote down vote up
def remote_exec(self, source, **kwargs):
        """ return channel object and connect it to a remote
            execution thread where the given ``source`` executes.

            * ``source`` is a string: execute source string remotely
              with a ``channel`` put into the global namespace.
            * ``source`` is a pure function: serialize source and
              call function with ``**kwargs``, adding a
              ``channel`` object to the keyword arguments.
            * ``source`` is a pure module: execute source of module
              with a ``channel`` in its global namespace

            In all cases the binding ``__name__='__channelexec__'``
            will be available in the global namespace of the remotely
            executing code.
        """
        call_name = None
        file_name = None
        if isinstance(source, types.ModuleType):
            file_name = inspect.getsourcefile(source)
            linecache.updatecache(file_name)
            source = inspect.getsource(source)
        elif isinstance(source, types.FunctionType):
            call_name = source.__name__
            file_name = inspect.getsourcefile(source)
            source = _source_of_function(source)
        else:
            source = textwrap.dedent(str(source))

        if not call_name and kwargs:
            raise TypeError("can't pass kwargs to non-function remote_exec")

        channel = self.newchannel()
        self._send(
            Message.CHANNEL_EXEC,
            channel.id,
            gateway_base.dumps_internal((source, file_name, call_name, kwargs)),
        )
        return channel 
Example #16
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_no_locals(self):
        linecache.updatecache('/foo.py', globals())
        e = Exception("uh oh")
        c = test_code('/foo.py', 'method')
        f = test_frame(c, globals(), {'something': 1})
        tb = test_tb(f, 6, None)
        exc = traceback.TracebackException(Exception, e, tb)
        self.assertEqual(exc.stack[0].locals, None) 
Example #17
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_locals(self):
        linecache.updatecache('/foo.py', globals())
        e = Exception("uh oh")
        c = test_code('/foo.py', 'method')
        f = test_frame(c, globals(), {'something': 1, 'other': 'string'})
        tb = test_tb(f, 6, None)
        exc = traceback.TracebackException(
            Exception, e, tb, capture_locals=True)
        self.assertEqual(
            exc.stack[0].locals, {'something': '1', 'other': "'string'"}) 
Example #18
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_lookup_lines(self):
        linecache.clearcache()
        e = Exception("uh oh")
        c = test_code('/foo.py', 'method')
        f = test_frame(c, None, None)
        tb = test_tb(f, 6, None)
        exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False)
        self.assertEqual({}, linecache.cache)
        linecache.updatecache('/foo.py', globals())
        self.assertEqual(exc.stack[0].line, "import sys") 
Example #19
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_locals(self):
        linecache.updatecache('/foo.py', globals())
        c = test_code('/foo.py', 'method')
        f = test_frame(c, globals(), {'something': 1})
        s = traceback.StackSummary.extract(iter([(f, 6)]), capture_locals=True)
        self.assertEqual(s[0].locals, {'something': '1'}) 
Example #20
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_extract_stackup_deferred_lookup_lines(self):
        linecache.clearcache()
        c = test_code('/foo.py', 'method')
        f = test_frame(c, None, None)
        s = traceback.StackSummary.extract(iter([(f, 6)]), lookup_lines=False)
        self.assertEqual({}, linecache.cache)
        linecache.updatecache('/foo.py', globals())
        self.assertEqual(s[0].line, "import sys") 
Example #21
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_extract_stack_lookup_lines(self):
        linecache.clearcache()
        linecache.updatecache('/foo.py', globals())
        c = test_code('/foo.py', 'method')
        f = test_frame(c, None, None)
        s = traceback.StackSummary.extract(iter([(f, 6)]), lookup_lines=True)
        linecache.clearcache()
        self.assertEqual(s[0].line, "import sys") 
Example #22
Source File: test_linecache.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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)