NEWBIE: Extending Python with C

Bengt Richter bokr at oz.net
Sun Nov 10 00:09:43 EST 2002


On Sat, 09 Nov 2002 08:02:43 GMT, Alex Martelli <aleax at aleax.it> wrote:
[...]
>
>This is more or less the end of the line in terms of what
>os.popen can offer -- due to buffering and suchlike there's
>no practical way for python to drive BOTH standard input
>and standard-output of another external program.

I think there *ought* to be a practical way, *and* amongst the batteries ;-)

It's hard to believe that, for NT at least, someone out there
hasn't written a console proxy module that could interface to console
apps the way typing and viewing in a console window does, so something
like the following (unimplemented) code would work as expected, without
(or at least transparently hiding) the buffering problems of the popen
family. (Exception handling omitted here):

import ConsoleProxy as CP
cp = CP.ConsoleProxy('some_console_app.exe') # w/ opt args for cmdline, env, working dir, etc.
while 1:
    line = raw_input('> ')
    if line == 'quit': break
    cp.write(line)
    print cp.read12()	# example convenience method to get both stdout & stderr
del cp

As you point out, TMTOWTDI applies in spades, but if there's
a solid way to run a program to do what you want from the
console window command line, why shouldn't there be a solid
way to use that via something like the above? (Especially handy for
those who don't have compilers or ready knowledge to make C extensions).

I haven't needed such a thing yet, but if I had to, I think I
could make one, and that generally means someone already has ;-)

[... further TMTOWTDI elaborations snipped ...]

Regards,
Bengt Richter



More information about the Python-list mailing list