By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

Using printf in a C Extension

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

<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

P: n/a
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

P: n/a
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.