On 23 Jan 2006 04:00:40 -0800, "Fuzzyman" <[EMAIL PROTECTED]> wrote:

>
>Bengt Richter wrote:
>
[...]
>> It wouldn't be shadowing, but I suppose you could replace sys.stdout with
>> a custom object whose methods check where they were called from.
>> Then you could give the object initialization parameters as to which 
>> namespace
>> you want to have the effect in, and/or methods to control the action or turn
>> it on or off etc. BTW, how about stderr?
>>
>
>Redirecting stderr is identical in concept to redirecting stdout.
>
Yeah, just reminding, in case you need to do that too ;-)

>The following in the write method of the custom out object works :
>
>    sys._getframe(1).f_globals['__name__']
>
>sys.stdout.write is *always* called from at least one frame deep in the
>stack - so it works.
Yes, that's about what I had in mind, not being able to think of an alternative
(other than the obvious one of using something other than print in the module
where you want to redirect, either by hand or by effective source rewrite one 
way
or another to translate print statments into e.g. myprint('the', 'print', 
'args') or such.
You could do source preprocessing or translating at the AST level, or you could 
do byte code munging.
Nothing too nice.

>
>However the Python docs say of sys._getframe :
>
>    This function should be used for internal and specialized purposes
>only.
>
>Might this approach be brittle ?
>
In what sense? I guess it ought to work for the version it's defined for, but 
the advice is there.
Maybe we can get something more specific from the author of the above doc 
snippet ;-)

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to