From 46ea4b84747f206859243b9150fc534dd857de4b Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Fri, 18 May 2007 21:45:15 +0000 Subject: [PATCH] improved ~objects --- direct/src/showbase/ObjectPool.py | 15 ++++--- direct/src/showbase/PythonUtil.py | 71 ++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/direct/src/showbase/ObjectPool.py b/direct/src/showbase/ObjectPool.py index 00a6e498fe..3bd874ed3d 100755 --- a/direct/src/showbase/ObjectPool.py +++ b/direct/src/showbase/ObjectPool.py @@ -4,7 +4,8 @@ __all__ = ['Diff', 'ObjectPool'] from direct.directnotify.DirectNotifyGlobal import directNotify from direct.showbase.PythonUtil import invertDictLossless, makeList, safeRepr -from direct.showbase.PythonUtil import getNumberedTypedString +from direct.showbase.PythonUtil import getNumberedTypedString, getNumberedTypedSortedString +from direct.showbase.PythonUtil import getNumberedTypedSortedStringWithReferrersGen import types import gc @@ -96,8 +97,8 @@ class ObjectPool: s += '\n%s\t%s' % (count, typ) return s - def printObjsByType(self): - print 'Object Pool: Objects By Type' + def printObjsByType(self, printReferrers=False): + print 'Object Pool: Objects By Type' print '\n============================' counts = list(set(self._count2types.keys())) counts.sort() @@ -105,8 +106,12 @@ class ObjectPool: for count in counts: types = makeList(self._count2types[count]) for typ in types: - print 'TYPE: %s' % repr(typ) - print getNumberedTypedString(self._type2objs[typ]) + print 'TYPE: %s, %s objects' % (repr(typ), len(self._type2objs[typ])) + if printReferrers: + for line in getNumberedTypedSortedStringWithReferrersGen(self._type2objs[typ]): + print line + else: + print getNumberedTypedSortedString(self._type2objs[typ]) def containerLenStr(self): s = 'Object Pool: Container Lengths' diff --git a/direct/src/showbase/PythonUtil.py b/direct/src/showbase/PythonUtil.py index b930493da2..1e93f6fc1b 100644 --- a/direct/src/showbase/PythonUtil.py +++ b/direct/src/showbase/PythonUtil.py @@ -21,6 +21,8 @@ __all__ = ['enumerate', 'unique', 'indent', 'nonRepeatingRandomList', 'SingletonError', 'printListEnum', 'gcDebugOn', 'safeRepr', 'fastRepr', 'tagRepr', 'tagWithCaller', 'isDefaultValue', 'set_trace', 'pm', 'ScratchPad', 'Sync', 'RefCounter', 'itype', 'getNumberedTypedString', +'getNumberedTypedSortedString', 'getNumberedTypedSortedStringWithReferrers', +'getNumberedTypedSortedStringWithReferrersGen', 'printNumberedTyped', 'DelayedCall', 'DelayedFunctor', 'FrameDelayedCall', 'ArgumentEater', 'ClassTree', 'getBase', 'superFlattenShip','HotkeyBreaker','logMethodCalls','GoldenRatio', @@ -38,6 +40,7 @@ import sys import random import time import new +import gc #if __debug__: import traceback @@ -2317,17 +2320,83 @@ def getNumberedTypedString(items, maxLen=5000, numPrefix=''): format = numPrefix + '%0' + '%s' % digits + 'i:%s \t%s' first = True s = '' + snip = '' for i in xrange(len(items)): if not first: s += '\n' first = False objStr = fastRepr(items[i]) if len(objStr) > maxLen: - snip = '' objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip) s += format % (i, itype(items[i]), objStr) return s +def getNumberedTypedSortedString(items, maxLen=5000, numPrefix=''): + """get a string that has each item of the list on its own line, + the items are stringwise-sorted, and each item is numbered on + the left from zero""" + digits = 0 + n = len(items) + while n > 0: + digits += 1 + n /= 10 + digits = digits + format = numPrefix + '%0' + '%s' % digits + 'i:%s \t%s' + snip = '' + strs = [] + for item in items: + objStr = fastRepr(item) + if len(objStr) > maxLen: + objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip) + strs.append(objStr) + first = True + s = '' + strs.sort() + for i in xrange(len(strs)): + if not first: + s += '\n' + first = False + objStr = strs[i] + s += format % (i, itype(items[i]), strs[i]) + return s + +def getNumberedTypedSortedStringWithReferrersGen(items, maxLen=10000, numPrefix=''): + """get a string that has each item of the list on its own line, + the items are stringwise-sorted, the object's referrers are shown, + and each item is numbered on the left from zero""" + digits = 0 + n = len(items) + while n > 0: + digits += 1 + n /= 10 + digits = digits + format = numPrefix + '%0' + '%s' % digits + 'i:%s @ %s \t%s' + snip = '' + strs = [] + for item in items: + strs.append(fastRepr(item)) + strs.sort() + for i in xrange(len(strs)): + item = items[i] + objStr = strs[i] + objStr += ', \tREFERRERS=[' + referrers = gc.get_referrers(item) + for ref in referrers: + objStr += '%s@%s, ' % (itype(ref), id(ref)) + objStr += ']' + if len(objStr) > maxLen: + objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip) + yield format % (i, itype(items[i]), id(items[i]), objStr) + +def getNumberedTypedSortedStringWithReferrers(items, maxLen=10000, numPrefix=''): + """get a string that has each item of the list on its own line, + the items are stringwise-sorted, the object's referrers are shown, + and each item is numbered on the left from zero""" + s = '' + for line in getNumberedTypedSortedStringWithReferrersGen(items, maxLen, numPrefix): + s += '%s\n' % line + return s + def printNumberedTyped(items, maxLen=5000): """print out each item of the list on its own line, with each item numbered on the left from zero"""