468,107 Members | 1,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

reading a python file object in c extension crashes python


I have an extension in which a file object is created in python and
passed down to a c extension which attempts to read from it or write
to it. Writing to the file pointer seems to work okay, but reading
from it results in EBADF. It also causes python to crash on exit.

I've attached the minimal (I think) c code, python code, build script,
build log, and run log. Any and all help is greatly appreciated.


System: Win-XP Professional
Compiler: Vc7 (MSVS 2003)
Python: Python 2.4.1
Builder: SCons 0.96.1

P.S. Sorry for including everything in one message. I keep getting
bounced when I attach them.
### file:my_module.c -------------------------------------------------

#include <Python.h>
#define MAXLEN 128

my_read(char *buffer, int max, FILE *fp)
int n = 0;
fprintf (stdout, "[c] pre-read: errno=%d, fp=%x\n", errno, fp);
n = fread(buffer, 1, max, fp);
fprintf (stdout, "[c] post-write: errno=%d, fp=%x, count=%d,
data='%s'\n", errno, fp, n, buffer);
return n;

my_write(const char *buffer, FILE *fp)
int n = 0;
fprintf (stdout, "[c] pre-write: errno=%d, fp=%x, data='%s'\n",
errno, fp, buffer);
n = fprintf(fp, "%s", buffer);
fprintf (stdout, "[c] post-write: errno=%d, fp=%x, count=%d\n",
errno, fp, n);
return n;

static PyObject *
my_module_my_read(PyObject *self, PyObject *args)
char buffer[MAXLEN];
int count = 0;
FILE *fp = NULL;
PyObject* py_file = NULL;


if (!PyArg_ParseTuple(args, "O", &py_file))
return NULL;

if (PyFile_Check(py_file))

count = my_read((char*)&(buffer), MAXLEN, PyFile_AsFile(py_file));
return Py_BuildValue("(is)", count, (char*)buffer);
return NULL;

static PyObject *
my_module_my_write(PyObject *self, PyObject *args)
char buffer[MAXLEN];
int count = 0;
FILE *fp = NULL;
PyObject* py_file = NULL;
const char* py_string;


if (!PyArg_ParseTuple(args, "sO", &py_string, &py_file))
return NULL;

if ((PyFile_Check(py_file)) && (py_string != NULL))
count = my_write(py_string, PyFile_AsFile(py_file));
return Py_BuildValue("i", count);
return NULL;

static PyMethodDef MyModuleMethods[] = {
{"my_read", my_module_my_read, METH_VARARGS,
"read at most 128 bytes from a file"},
{"my_write", my_module_my_write, METH_VARARGS,
"write a string to a file"},

PyObject *m;
m = Py_InitModule("my_module", MyModuleMethods);
### file: my_module.py ------------------------------------------------

import my_module

filename = "hello.txt"
fp1 = open(filename, "wb")
print "[py] pre-write: fp=%s" % (fp1)
result1 = my_module.my_write("Hello, World!", fp1)
print "[py] result=", result1

fp2 = open(filename, "rb")
print "[py] pre-read: fp=%s" % (fp2)
result2 = my_module.my_read(fp2)
print "[py] result=", result2
# crashes python here EBADF when fp2.close() is called
print "done"
# crashes python here when fp2.close() is commented out

### file: Sconstruct --------------------------------------------------
env = Environment()
import os

python_include_path = os.path.join(os.path.sep,'Python24','include')
python_lib_path = os.path.join(os.path.sep,'Python24','libs')
python_libs = 'python24'
my_module_name = 'my_module'
my_module_sources = 'my_module.c'

library = env.SharedLibrary(my_module_name,
### file: build.log ---------------------------------------------------
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
cl /nologo /I\Python24\include /c my_module.c /Fomy_module.obj
link /nologo /dll /out:my_module.dll /implib:my_module.lib
/LIBPATH:\Python24\libs python24.lib my_module.obj
Creating library my_module.lib and object my_module.exp
scons: done building targets.
### file: run.log -----------------------------------------------------
[py] pre-write: fp=<open file 'hello.txt', mode 'wb' at 0x009B54A0>
[c] pre-write: errno=0, fp=7c38b548, data='Hello, World!'
[c] post-write: errno=0, fp=7c38b548, count=13
[py] result= 13
[py] pre-read: fp=<open file 'hello.txt', mode 'rb' at 0x009B5608>
[c] pre-read: errno=0, fp=7c38b548
[c] post-write: errno=9, fp=7c38b548, count=0, data=''
[py] result= (0, '')
Jul 19 '05 #1
0 1531

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by youngdubliner | last post: by
3 posts views Thread by Matthias Baas | last post: by
13 posts views Thread by mloichate | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
2 posts views Thread by oyster | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.