[EMAIL PROTECTED] wrote:
> Steven D'Aprano wrote:
> > Try something like this:
> >
> > # WARNING: untested
> > def run_without_stdout(*args, **kwargs):
> >     function = args[0]
> >     args = args[1:]
> >     savestdout = sys.stdout
> >     sys.stdout = cStringIO.StringIO()
> >     result = None
> >     try:
> >         result = function(*args, **kwargs)
> >     finally:
> >         # don't forget to restore stdout, or you
> >         # really will regret it...
> >         sys.stdout = savestdout
> >     return result
> >
> Thanks!
>
> I have tried your method, but I found it didn't work as expected.
>
> The output produced by the external function couldn't be depressed,
> but the "print " statement i wrote in python is depressed. It seems
> make cStringIO.StringIO() as a temporary replacement of sys.stdout
> has no effect on the external function.
>
> Here is an example to make myself clear(actually it's modified version
> of Steven's code):
>
> def run_without_stdout(*args, **kwargs):
>     function = args[0]
>     args = args[1:]
>     savestdout = sys.stdout
>     sys.stdout = cStringIO.StringIO()
>     print "something"
>     result = None
>     try:
>         result = function(*args, **kwargs)
>     finally:
>         # don't forget to restore stdout, or you
>         # really will regret it...
>         sys.stdout = savestdout
>     print "some other thing"
>     return result
>
> When run_without_stdout() is called, the "print" statements wrote in python
> don't produce output, but function() produces output to the standard output
> just as before:(
>
> I have tried to replace sys.stdout globally with cStringIO.StringIO()
> in my program(I mean, make "sys.stdout = cStringIO.StringIO()" as a
> globall statement), but it worked just as previous version did.

Perhaps try redirecting sys.stderr instead of sys.stdout.

André
> 
> Regards,
> 
> xiaojf

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to