473,799 Members | 3,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(PyOb ject* self)
{
printf("can print from in here?");
return Py_BuildValue(" s", "hello");
}
static char hw_docstr[] = "hello docstr";
static PyMethodDef hw_funcs[] = {
{"helloworld ", (PyCFunction)he lloworld, METH_NOARGS, hw_docstr},
{NULL}
};
void
inithelloworld( void)
{
Py_InitModule3( "helloworld",hw _funcs, "simple module");
}
Dec 9 '05 #1
4 6150
us**@domain.inv alid 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***********@a cm.org
Dec 9 '05 #2

<us**@domain.in valid> 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********@new s3.es.net>, us**@domain.inv alid 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_WriteStdo ut().

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
9902
by: Prashanth Badabagni | last post by:
Hi, Can any body tell me how to print "hello,world" with out using semicolon Thanks in advance .. Bye Prashanth Badabagni
12
7633
by: John Devereux | last post by:
Hi, I would like to know if their is a conversion specifier, or other method, that will allow me to not use particular arguments to printf. Failing that, is it permissable to simply "not use" the parameter if it is at the end of the parameter list? For example,
27
2982
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some extensions but I do not recall exactly if they were meant for arrays. Thanks
5
13936
by: Steven Woody | last post by:
hi, i get a struct as below, typedef struct { uint16_t id; long offset; } foo_t;
5
6200
by: Chris McDonald | last post by:
I've been trying to wean myself off using parentheses after the sizeof operator (and after the return keyword, too), but my understanding is challenged by the 4th use of sizeof in the following code: #include <stdio.h> struct { int i; char *p; } var;
5
2408
by: ritesh | last post by:
Hi, I'm compiling the following code on linux/cc and keep getting a compiler error "attrib.c:4: syntax error before '{' token" I'm using the following options to compile - cc -Wall -c attrib.c #include "stdio.h"
7
3127
by: smartbeginner | last post by:
I cannot view in the console what all i have included in printf() .Why this? Code: #include<stdio.h> #include<conio.h> #include "SDL/SDL.h" int main(void) { int x=1; SDL_Event event;
1
3049
by: aarklon | last post by:
Hi all, in the web page http://pramode-ce.livejournal.com/2006/01/18/ it is given as follows The GNU Compiler offers an interesting extension by which you can store the address of a label in a pointer variable. main()
15
85805
by: David Marsh | last post by:
I accidentally typed %b instead of %d in a printf format string and got a binary representation of the number. Is that standard C or a compiler extension?
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.