本文整理汇总了Python中tracemalloc.get_object_traceback函数的典型用法代码示例。如果您正苦于以下问题:Python get_object_traceback函数的具体用法?Python get_object_traceback怎么用?Python get_object_traceback使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_object_traceback函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_clear_traces
def test_clear_traces(self):
obj, obj_traceback = allocate_bytes(123)
traceback = tracemalloc.get_object_traceback(obj)
self.assertIsNotNone(traceback)
tracemalloc.clear_traces()
traceback2 = tracemalloc.get_object_traceback(obj)
self.assertIsNone(traceback2)
示例2: memory_obj
def memory_obj(args: tuple, packet: ircp.Packet, ___: dict):
""" Print the biggest memory hogs """
if not _IS_TRACING:
return packet.notice(
"Sorry, but tracing is currently disabled. "
'Please restart probot with the "PYTHONTRACEMALLOC=NFRAME" '
"environment variable."
)
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics("filename")
num = 0
if len(args) >= 2:
try:
num = int(args[1])
except ValueError:
return packet.notice("Your argument must be an integer")
else:
return packet.notice("You must specify an object to inspect!")
if len(top_stats) >= num:
output = [packet.notice("Memory hog #{}".format(num))]
obj = top_stats[num]
trace = tracemalloc.get_object_traceback(obj)
for line in trace:
output.append(packet.notice(line))
return output
else:
return packet.notice("Sorry, but that object does not exist")
示例3: _formatwarnmsg_impl
def _formatwarnmsg_impl(msg):
import linecache
s = ("%s:%s: %s: %s\n"
% (msg.filename, msg.lineno, msg.category.__name__,
msg.message))
if msg.line is None:
line = linecache.getline(msg.filename, msg.lineno)
else:
line = msg.line
if line:
line = line.strip()
s += " %s\n" % line
if msg.source is not None:
import tracemalloc
tb = tracemalloc.get_object_traceback(msg.source)
if tb is not None:
s += 'Object allocated at (most recent call first):\n'
for frame in tb:
s += (' File "%s", lineno %s\n'
% (frame.filename, frame.lineno))
line = linecache.getline(frame.filename, frame.lineno)
if line:
line = line.strip()
s += ' %s\n' % line
return s
示例4: _formatwarnmsg_impl
def _formatwarnmsg_impl(msg):
category = msg.category.__name__
s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n"
if msg.line is None:
try:
import linecache
line = linecache.getline(msg.filename, msg.lineno)
except Exception:
# When a warning is logged during Python shutdown, linecache
# and the import machinery don't work anymore
line = None
linecache = None
else:
line = msg.line
if line:
line = line.strip()
s += " %s\n" % line
if msg.source is not None:
try:
import tracemalloc
# Logging a warning should not raise a new exception:
# catch Exception, not only ImportError and RecursionError.
except Exception:
# don't suggest to enable tracemalloc if it's not available
tracing = True
tb = None
else:
tracing = tracemalloc.is_tracing()
try:
tb = tracemalloc.get_object_traceback(msg.source)
except Exception:
# When a warning is logged during Python shutdown, tracemalloc
# and the import machinery don't work anymore
tb = None
if tb is not None:
s += 'Object allocated at (most recent call last):\n'
for frame in tb:
s += (' File "%s", lineno %s\n'
% (frame.filename, frame.lineno))
try:
if linecache is not None:
line = linecache.getline(frame.filename, frame.lineno)
else:
line = None
except Exception:
line = None
if line:
line = line.strip()
s += ' %s\n' % line
elif not tracing:
s += (f'{category}: Enable tracemalloc to get the object '
f'allocation traceback\n')
return s
示例5: test_set_traceback_limit
def test_set_traceback_limit(self):
obj_size = 10
tracemalloc.stop()
self.assertRaises(ValueError, tracemalloc.start, -1)
tracemalloc.stop()
tracemalloc.start(10)
obj2, obj2_traceback = allocate_bytes(obj_size)
traceback = tracemalloc.get_object_traceback(obj2)
self.assertEqual(len(traceback), 10)
self.assertEqual(traceback, obj2_traceback)
tracemalloc.stop()
tracemalloc.start(1)
obj, obj_traceback = allocate_bytes(obj_size)
traceback = tracemalloc.get_object_traceback(obj)
self.assertEqual(len(traceback), 1)
self.assertEqual(traceback, obj_traceback)
示例6: fork_child
def fork_child(self):
if not tracemalloc.is_tracing():
return 2
obj_size = 12345
obj, obj_traceback = allocate_bytes(obj_size)
traceback = tracemalloc.get_object_traceback(obj)
if traceback is None:
return 3
# everything is fine
return 0
示例7: __del__
def __del__(self):
if self._watcher:
tb = get_object_traceback(self)
tb_msg = ''
if tb is not None:
tb_msg = '\n'.join(tb.format())
tb_msg = '\nTraceback:\n' + tb_msg
warnings.warn("Failed to close watcher %r%s" % (self, tb_msg),
ResourceWarning)
# may fail if __init__ did; will be harmlessly printed
self.close()
示例8: show
def show(filename=None):
global before_objects
gc.collect()
after_objects = gc.get_objects()
frame = sys._getframe()
globals_ = globals()
num_leaks = 0
before_id_set = set(map(id, before_objects))
if filename is not None:
f = open(filename, 'w')
else:
f = sys.stderr
for obj in after_objects:
if id(obj) not in before_id_set:
if obj in (before_objects, frame):
continue
num_leaks += 1
print("Leak: id=0x{:x} type={} {}".format(id(obj), type(obj), _get_detail(obj)), file=f)
tb = tracemalloc.get_object_traceback(obj)
if tb is None:
print("Traceback: None", file=f)
else:
print("Traceback:", file=f)
print("\n".join(tb.format(MAX_FRAMES)), file=f)
print(file=f)
print("Referrers:", file=f)
for ref in gc.get_referrers(obj):
if ref in (after_objects, before_objects, frame, globals_):
continue
print(" id=0x{:x} type={} {}".format(id(ref), type(ref), _get_detail(ref)), file=f)
print(" traceback: {}".format(tracemalloc.get_object_traceback(ref)), file=f)
print(file=f)
print(file=f)
print("Total leaks: {}".format(num_leaks), file=f)
if filename is not None:
f.close()
tracemalloc.stop()
gc.enable()
示例9: _formatwarnmsg_impl
def _formatwarnmsg_impl(msg):
s = ("%s:%s: %s: %s\n"
% (msg.filename, msg.lineno, msg.category.__name__,
msg.message))
if msg.line is None:
try:
import linecache
line = linecache.getline(msg.filename, msg.lineno)
except Exception:
# When a warning is logged during Python shutdown, linecache
# and the import machinery don't work anymore
line = None
linecache = None
else:
line = msg.line
if line:
line = line.strip()
s += " %s\n" % line
if msg.source is not None:
try:
import tracemalloc
tb = tracemalloc.get_object_traceback(msg.source)
except Exception:
# When a warning is logged during Python shutdown, tracemalloc
# and the import machinery don't work anymore
tb = None
if tb is not None:
s += 'Object allocated at (most recent call first):\n'
for frame in tb:
s += (' File "%s", lineno %s\n'
% (frame.filename, frame.lineno))
try:
if linecache is not None:
line = linecache.getline(frame.filename, frame.lineno)
else:
line = None
except Exception:
line = None
if line:
line = line.strip()
s += ' %s\n' % line
return s
示例10: test_get_object_traceback
def test_get_object_traceback(self):
tracemalloc.clear_traces()
obj_size = 12345
obj, obj_traceback = allocate_bytes(obj_size)
traceback = tracemalloc.get_object_traceback(obj)
self.assertEqual(traceback, obj_traceback)