stdout/err and C extentions

hg hg at nospam.org
Tue Jun 12 15:25:52 EDT 2007


Hi,

I have the following

********************* C extention  - redir.c


#include "Python.h"

PyObject * test_redir_test(PyObject *self) {
  fprintf(stdout, "Hello from an extention!\n");
  Py_INCREF(Py_None);
  return Py_None;
}

static PyMethodDef test_redir_methods[] = {
    {"test", (PyCFunction)test_redir_test, METH_NOARGS, "Prints test string
\n"},
    {NULL, NULL, 0, NULL}
};

DL_EXPORT(void) inittest_redir(void)
{
    Py_InitModule3("test_redir", test_redir_methods, "Provides a test
function.\n");   
}

********************* setup.py :

from distutils.core import setup, Extension
import sys
import os 


include = []



setup(name="test_redir", version="0.1",
   ext_modules=[
       Extension(
           "test_redir",
            ["redir.c"],
            undef_macros=['RELEASE'],
            library_dirs=[],
        )
    ]
)

*********************** python test script: test.py:



import sys


class My_Stdout:
    def write(self, p_string):
        l_file = open('res.txt','a')
        l_file.write(p_string)
        l_file.close


sys.stdout = My_Stdout()

print 'toto'
import test_redir



test_redir.test()



**************** Question:


print 'toto' does go to "res.txt" while "Hello from an extention!\n" goes to
the console.




Any clue ?


Thanks,


hg






More information about the Python-list mailing list