Python extensions in C... and stdout problems!

Victor B. Putz vputz at nyx.net
Sun Aug 13 21:17:25 EDT 2000


Ran into an irritating problem with gcc 2.95, Python 1.5.2, and building a
shared-module extension module in C.

The essence of the problem is that any C function which references stdout (and I
assume stdin or stderr) crashes the program and interpreter immediately. 
Relating this to the "spammodule" example for building a shared module, here's
the module source:

#include <Python.h>

static PyObject *
spam_system( PyObject* self, PyObject* args ) 
{
  char* command;
  int status;

  if ( !PyArg_ParseTuple( args, "s", &command ) ) 
    {
      return NULL;
    }
  /*  fputc( 'c', stdout );*/
  status = system( command );
  return Py_BuildValue( "i", status );
}
 
static PyMethodDef SpamMethods[] = {
  { "system", spam_system, METH_VARARGS },
  { NULL, NULL }
};

void
initspam()
{
  (void) Py_InitModule( "spam", SpamMethods );
}


And now the python "test program": 

import spam
spam.system( "echo 'hello, world!'" )


note the commented-out "fputc( 'c', stdout)" in spam_system: if uncommented, it
causes a core dump immediately, no questions asked or message given.  Since I'm
very much trying to interface Python with a C library that uses stdout
extensively, I'm at something of a loss.

I'm using the distributed "Makefile.pre.in" to make the module, and without the
reference to stdout, it works like a champ--with it, it's a no go.  I can't
figure out if Python isn't initializing the stdout family on startup, if there's
something wrong with the way the shared module is being created, or what.  If
anyone has ANY ideas, I'd really appreciate some help.

Thanks-->VPutz




More information about the Python-list mailing list