PyObject *data - access to raw data?

Donn Cave donn at u.washington.edu
Mon Aug 7 18:56:03 EDT 2000


Quoth Thomas Gagne <tgagne at ix.netcom.com>:
| To learn python I decided to try creating a Python interface to my middleware
| library, called isdio.  Before I get much passed the login/logout stuff
| (though it is working) I'm curious:
|
| The routines for sending and receiving data assume the programmer knows what
| it is they want to send and what it is they are receiving.  Is there a way for
| me to access PyObject data in a raw format, along with it's length, so that if
| the programmer is passing a string of 20 characters I get a void * to the 20
| characters AND can find out the length is 20 so I don't write too many bytes
| in the network packet?

Yes, for string of course you may do this, for example with '#' in the
format.  When you know the size of the data and there might be 0 value
characters in there, it's obviously a good idea too.

Anything else you want to send, the basic idea is that you should
call on it to represent itself as text.  This marshalling business
is potentially tricky, and there is already lots of good work done
so you don't have to go there yourself.  I would say look at "struct"
first, which works with plain data but is limited to machine data
types, and pickle, which handles complex objects and includes its
own unpacking instructions in the data.  Whether you use an existing
marshalling function or not, your approach will end up looking at
least superficially similar.

| Is there a way I can coerce an arbitrary number of bytes in to PyObject
| without knowing what kind of PyObject it is that's receiving it?  Say the
| programmer knows they're getting a string, how can I return a string object if
| all I know about in the receiving function is
| a) the bytes I read
| b) the number of bytes I read
| c) a PyObject (I suppose) from PyArg_ParseTuple().
|
| If I need to replace the value (c) contains, can I do this from within the C
| function?

You don't want to do that, if I understand you right.  Objects should
be returned literally as the function return value, tuple of objects if
required.  A function may also modify the contents of a list or dictionary,
but that would be pretty unusual for a library routine.  Basically you
can't know what the programmer expects to get back.

| Theoretically, there will be a python module with python code wrapping the C
| functions in isdio which may make it easier..

Good, that's sure where I would tackle this stuff.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list