Setting a C-structure from Python

Bernhard Herzog herzog at online.de
Mon Jul 10 18:40:57 EDT 2000


Armin Steinhoff <Armin at Steinhoff_de> writes:

> In article <3967F70C.17A90288 at seatech.fau.edu>, Benoit says...
> >
> >I think I find out where the bug was.
> >Thanks Armin !
> >
> >Why this C extension didn't work ?
> >
> >>
> >>  static PyObject * test_setstruct(PyObject * self, PyObject * args) {
> >>  PyObject * string_from_python;
> >>
> >>  if (!PyArg_ParseTuple(args,"S",&string_from_python)) return NULL;
> >>
> >>  example= (examplestruct *) PyString_AsString(string_from_python);
> >> Py_DECREF(string_from_python);

This Py_DECREF is the culprit. example is only a borrowed reference to
the string object and you don#t have to DECREF it yourself.

> >>  print_struct();
> >>
> >>  Py_INCREF(Py_None);
> >>  return Py_None;
> >>  }
> >
[...]

> I believe the real point is that you can't apply the "S" format to a "string
> buffer" which contains binary zeros. IMHO it corrupts the interpreter.

No, Python's string objects handle embedded NULs just fine. 


-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list