From 904d6cb12863b4ab26052bcc38d6cedd84bcb6a9 Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Fri, 12 Sep 2008 01:14:37 +0000 Subject: [PATCH] improved readability of GarbageReport Python-syntax section --- direct/src/showbase/GarbageReport.py | 18 +++++++---- direct/src/showbase/PythonUtil.py | 48 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/direct/src/showbase/GarbageReport.py b/direct/src/showbase/GarbageReport.py index 52335c1327..5e5f684fe9 100755 --- a/direct/src/showbase/GarbageReport.py +++ b/direct/src/showbase/GarbageReport.py @@ -4,6 +4,7 @@ __all__ = ['FakeObject', '_createGarbage', 'GarbageReport', 'GarbageLogger'] from direct.directnotify.DirectNotifyGlobal import directNotify from direct.showbase.PythonUtil import gcDebugOn, safeRepr, fastRepr, printListEnumGen, printNumberedTypesGen +from direct.showbase.PythonUtil import AlphabetCounter from direct.showbase.Job import Job import gc import types @@ -152,10 +153,11 @@ class GarbageReport(Job): # if cycle starts off with an instance dict, start with the instance instead startIndex = 0 - endIndex = numObjs - if type(objs[-1]) is types.InstanceType: - startIndex = -1 - endIndex = numObjs - 1 + # + 1 to include a reference back to the first object + endIndex = numObjs + 1 + if type(objs[-1]) is types.InstanceType and type(objs[0]) is types.DictType: + startIndex -= 1 + endIndex -= 1 for index in xrange(startIndex, endIndex): if numToSkip: @@ -207,7 +209,7 @@ class GarbageReport(Job): cycleBySyntax += '%s%s' % (index, brackets[1]) objAlreadyRepresented = True else: - cycleBySyntax += '%s->' % itype(obj) + cycleBySyntax += '%s --> ' % itype(obj) objAlreadyRepresented = False newCyclesBySyntax.append(cycleBySyntax) yield None @@ -276,15 +278,17 @@ class GarbageReport(Job): if self._args.findCycles: s.append('===== Garbage Cycles By Index =====') + ac = AlphabetCounter() for i in xrange(len(self.cycles)): yield None - s.append('%s' % self.cycles[i]) + s.append('%s:%s' % (ac.next(), self.cycles[i])) if self._args.findCycles: s.append('===== Garbage Cycles By Python Syntax =====') + ac = AlphabetCounter() for i in xrange(len(self.cyclesBySyntax)): yield None - s.append('%s' % self.cyclesBySyntax[i]) + s.append('%s:%s' % (ac.next(), self.cyclesBySyntax[i])) if self._args.fullReport: format = '%0' + '%s' % digits + 'i:%s' diff --git a/direct/src/showbase/PythonUtil.py b/direct/src/showbase/PythonUtil.py index 66f46993e9..824ce579f4 100644 --- a/direct/src/showbase/PythonUtil.py +++ b/direct/src/showbase/PythonUtil.py @@ -3452,6 +3452,54 @@ def formatTimeCompact(seconds): result += '%ss' % seconds return result +class AlphabetCounter: + # object that produces 'A', 'B', 'C', ... 'AA', 'AB', etc. + def __init__(self): + self._curCounter = ['A'] + def next(self): + result = ''.join([c for c in self._curCounter]) + index = -1 + while True: + curChar = self._curCounter[index] + if curChar is 'Z': + nextChar = 'A' + carry = True + else: + nextChar = chr(ord(self._curCounter[index])+1) + carry = False + self._curCounter[index] = nextChar + if carry: + if (-index) == len(self._curCounter): + self._curCounter = ['A',] + self._curCounter + break + else: + index -= 1 + carry = False + else: + break + return result + +if __debug__: + def testAlphabetCounter(): + tempList = [] + ac = AlphabetCounter() + for i in xrange(26*3): + tempList.append(ac.next()) + assert tempList == [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ', + 'BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ',] + ac = AlphabetCounter() + num = 26 # A-Z + num += (26*26) # AA-ZZ + num += 26 # AAZ + num += 1 # ABA + num += 2 # ABC + for i in xrange(num): + x = ac.next() + assert x == 'ABC' + testAlphabetCounter() + del testAlphabetCounter + globalPdb = None traceCalled = False