Using printf in a C Extension

U

user

Hi,


I am extending python with C and trying to debug with printf. The code
below succssfully returns the string "hello" when compiled and called,
but the "can print from in here phrase" does not reach python stdout.
Is there something screwy with my environment or is there some trick to
this that I don't know. Any help would be greatly appreciated!


-mike


#include <Python.h>


static PyObject*
helloworld(PyObject* self)
{
printf("can print from in here?");
return Py_BuildValue("s", "hello");
}


static char hw_docstr[] = "hello docstr";


static PyMethodDef hw_funcs[] = {
{"helloworld", (PyCFunction)helloworld, METH_NOARGS, hw_docstr},
{NULL}
};


void
inithelloworld(void)
{
Py_InitModule3("helloworld",hw_funcs, "simple module");
}
 
S

Scott David Daniels

I am extending python with C and trying to debug with printf. The code
below succssfully returns the string "hello" when compiled and called,
but the "can print from in here phrase" does not reach python stdout. Is
there something screwy with my environment or is there some trick to
this that I don't know. Any help would be greatly appreciated!

printf does not magically go to Python stdout (which it knows nothing
about. printf goes to stdout. Perhaps if you end your print string
with "\n", you may see the output on a terminal display (some stdout
C systems are line buffered).

--Scott David Daniels
(e-mail address removed)
 
F

Frithiof Andreas Jensen

Hi,


I am extending python with C and trying to debug with printf. The code
below succssfully returns the string "hello" when compiled and called,
but the "can print from in here phrase" does not reach python stdout.

It should go to stdout - maybe stdout does not flush. Maybe you need '\n' to
make it flush. Maybe your code broke printf.
Is there something screwy with my environment or is there some trick to
this that I don't know. Any help would be greatly appreciated!

*printf* itself is screwy, much too clever: an interpreter messing with
nakkid pointers on top of varargs!! Bound to blow up if the bug you are
trying to find messes with any of the intricate machinery needed by printf,
such as the stack.

Try to locate the appropriate "debug version"s of printf available for your
platform. Usually there will be several that have the name "print" in them,
like "printmsg", "printerr", "printk" and so forth. They may even be macros
that are #ifdef'ed in/out with "debug" compile options - so you might need
to switch them on.

The point of the debug variants is that they always flush to stdout/stderr -
even when those are redirected - and they are designed to work even in
obscure circumstances such as in the middle of an exception. The price is
usually the loss of the printf formatting that nobody uses anyway (about 80%
of the options ;-) and no varargs.
 
J

Just

I am extending python with C and trying to debug with printf. The code
below succssfully returns the string "hello" when compiled and called,
but the "can print from in here phrase" does not reach python stdout.
Is there something screwy with my environment or is there some trick to
this that I don't know. Any help would be greatly appreciated!

Have a look at PySys_WriteStdout().

Just
 
L

Lonnie Princehouse

printf will generally work in C extensions (although, as others have
said, it goes to STDOUT which is not necessarily the same as Python
sys.stdout)

Try explicitly flushing the buffer with fflush(stdout)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top