#! /usr/bin/env python3"""combinerefs pathA helper for analyzing PYTHONDUMPREFS output.When the PYTHONDUMPREFS envar is set in a debug build, at Python shutdowntime Py_FinalizeEx() prints the list of all live objects twice: first itprints the repr() of each object while the interpreter is still fully intact.After cleaning up everything it can, it prints all remaining live objectsagain, but the second time just prints their addresses, refcounts, and typenames (because the interpreter has been torn down, calling repr methods atthis point can get into infinite loops or blow up).Save all this output into a file, then run this script passing the path tothat file. The script finds both output chunks, combines them, then printsa line of output for each object still alive at the end:address refcnt typename repraddress is the address of the object, in whatever format the platform Cproduces for a %p format code.refcnt is of the form"[" ref "]"when the object's refcount is the same in both PYTHONDUMPREFS output blocks,or"[" ref_before "->" ref_after "]"if the refcount changed.typename is object->ob_type->tp_name, extracted from the second PYTHONDUMPREFSoutput block.repr is repr(object), extracted from the first PYTHONDUMPREFS output block.CAUTION: If object is a container type, it may not actually contain all theobjects shown in the repr: the repr was captured from the first output block,and some of the containees may have been released since then. For example,it's common for the line showing the dict of interned strings to displaystrings that no longer exist at the end of Py_FinalizeEx; this can be recognized(albeit painfully) because such containees don't have a line of their own.The objects are listed in allocation order, with most-recently allocatedprinted first, and the first object allocated printed last.Simple examples:00857060 [14] str '__len__'The str object '__len__' is alive at shutdown time, and both PYTHONDUMPREFSoutput blocks said there were 14 references to it. This is probably due toC modules that intern the string "__len__" and keep a reference to it in afile static.00857038 [46->5] tuple ()46-5 = 41 references to the empty tuple were removed by the cleanup actionsbetween the times PYTHONDUMPREFS produced output.00858028 [1025->1456] str '<dummy key>'The string '<dummy key>', which is used in dictobject.c to overwrite a realkey that gets deleted, grew several hundred references during cleanup. Itsuggests that stuff did get removed from dicts by cleanup, but that the dictsthemselves are staying alive for some reason. """import reimport sys# Generate lines from fileiter. If whilematch is true, continue reading# while the regexp object pat matches line. If whilematch is false, lines# are read so long as pat doesn't match them. In any case, the first line# that doesn't match pat (when whilematch is true), or that does match pat# (when whilematch is false), is lost, and fileiter will resume at the line# following it.def read(fileiter, pat, whilematch):for line in fileiter:if bool(pat.match(line)) == whilematch:yield lineelse:breakdef combinefile(f):fi = iter(f)for line in read(fi, re.compile(r'^Remaining objects:$'), False):passcrack = re.compile(r'([a-zA-Z\d]+) \[(\d+)\] (.*)')addr2rc = {}addr2guts = {}before = 0for line in read(fi, re.compile(r'^Remaining object addresses:$'), False):m = crack.match(line)if m:addr, addr2rc[addr], addr2guts[addr] = m.groups()before += 1else:print('??? skipped:', line)after = 0for line in read(fi, crack, True):after += 1m = crack.match(line)assert maddr, rc, guts = m.groups() # guts is type name hereif addr not in addr2rc:print('??? new object created while tearing down:', line.rstrip())continueprint(addr, end=' ')if rc == addr2rc[addr]:print('[%s]' % rc, end=' ')else:print('[%s->%s]' % (addr2rc[addr], rc), end=' ')print(guts, addr2guts[addr])print("%d objects before, %d after" % (before, after))def combine(fname):with open(fname) as f:combinefile(f)if __name__ == '__main__':combine(sys.argv[1])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。