[Python-Dev] buffer interface for C extensions (original) (raw)
M.-A. Lemburg mal at egenix.com
Thu May 22 23:10:09 CEST 2008
- Previous message: [Python-Dev] buffer interface for C extensions
- Next message: [Python-Dev] buffer interface for C extensions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2008-05-19 00:59, Dan Lenski wrote:
Hi all,
I've written a small C extension to submit commands to SCSI devices via Linux's sgio driver (for a camera hacking project). The extension is just a wrapper around a couple ioctl()'s with Pythonic exception handling thrown in. One of my extension methods is called like this from python: sg.write(fd, command[, data, timeout) Both command and data are binary strings. I would like to be able to use either a regular Python string or an array('B', ...) for these read-only arguments. So I tried to use the "t#" argument specifier to indicate that these arguments could be either strings or objects that implement the read- only buffer interface: if (!PyArgParseTuple(args, "it#|t#i:write", &sgfd, &cmd, &cmdLen, &buf, &bufLen, &timeout)) return NULL; Now, this works fine with strings, but when I call it with an array I get a TypeError: TypeError: write() argument 2 must be string or read-only character buffer, not array.array So, I then tried changing "t#" to "w#" to indicate that the arguments must implement the /read-write/ buffer interface. Now the array objects work, but when I try a string argument, I naturally get this error: TypeError: Cannot use string as modifiable buffer So here's what I don't understand. Why doesn't the "t#" argument specifier support read-write buffers as well as read-only buffers? Aren't read-write buffers a superset of read-only buffers?? Is there something I'm doing wrong or a quick fix to get this to work appropriately?
You should probably ask such questions on the capi-sig list.
To answer your question:
t# requires support for the read-only 8-bit character buffer interface s# can use the read buffer interface w# requires support for the write buffer interface
Those are two different buffer interface slots, so whether a particular object works with t# or w# depends on whether it implements the slots in question.
You should probably try s#, as this will also work with objects that implement the getreadbuffer slot.
The details can be found in Python/getargs.c
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, May 22 2008)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
- Previous message: [Python-Dev] buffer interface for C extensions
- Next message: [Python-Dev] buffer interface for C extensions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]