Analyzing Python GC output - turns out to be MySQLdb problem

John Nagle nagle at animats.com
Fri Jan 11 14:41:41 EST 2008


Francesco Guerrieri wrote:
> On Jan 11, 2008 6:20 PM, John Nagle <nagle at animats.com> wrote:
>> Tried:
>>         print item.dir()
>> got:
>>         'cell' object has no attribute 'dir'

    It's a problem inside MySQLdb's "connections.py":

         def _get_unicode_literal():
             def unicode_literal(u, dummy=None):
                 return db.literal(u.encode(unicode_literal.charset))
             return unicode_literal

Each time a MySQLdb Connection object is created, it generates a closure,
with another instance of the function "unicode_literal" plus some other
junk.  That generates circular references.  Eventually those
things get garbage-collected, but if you're running GC in leak detection
mode, they show up.

The reason for the closure is that "unicode_literal.charset" of the
function is being stored into from outside the function.  So there
actually is a reason to use a closure.

				John Nagle



More information about the Python-list mailing list