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


Groups > comp.lang.python > #18999 > unrolled thread

Extension module question

Started byEvan Driscoll <edriscoll@wisc.edu>
First post2012-01-15 01:37 -0600
Last post2012-01-15 01:37 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

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

#18999 — Extension module question

FromEvan Driscoll <edriscoll@wisc.edu>
Date2012-01-15 01:37 -0600
SubjectExtension module question
Message-ID<mailman.4762.1326613079.27778.python-list@python.org>

[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


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web