[Python-Dev] Strange artifacts with PEP 3121 and monkey-patching sys.modules (in csv, ElementTree and others)

Antoine Pitrou solipsis at pitrou.net
Sun Aug 11 13:48:26 CEST 2013

On Sun, 11 Aug 2013 07:04:40 -0400
Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 11 August 2013 06:33, Antoine Pitrou <solipsis at pitrou.net> wrote:
> > So code can be written like:
> >
> >   PyObject *dialects = PyState_GetModuleAttr(
> >       &_csvmodule, "dialects", &PyDict_Type);
> >   if (dialects == NULL)
> >       return NULL;
> This sounds like a good near term solution to me.
> Longer term, I think there may be value in providing a richer
> extension module initialisation API that lets extension modules be
> represented as module *subclasses* in sys.modules, since that would
> get us to a position where it is possible to have *multiple* instances
> of an extension module in the *same* subinterpreter by holding on to
> external references after removing them from sys.modules (which is
> what we do in the test suite for pure Python modules).

Either that, or add a "struct PyMemberDef *m_members" field to
PyModuleDef, to enable looking up stuff in the m_state using regular
attribute lookup.

Unfortunately, doing so would probably break the ABI. Also, allowing
for module subclasses is probably more flexible in the long term. We
just need to devise a convenience API for that (perhaps by allowing to
create both the subclass *and* instantiate it in a single call).

> However, that's dependent on the ModuleSpec idea we're
> currently thrashing out on import-sig (and should be able to bring to
> python-dev soon), and I think getting that integrated at all will be
> ambitious enough for 3.4 - using it to improve extension module
> handling would then be a project for 3.5.

Sounds reasonable.



More information about the Python-Dev mailing list