(3.2) Overload print() using the C API?

Chris Angelico rosuav at gmail.com
Thu Apr 26 14:26:43 EDT 2012


On Fri, Apr 27, 2012 at 3:57 AM, Peter Faulks <faulksp at iinet.net.au> wrote:
> I want to extend an embedded interpreter so that calls to print() are
> automagically sent to a C++ gui (windows exe) via a callback function in the
> DLL.
>
> Then I'll be able to do this:
>
> ----test.py----
> import printoverload
>
> printoverload.set_stdout()
> printoverload.set_stderr()
>
> print("this will be sent to a C function in printoverload.pyd")
> ---------------
> Is this feasible?

Absolutely.

> Any gotchas I should be aware of?

Yes, one. Make sure you use Python 3, not Python 2. Then all you have
to do is assign to print, which is a function like any other.

Alternatively, replace sys.stdout - untested but I'm pretty sure that
will work just as well.

ChrisA
PS. What on earth are you doing at 4AM on a Friday? Oh wait. What am I
doing at 4AM on a Friday? We both seem to be nocturnal Australians
here...



More information about the Python-list mailing list