Debug: Module to track python objects.

R Wentworth rhww at erols.com
Thu Jun 10 09:19:12 EDT 1999


> I'm open to comments, include those designed to make it more effecient,
> powerful or easier to use.

David,

One thought on ease of use... you could easily set things up so that
classes did not need to initially be defined with your Debug package
in mind.  This might work as follows (this hasn't been debugged):

class "Debug":
	class WrapMethod:
		"Helper class to wrap a class method"
		def __init__(self,appendfunc, method=None):
			self.appendfunc = appendfunc
			self.method = method
		def __call__(self, *largs, **kargs):
			if self.method:
				apply(self.method, (self,)+largs, kargs)
			if __debug__:
				apply(appendfunc, (self,))
	def DebugClass(self, Class):
		"Add debugging functionality to class Class"
		if hasattr(Class, "__del__"):
			Class.__del__ = self.WrapMethod(self.LogDel, Class.__del__)
		else:
			Class.__del__ = self.WrapMethod(self.LogDel)
		if hasattr(Class, "__init__"):
			Class.__init__ = self.WrapMethod(self.LogInit, Class.__init__)
		else:
			Class.__init__ = self.WrapMethod(self.LogInit)
		if hasattr(Class, "__destroy__"):
			Class.__destroy__ = self.WrapMethod(self.LogDestroy, 
				Class.__destroy__)
		else:
			Class.__destroy__ = self.WrapMethod(self.LogDestroy)

	...

class MyClass:
	...

debug=Debug()
debug.DebugClass(MyClass)

In addition, you could write a "DebugModule()" method which, given
a module, would find all the class definitions and apply DebugClass()
to each.
name?

Finally, the name "Debug" is a bit generic for a module.
Maybe something like "ObjLifeDebug"?

FWIW,
Bob Wentworth

"Stidolph, David" wrote:
> 
> First and foremost I want to thank Guido for the traceback system - just
> having made this module possible and my task MUCH easier.
> 
> This is an update to a debugging module that I posted before.  See the end
> of this mail for the module.
> 
> Purpose:  The Debug module tracks python class instances between their
> __init__ and __del__ method calls.  The idea is to provide tracking for
> objects so that a report can be generated with objects that have not been
> freed.  When we first ran this on our code we found almost 300 objects that
> were not being destroyed (more than half were windows resources like graphic
> texture files).
> 
> Asumptions:  The __init__ method of every class calls LogInit method of the
> debug class.  The __del__ method calls LogDel.  We have a special method
> called Destroy for every class that is designed to remove references to
> other objects and handle the circular reference problem.  The Destroy method
> calls LogDel
> 
> Useage (we have a Globals module with specific instance variables to grant
> access to modules like Debug):
> 
>         #sample
>         import Globals
> 
>         class Name:
>           def __del__(self):
>             if __debug__:
>               Globals.debug.LogDel(self)
> 
>           def __init__(self):
>             if __debug__:
>               Globals.debug.LogInit(self)
>             # your code
> 
>           def Destroy(self):
>             if __debug__:
>               Globals.debug.LogDestroy(self)
>             # your code
> 
> Questions:
> 
> 1. We are losing the tracebacks.  At some point the output terminates.  This
> happens after a large output, so I suspect a buffer overrun.
> 2. Can we shrink the tracebacks?  Keeping all the strings while the program
> runs seems a little much.  We also want to modify the output for MSVC.
> 3. Is there any other tracking we can do for catching bugs?  Ways to enhance
> this module?
> 
> 
> Thank you for your help,
> 
> David Stidolph
> Origin.

[detailed code omitted]




More information about the Python-list mailing list