[Python-3000] PEP 3121: Module Initialization and finalization

"Martin v. Löwis" martin at v.loewis.de
Sun Apr 29 20:15:05 CEST 2007

>> >  if (!module)
>> >    return NULL;
>> >  PyObject* xx_error = xxstate(module)->ErrorObject;
>> >  if (!xx_error) {
>> >    PyErr_SetString(PyExc_SystemError, "xx.error missing");
>> >    return NULL;
>> >  }
>> That should not fail. If the module object is still present,
>> the state shouldn't be cleared normally. So this might
>> only occur if the code above is invoked in the "clear cycles"
>> phase of garbage collection.
> Maybe it shouldn't, but can it?  Is there something preventing the
> exception object from being cleared beyond people not being stupid?

I believe it would always be possible to trigger a problem, no matter
what we do: if you key invocation of the function to deletion of
the exception class (which is always possible through weak references),
then you can trigger a problem indeed.

I also now think there can't be a solution: E.g. you may associate
reference the exception from the method object itself, so that you
the exception "should" be alive while the function is still available.
However, breaking cycles may decide to tp_clear that reference, making
that approach futile.

In some cases, it may be possible to statically determine that clearing
certain reference will never be necessary to break all cycles (because
every such cycle would be broken at a different reference also),
however, that is difficult to generalize, and prone to errors that
result in uncollectable garbage.


More information about the Python-3000 mailing list