Stdout in extensions under IDLE

Alex Martelli aleaxit at yahoo.com
Thu Oct 5 21:58:36 CEST 2000


"Jeff Perry" <jsp at mail.utexas.edu> wrote in message
news:8riefl$g2k$1 at geraldo.cc.utexas.edu...
> My C extension's output is different under the python command line shell
> than it is under the graphical shell, IDLE.
>
> pyext.c:
>     #include <stdio.h>
>     void Print() { printf("This is the message.\n"); }
>
> Command line python:
>
>     $ python
>     >>> import pyext
>     >>> pyext.Print()
>     This is the message.
>     >>>
>
> Windowed python:
>
>     $ idle
>     >>> import pyext
>     >>> pyext.Print ()
>     >>>
>
> Under IDLE, there is no output.
>
> Is this a bug to report to sourceforge?

No, it's a consequence of the fact that IDLE (the underlying tkinter,
actually, I believe) does not even try to redirect the underlying
C-level stdout (it would be a huge mess to try -- and what for?).

You can use, e.g., PyRun_SimpleString if you want to use the
Python-level print statement:

void Print()
{
    char buff[80];
    sprintf(buff, "print '%s\n'", "This is the message");
    PyRun_SimpleString(buff);
}

Or, more thoroughly, you can find the sys.stdout object and
call its write method.


> Is there a way to step into extensions with a debugger?

Yes, at least with MSVC++ 6 it's pretty easy.  I'm surprised
your setup works at all...:

> My environment:
> - Python 2.0 b2
> - Windows 98
> - SWIG 1.1
> - MSVC 5.0

...as I thought I had recently read on this group that MSVC++
5 can't be used any more to embed/extend Python 2 on Win32,
but rather MSVC++ 6 is required.

I don't recall how one went about it in 5.0 (it HAS been
years...), but in 6.0 you simply need to set the appropriate
python.exe as the program to run to debug the DLL (i.e.,
the PYD) that you're developing; then, from the interactive
VStudio IDE, set the breakpoints you want, and run with F5.
(Remember to _avoid_ using a runtime library marked as
"debug", if you want to run within the normal python.exe
or pythonw.exe; you can set up everything else for debug,
but as for the runtime, you MUST use the "Multithreaded
DLL" option!).  The IDE will warn you that python.exe has
no debug symbols, but that's no problem, don't worry!

If you don't have an interactive session to start with (e.g.
because you use distutils, which rely on compile-line and
not on the IDE), you can use the just-in-time-debugging
feature.  Just cause an exception where you want your
first breakpoint in your extension (DebugBreak() is a
popular Win API existing just for this purpose...) -- do recall
to add one "level of indirectness" (e.g. don't call DebugBreak
directly, call a small function whose only job is to call
DebugBreak!) to ensure against messing anything up in
your 'real' function.

You can also 'capture the process' (that is running python.exe
and has you pyd loaded), etc, etc.  In other words, you can
use all of the zillion tricks one uses with MSVC++ for most all
debugging of DLL's, COM servers, etc, etc.  Python has very
little to do with it.


Alex






More information about the Python-list mailing list