PyObject *data - access to raw data?
Thomas Gagne
tgagne at ix.netcom.com
Mon Aug 7 16:21:30 EDT 2000
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?
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?
Theoretically, there will be a python module with python code wrapping the C
functions in isdio which may make it easier..
For the fun of it, I've attached my first module, isdio.c
-------------- next part --------------
#include "isdio.h"
#include "python1.5/Python.h"
static PyObject *login(PyObject *self, PyObject *args)
{
char *hostname, *service;
int priority;
int socket;
if (!PyArg_ParseTuple(args, "zzi", &hostname, &service, &priority)) {
return NULL;
}
socket = isdLogin(hostname, service, priority);
return Py_BuildValue("i", socket);
}
static PyObject *logout(PyObject *self, PyObject *args)
{
int socket;
if (!PyArg_ParseTuple(args, "i", &socket)) {
return NULL;
}
isdLogout(socket);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef isdio_methods[] = {
{ "login", login, 1, "int socket = isdLogin(string hostname, string servicename, int priority)" },
{ "logout", logout, 1, "int result = isdLogout(int socket)" },
{ NULL, NULL }
};
void initisdio(void)
{
Py_InitModule("isdio", isdio_methods);
}
More information about the Python-list
mailing list