improved readability of GarbageReport Python-syntax section

This commit is contained in:
Darren Ranalli 2008-09-12 01:14:37 +00:00
parent 6fe61ef5c1
commit 904d6cb128
2 changed files with 59 additions and 7 deletions

View File

@ -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'

View File

@ -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