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

Yves-Eric Martin yemartin at
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

    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[] = {

void inittest()

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

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

---------------- ----------------
import test

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

The result of running "python" is:

About to run test...
Segmentation fault


Yves-Eric Martin
Digital Garage Inc.
yemartin at

More information about the Python-list mailing list