[Python-Dev] addressing distutils inability to track file dependencies

Tim Peters tim.one@comcast.net
Fri, 14 Jun 2002 12:41:03 -0400

[Thomas Heller]
> I can (in 2.2) see sys.getobjects() and sys.gettotalrefcount().
> I can also guess what gettotalrefcount does, but what does
> getobjects() do? Is it documented somewhere?

Sorry, I don't think any debug-mode-only gimmicks are documented outside of
comments in the source files.

In a debug build, the PyObject layout changes (btw, that's why you can't mix
debug-build modules w/ release-build modules), adding new _ob_next and
_ob_prev pointers at the start of every PyObject.

The pointers form a doubly-linked list, which contains every live object in
existence, except for those statically allocated (the builtin type objects).
The head of the list is in object.c's static refchain vrbl.

sys.getobjects(n) returns that C list of (almost) all live objects, as a
Python list.  Excluded from the list returned are the list itself, and the
objects created to *call* getobjects().  The list of objects is in
allocation order, most-recently allocated at the start (getobjects()[0]).  n
is the maximum number of objects it will return, where n==0 means (of course
<wink>) infinity.

You can also pass it a type after the int, and, if you do, only objects of
that type get returned.

getobjects() is the tool of last resort when trying to track down an excess
of increfs over decrefs.  Python code that's exceedingly careful to account
for its own effects can figure out anything using it.  I once determined
that the compiler was leaking references to the integer 2 this way <wink>.