[Python-Dev] [Python-checkins] python/dist/src/Modules _csv.c, 1.37, 1.38

Armin Rigo arigo at tunes.org
Wed Jun 15 16:06:35 CEST 2005


Hi Skip,

On Wed, Jun 15, 2005 at 06:35:10AM -0700, montanaro at users.sourceforge.net wrote:
> Why this worked is a bit mystical.  Perhaps it never gets freed because the
> object just happens never to be DECREF'd (but that seems unlikely).
>          /* Add the Dialect type */
> +	Py_INCREF(&Dialect_Type);
>          if (PyModule_AddObject(module, "Dialect", (PyObject *)&Dialect_Type))
>                  return;

Hum, you probably don't want to know, but it works just fine to forget
a Py_INCREF before PyModule_AddObject() for the following reason:

1. the reference is stored in the module's dict, so the object is kept
alive from there.

2. after the module initialization code is completed, the import
mechanism make a copy of the dict (!) just in case some users wants to
reload() the module (!!) in which case the module's dict is simplify
overwritten with the copy again (!!!).

So there is a reference left to the object from this hidden dict, and no
way for the user to kill it -- short of using gc.getreferrers(), which
is how I figured this out, but gc.getreferrers() is officially
dangerous.  So unlike what I thought in my previous e-mail, even if the
user deletes the entry in the module's normal dict, nothing bad can
happend because of this particular feature of import...

So it just works.  Hum.


Armin


More information about the Python-Dev mailing list