Extension module question

Evan Driscoll edriscoll at wisc.edu
Sun Jan 15 02:37:22 EST 2012

As I hinted at in an earlier email, I'm working on a module which will
allow calling readdir() (and FindFirstFile on Windows, hopefully pretty
uniformly) from Python. The responses I got convinced me that it was a
good idea to write a C-to-Python bridge as an extension module.

What I'm not sure about is how to store pointers to *C* stuff between
calls. In particular, opendir() returns a DIR* which you then need to
pass to calls to readdir() in the future (and closedir()).

So I've got this:

    static PyObject*
    py_opendir(PyObject* self, PyObject* args)
        const char* dirname = 0;
        if (!PyArg_ParseTuple(args, "s", &dirname)) {
            return NULL;
        // Eventually want to Py_BEGIN_ALLOW_THREADS here
        DIR* directory =

        PyObject out = PyBuildValue( ???, directory );
        return out;

but I don't know what to build. (I might want to wrap it in a custom
handle class or something, but I still need to know how to build the
value I eventually store in an attribute of that class.)

My best idea is to use an unsigned long (so "k") and add a static
assertion that sizeof(long)==sizeof(void*). Is this the canonical way of
doing something like this, or am I missing a better way?


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 552 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20120115/27ef7baf/attachment.sig>

More information about the Python-list mailing list