Seg fault in Python/C API: PyString_FromString(NULL)

Yves-Eric Martin yemartin at garage.co.jp
Wed Apr 12 09:16:21 CEST 2000


    Hi there,

    While writing a little test unit for a Python gettext
implementation, I ran into a seg fault that I traced to
PyString_FromString being called with a NULL argument.

    First, I am new to the Python/C API, so correct me if I am wrong:
shouldn't PyString_FromString(NULL) return Py_None? Or am I out to
lunch?

    Any information on this segfault is welcome. Here is how I simply
reproduce it: (This was run on Debian GNU/Linux 2.2.14, with gcc
2.95.2 and Python 1.5.2).

---------------- test.c ----------------
#include <stdio.h>
#include "Python.h"

static PyObject*
PyTest_testnull(PyObject* self,PyObject *args)
{
  return PyString_FromString(NULL);
}

static struct PyMethodDef PyTest_Methods[] = {
  {"testnull",(PyCFunction)PyTest_testnull,METH_VARARGS},
  {NULL, NULL}
};

void inittest()
{
  Py_InitModule("test",PyTest_Methods);
}
--------------------------------


---------------- Makefile ----------------
test.so: test.o test.c
        gcc -shared -lc  test.o  -o test.so

test.o: test.c
        gcc -fPIC  -g -O2 -I/usr/include/python1.5 -I/usr/include/python1.5 -DHAVE_CONFIG_H -c ./test.c
--------------------------------


---------------- runtest.py ----------------
import test

print "About to run test..."
test.testnull()
print "If you are reading this, it's working!"
--------------------------------

The result of running "python runtest.py" is:

About to run test...
Segmentation fault



    Thanks.

-- 
Yves-Eric Martin
Digital Garage Inc.
yemartin at garage.co.jp




More information about the Python-list mailing list