views:

223

answers:

2

I compiled a simple hello world C module for Python and it works correctly in everything I've tried but IDLE. Here's what I type to test it:

>>> import hello
>>> hello.say_hello('Justin')

I have tried this using Python from the command prompt(I'm using Windows), in Eclipse's PyDev, and with PieDream and they all print out Hello Justin!. However, in IDLE it doesn't print anything - it just gives me the prompt.

The module and setup.py I'm using are from this page. I think that the problem is with the compiler. I'm using MinGW which I set as the compiler for distutils in a .cfg file. I build the module from the command prompt with:

python setup.py build

and get

running build
running build_ext
building 'hello' extension
creating build
creating build\temp.win32-2.6
creating build\temp.win32-2.6\Release
C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python26\include -IC:\Python26\PC -c hellomodule.c -o build\temp.win32-2.6\Release\hellomodule.o
writing build\temp.win32-2.6\Release\hello.def
creating build\lib.win32-2.6
C:\MinGW\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.6\Release\hellomodule.o build\temp.win32-2.6\Release\hello.def -LC:\Python26\libs -LC:Python26\PCbuild -lpython26 -lmsvcr90 -o build\lib.win32-2.6\hello.pyd

I'm thinking that it might have something to do with the -mno-cygwin part in there, but I'm not sure.

Any ideas or suggestions as to why this module won't work in IDLE? Should I be using a different compiler?

+5  A: 

Sounds like the hello is writing directly to stdout. Python's stdout is not necessarily the the same place as C stdout

Normally you would return a string to Python so Python can print it to it's own stdout

gnibbler
Yes, I think that you're right. If I `import sys`, `sys.stdout` is different in IDLE compared to the other places I was running the module. IDLE sets `sys.stdout` to something from the idlelib module.
Justin Peel
@Justin, now to see if you can convert the C string into a Python str and return it :)
gnibbler
@gnibbler Piece of cake. Onward and upward! Strange that most of the examples I've seen have used printf when apparently it is more appropriate to return a string.
Justin Peel
+3  A: 
Alex Martelli