E
Evan Driscoll
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 wayof
doing something like this, or am I missing a better way?
Evan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJPEoI5AAoJEAOzoR8eZTzguAoH/2KUxzE5mruwRrZOelKRjL7e
1HXXx9NP7PwhSzzSBwcBsgrgwyRexAOHRS9VPwrVghYv1m/WKsbBto71bFf+Y/5I
5+DU1m58qhYBssqGSH75HG7XzetF7zy/HHoMcjO+v7D50YQxsBBhNzw3Hb8Y8Bfy
MZYbjiiVwCWiFjL9K8QhfiDxnnV1oXpv19u7Az4W5M//+iYWD7WR6swnz4I+DGH/
zO/ITIR0O0GWdCPw7/HGF4Hy3u8qtX6sL6Al5OCGos/41Om51mK/Uj7APE89aBBL
IKmoJiGD6saGMR5tasA5IL7w+3Bo9CHg+KXa2HCNNzLxO7DfBPRpo3uBm9amGSg=
=Y/HB
-----END PGP SIGNATURE-----
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 wayof
doing something like this, or am I missing a better way?
Evan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJPEoI5AAoJEAOzoR8eZTzguAoH/2KUxzE5mruwRrZOelKRjL7e
1HXXx9NP7PwhSzzSBwcBsgrgwyRexAOHRS9VPwrVghYv1m/WKsbBto71bFf+Y/5I
5+DU1m58qhYBssqGSH75HG7XzetF7zy/HHoMcjO+v7D50YQxsBBhNzw3Hb8Y8Bfy
MZYbjiiVwCWiFjL9K8QhfiDxnnV1oXpv19u7Az4W5M//+iYWD7WR6swnz4I+DGH/
zO/ITIR0O0GWdCPw7/HGF4Hy3u8qtX6sL6Al5OCGos/41Om51mK/Uj7APE89aBBL
IKmoJiGD6saGMR5tasA5IL7w+3Bo9CHg+KXa2HCNNzLxO7DfBPRpo3uBm9amGSg=
=Y/HB
-----END PGP SIGNATURE-----