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


Groups > comp.lang.python > #18999

Extension module question

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <edriscoll@wisc.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'subject:module': 0.04; 'attribute': 0.07; 'something,': 0.07; 'const': 0.09; 'content-type:multipart/signed': 0.09; 'filename:fname piece:signature': 0.09; 'pointers': 0.09; 'unsigned': 0.09; 'this:': 0.15; '*c*': 0.16; 'assertion': 0.16; 'build.': 0.16; 'content-type:application/pgp-signature': 0.16; 'dirname': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname:signature.asc': 0.16; 'pyobject': 0.16; 'pyobject*': 0.16; 'self,': 0.16; 'subject:question': 0.16; 'wrap': 0.18; 'extension': 0.21; 'calls.': 0.23; 'way?': 0.23; 'hopefully': 0.24; 'static': 0.24; 'module': 0.26; 'stuff': 0.26; "i'm": 0.27; 'earlier': 0.28; 'pass': 0.28; 'module.': 0.28; 'class': 0.29; '(and': 0.29; '(so': 0.30; 'args)': 0.30; 'null;': 0.30; 'pretty': 0.30; "i've": 0.31; 'skip:( 20': 0.31; 'idea': 0.32; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.33; 'calling': 0.34; 'something': 0.35; 'but': 0.37; 'received:128': 0.37; 'received:192': 0.38; 'doing': 0.39; 'missing': 0.39; 'might': 0.40; 'to:addr:python.org': 0.40; 'got': 0.40; 'custom': 0.61; 'here': 0.64; 'bridge': 0.80; 'canonical': 0.91
Date Sun, 15 Jan 2012 01:37:22 -0600
From Evan Driscoll <edriscoll@wisc.edu>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1
MIME-Version 1.0
To python-list@python.org
Subject Extension module question
X-Enigmail-Version 1.3.4
Content-Type multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig92B47A97224A2B6C2FC03161"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4762.1326613079.27778.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326613079 news.xs4all.nl 6979 [2001:888:2000:d::a6]:42731
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18999

Show key headers only | 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