本文整理汇总了Python中linecache.updatecache方法的典型用法代码示例。如果您正苦于以下问题:Python linecache.updatecache方法的具体用法?Python linecache.updatecache怎么用?Python linecache.updatecache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linecache
的用法示例。
在下文中一共展示了linecache.updatecache方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_large_file_via_line_cache
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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]
示例2: test_lazycache_provide_after_failed_lookup
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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))
示例3: test_memoryerror
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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)
示例4: test_extract_stack_lookup_lines
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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")
示例5: test_extract_stackup_deferred_lookup_lines
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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")
示例6: test_locals
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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'})
示例7: test_lookup_lines
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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")
示例8: test_no_locals
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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)
示例9: remote_exec
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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
示例10: formatCode
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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
示例11: formatCode
# 需要导入模块: import linecache [as 别名]
# 或者: from linecache import updatecache [as 别名]
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