Henry Gomersall | 29 Feb 2012 14:30
Favicon
Gravatar

cleaning up in a module

What's the preferred way to clean up a C library when a module is
deleted?

Cheers,

Henry

Stefan Behnel | 29 Feb 2012 14:34
Picon
Favicon

Re: cleaning up in a module

Henry Gomersall, 29.02.2012 14:30:
> What's the preferred way to clean up a C library when a module is
> deleted?

PEP 3121:

http://www.python.org/dev/peps/pep-3121/

However, given that CPython doesn't currently support unloading extension
modules, your question is rather hypothetical.

Stefan

Stefan Behnel | 29 Feb 2012 14:42
Picon
Favicon

Re: cleaning up in a module

Stefan Behnel, 29.02.2012 14:34:
> Henry Gomersall, 29.02.2012 14:30:
>> What's the preferred way to clean up a C library when a module is
>> deleted?
> 
> PEP 3121:
> 
> http://www.python.org/dev/peps/pep-3121/
> 
> However, given that CPython doesn't currently support unloading extension
> modules, your question is rather hypothetical.

Oh, what you *can* do, however, is register an "atexit" function that does
the cleanup when the interpreter terminates. Maybe that's what you are
looking for?

Stefan

Henry Gomersall | 29 Feb 2012 14:51
Favicon
Gravatar

Re: cleaning up in a module

On Wed, 2012-02-29 at 14:42 +0100, Stefan Behnel wrote:
> > http://www.python.org/dev/peps/pep-3121/
> > 
> > However, given that CPython doesn't currently support unloading
> extension
> > modules, your question is rather hypothetical.
> 
> Oh, what you *can* do, however, is register an "atexit" function that
> does
> the cleanup when the interpreter terminates. Maybe that's what you are
> looking for? 

What I was *going* to say when I started writing this email is as
follows:
"The question really is regarding external libraries that don't like
being initialised twice.

The library can be initialised by running the code in the main body of
the module, and then new instances of classes can assume it has been
initialised. The problem comes when the module is deleted"

Anyway, at that point I went off to run some tests. It seems that the
code in the main body of the module is only ever run once, no matter how
often the code is imported and deleted. Now I'm confused as to what del
actually means on a module.

On the flip side, I suppose this means that I can happily initialise a C
library in the body of the module and assume it's only going to happen
once (though no doubt I'm going to be told of some corner case!).

(Continue reading)

Stefan Behnel | 29 Feb 2012 15:28
Picon
Favicon

Re: cleaning up in a module

Henry Gomersall, 29.02.2012 14:51:
> On Wed, 2012-02-29 at 14:42 +0100, Stefan Behnel wrote:
>>> http://www.python.org/dev/peps/pep-3121/
>>>
>>> However, given that CPython doesn't currently support unloading
>> extension
>>> modules, your question is rather hypothetical.
>>
>> Oh, what you *can* do, however, is register an "atexit" function that
>> does
>> the cleanup when the interpreter terminates. Maybe that's what you are
>> looking for? 
> 
> What I was *going* to say when I started writing this email is as
> follows:
> "The question really is regarding external libraries that don't like
> being initialised twice.

Then PEP 3121 is for you.

> The library can be initialised by running the code in the main body of
> the module, and then new instances of classes can assume it has been
> initialised.

That's the usual way of doing it, yes.

> The problem comes when the module is deleted"

Which isn't currently supported at all.

(Continue reading)

Henry Gomersall | 29 Feb 2012 15:31
Favicon
Gravatar

Re: cleaning up in a module

On Wed, 2012-02-29 at 15:28 +0100, Stefan Behnel wrote:
> If Cython ever starts supporting module reinitialisation, we'll have
> to
> find a way to transition existing code. However, as long as CPython
> doesn't
> provide any actual benefit for doing this, I doubt that it'll happen.
> Until
> then,  the global module code is executed as part of the import, which
> only
> happens once. 

Thanks for the very helpful reply!

Henry

Stefan Behnel | 29 Feb 2012 15:41
Picon
Favicon

Re: [Cython] [cython-users] cleaning up in a module

Stefan Behnel, 29.02.2012 14:42:
> Stefan Behnel, 29.02.2012 14:34:
>> Henry Gomersall, 29.02.2012 14:30:
>>> What's the preferred way to clean up a C library when a module is
>>> deleted?
>>
>> PEP 3121:
>>
>> http://www.python.org/dev/peps/pep-3121/
>>
>> However, given that CPython doesn't currently support unloading extension
>> modules, your question is rather hypothetical.
> 
> Oh, what you *can* do, however, is register an "atexit" function that does
> the cleanup when the interpreter terminates.

Speaking of which, what about allowing users to implement a function

cdef void __dealloc__()

at the module level, which would then be called by the module cleanup
function (if generated) before running the rest of the cleanup code?

That would allow for an easier transition of user code towards a better
support of PEP 3121, specifically m_clear().

http://trac.cython.org/cython_trac/ticket/218

Stefan
(Continue reading)


Gmane