On Jul 15, 2009, at 10:41 AM, Nathann Cohen wrote: > Hello !!! > > I am writing an interface to a C library and I am having trouble with > one of its functions... It is writing a lot of text to stdout or > stderr ( I do not even know that ! ) and I would like to mute that. I > tried this trick I found on a website : > > #stdout.py > import sys,os > > saveout = sys.stdout > fsock = open(os.devnull, 'w') > sys.stdout = fsock > print 'This message will be logged instead of displayed' > sys.stdout = saveout > fsock.close() > > Where the "print" command is replaced by the C function I call.
As written, that should work just fine. However, if your C function call uses printf, etc. directly then re-directing Python's stdout will not redirect the C-level stdout by default. What you probably want is freopen, but make sure you always reset it (for example, the above code is dangerous--if the function you call raises an error the stdout will never be restored). > I began to think it may come from the fact I am using Cython > instead of > Python... Do you know of such a thing, or is it just another one of my > mistakes ? ;-) > > Thanks ! > > Nathann > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---
