Browse Source

improved ~objects

Darren Ranalli 18 years ago
parent
commit
46ea4b8474
2 changed files with 80 additions and 6 deletions
  1. 10 5
      direct/src/showbase/ObjectPool.py
  2. 70 1
      direct/src/showbase/PythonUtil.py

+ 10 - 5
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'

+ 70 - 1
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 = '<SNIP>'
     for i in xrange(len(items)):
         if not first:
             s += '\n'
         first = False
         objStr = fastRepr(items[i])
         if len(objStr) > maxLen:
-            snip = '<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 = '<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 = '<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"""