Where & how to deallocate resources in Python C extension

Mark Wooding mdw at distorted.org.uk
Wed Feb 4 15:49:23 EST 2009

fredbasset1000 at gmail.com writes:

> I've written a C extension, see code below, to provide a Python
> interface to a hardware watchdog timer.  As part of the initialization
> it makes some calls to mmap, I am wondering should I be making
> balanced calls to munmap in some kind of de-init function?

The kernel should remove the mapping when your process exits anyway --
otherwise the world would be left in an inconsistent state if your
process got killed by SIGKILL for example.

> Do Python extensions have d'tors?

No.  But you can cheat: stash some object whose tp_del slot does your
cleanup in your extension's module under some funny name.  (And woe
betide anyone who dels the funny name prematurely!)  I'm not sure I'd

-- [mdw]

More information about the Python-list mailing list