Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18999

Extension module question

Date 2012-01-15 01:37 -0600
From Evan Driscoll <edriscoll@wisc.edu>
Subject Extension module question
Newsgroups comp.lang.python
Message-ID <mailman.4762.1326613079.27778.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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 =
opendir(dirname);                                                    

        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?

Evan


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Extension module question Evan Driscoll <edriscoll@wisc.edu> - 2012-01-15 01:37 -0600

csiph-web