On 10/20/07, Facundo Batista <[EMAIL PROTECTED]> wrote:
> 2007/10/19, Christian Heimes <[EMAIL PROTECTED]>:
>
> > I know a possible solution. You could write a patch that moves the
> > imports in C code to the module init function and stores the modules in
> > a global static variable.
>
> I thought about this. It even will have the good side efect of not
> shooting the whole "import" machinery to see that you already imported
> it, every time you do an strptime.
>
> One question: The program makes this:
>
>     PyObject *strptime_module = PyImport_ImportModule("_strptime");
>     ...
>     Py_DECREF(strptime_module);
>
> If I'd import it in the beggining of the file with the following...
>
>     static PyObject *strptime_module = PyImport_ImportModule("_strptime");
>
> ... I'd never decref it, right?

Right.  Otherwise, if the module is removed from sys.modules then it
will have a refcount of 0 and be collected, leaving your static
variable holding junk memory.

One issue with this approach, though, is that the import is a one-time
thing, and so replacing what is in sys.modules['_strptime'] will not
take affect in the module ever thanks to the caching of the module's
dict.

Granted this is a small issue as normal Python code does not pick up
changes in sys.modules, but it is something to be aware of.

-Brett
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to