469,289 Members | 2,188 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Using printf in a C Extension

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");
}
Dec 9 '05 #1
4 5072
us**@domain.invalid wrote:
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
sc***********@acm.org
Dec 9 '05 #2

<us**@domain.invalid> wrote in message news:43********@news3.es.net...
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.

Dec 9 '05 #3
In article <43********@news3.es.net>, us**@domain.invalid wrote:
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
Dec 9 '05 #4
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)

Dec 9 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Prashanth Badabagni | last post: by
12 posts views Thread by John Devereux | last post: by
27 posts views Thread by jacob navia | last post: by
5 posts views Thread by Steven Woody | last post: by
5 posts views Thread by Chris McDonald | last post: by
5 posts views Thread by ritesh | last post: by
7 posts views Thread by smartbeginner | last post: by
1 post views Thread by aarklon | last post: by
15 posts views Thread by David Marsh | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.