473,654 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Redirecting standard out in a single namespace

Hello,

I'm trying to redirect standard out in a single namespace.

I can replace sys.stdout with a custom object - but that affects all
namespaces.

There will be code running simultaneously that could import sys
afterwards - so I don't want to make the change in the sys module.

I have an idea to redefine __import__ for the relevant namespace - so
that an attempt to import sys will return a different module with a
custom object for stdout. As sys is a builtin module this might not
work for the print statement, which is what I want to redirect.

I'm not in a position to test my idea until later :

__import = __import__
def __import__(name ):
if name == 'sys':
return __import('newsy s')
else:
return __import(name)

import sys

Is there another way to shadow the sys module from a single namespace ?

All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Jan 20 '06 #1
9 1756
Fuzzyman wrote:
Is there another way to shadow the sys module from a single namespace ?


I've never actually tried this, but try making a copy of the sys module
then replacing the stdout object with your own object. Then you can
replace "sys" of the namespace with your custom "sys" module. I guess it
would look something like this:

import mysys #My dummy sys module
import sys #Actual sys module

#Copy sys module into dummy sys module
mysys.__dict__. update(sys.__di ct__)

#Replace stdout with custom stdout
mysys.stdout = MyStdout()

#Replace sys of namespace
namespace.sys = mysys
This seems like it should work, however I don't know if copying the
entire dictionary of one module to another is a safe thing to do.

-Farshid
Jan 20 '06 #2
I'm sorry, but i don't see how this will solve the problem? It is
exactly the same, only now you've replaced everything in sys except
just sys.stdout?
At any rate, perhapse the code you will write will be more maintainable
if instead of redirecting sys.stdout for some of the code just use a
different function (instead of print) for the times where you wish it
to be redirected somewhere else? it will probably make your code
longer, but the maintainer will have one less "gotcha" to worry about a
few months/years down the road. perhapse it is best to not do the
"clever" thing this time ;-) ?

Jan 20 '06 #3

Ido Yehieli wrote:
I'm sorry, but i don't see how this will solve the problem? It is
exactly the same, only now you've replaced everything in sys except
just sys.stdout?
In the solution above (which I haven't had a chance to test) you're
just chaning hte reference to sys to point to a different module, so
you're not messing with sys itself. This won't affect other code that
uses the sys module.

Because of the builtin status of the sys module I'm not *convinced* it
will work. I'll try it though.
At any rate, perhapse the code you will write will be more maintainable
if instead of redirecting sys.stdout for some of the code just use a
different function (instead of print) for the times where you wish it
to be redirected somewhere else? it will probably make your code
longer, but the maintainer will have one less "gotcha" to worry about a
few months/years down the road. perhapse it is best to not do the
"clever" thing this time ;-) ?


We're executing code from another module that uses the print statement
a lot. That module isn't our code - other wise we wouldn't have a
problem. :-)

Changing the module will mean a great deal more to maintain, not less.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Jan 21 '06 #4
oh, ok...
I guess people have to learn to use a logger instead of print in
production code...

Jan 21 '06 #5
On 20 Jan 2006 07:37:15 -0800, "Fuzzyman" <fu******@gmail .com> wrote:
Hello,

I'm trying to redirect standard out in a single namespace.

I can replace sys.stdout with a custom object - but that affects all
namespaces.

There will be code running simultaneously that could import sys
afterwards - so I don't want to make the change in the sys module.

I have an idea to redefine __import__ for the relevant namespace - so
that an attempt to import sys will return a different module with a
custom object for stdout. As sys is a builtin module this might not
work for the print statement, which is what I want to redirect.

I'm not in a position to test my idea until later :

__import = __import__
def __import__(name ):
if name == 'sys':
return __import('newsy s')
else:
return __import(name)

import sys

Is there another way to shadow the sys module from a single namespace ?

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?

Regards,
Bengt Richter
Jan 23 '06 #6

Bengt Richter wrote:
On 20 Jan 2006 07:37:15 -0800, "Fuzzyman" <fu******@gmail .com> wrote:
Hello,

I'm trying to redirect standard out in a single namespace.

I can replace sys.stdout with a custom object - but that affects all
namespaces.

There will be code running simultaneously that could import sys
afterwards - so I don't want to make the change in the sys module.

I have an idea to redefine __import__ for the relevant namespace - so
that an attempt to import sys will return a different module with a
custom object for stdout. As sys is a builtin module this might not
work for the print statement, which is what I want to redirect. [snip..]Is there another way to shadow the sys module from a single namespace ?
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?


I've just tried checking __name__ in my custom stdout object.
Unfortunately __name__ is always the module in which the new stdout
object lives.

In theory I could go up (down?) the stack to the previous frame and
check __name__ there - but it sounds like a hack.

Any other ways of checking where sys.stdout is called from ?

All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Regards,
Bengt Richter


Jan 23 '06 #7

Bengt Richter wrote:
On 20 Jan 2006 07:37:15 -0800, "Fuzzyman" <fu******@gmail .com> wrote:
Hello,

I'm trying to redirect standard out in a single namespace.

I can replace sys.stdout with a custom object - but that affects all
namespaces.

There will be code running simultaneously that could import sys
afterwards - so I don't want to make the change in the sys module.

I have an idea to redefine __import__ for the relevant namespace - so
that an attempt to import sys will return a different module with a
custom object for stdout. As sys is a builtin module this might not
work for the print statement, which is what I want to redirect.
[snip..]
Is there another way to shadow the sys module from a single namespace ?
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.

The following in the write method of the custom out object works :

sys._getframe(1 ).f_globals['__name__']

sys.stdout.writ e is *always* called from at least one frame deep in the
stack - so it works.

However the Python docs say of sys._getframe :

This function should be used for internal and specialized purposes
only.

Might this approach be brittle ?

All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Regards,
Bengt Richter


Jan 23 '06 #8
On 23 Jan 2006 04:00:40 -0800, "Fuzzyman" <fu******@gmail .com> 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.wri te 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
Jan 24 '06 #9

Bengt Richter wrote:
[snip..]
The following in the write method of the custom out object works :

sys._getframe(1 ).f_globals['__name__']

sys.stdout.wri te 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 ;-)


``inspect.curre ntframe`` has no such warning. The equivalent would then
be :

inspect.current frame().f_back. f_globals['__name__']

I guess unless the implementation of Python changes that will continue
to work. :-)

Thanks

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Regards,
Bengt Richter


Jan 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
3099
by: Matt Burland | last post by:
I'm having a problem with redirecting the StandardOutput of a process that I use to run a DOS program in my application. The problem is that I when I start my process (which I do in a separate thread) I can't read anything from the stdout before the process actually finishes. If I do something like this: Process myProcess = new Process(myStartInfo) // this gets shown straight away MessageBox.Show("Process Started"); string s =...
32
30482
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of namespace in C. Of course, namespace doesn't eliminate name polution, but it helps more than just to put some prefix before a function name. Is it because adding namespace will make C more complex or some other reasons?
1
1481
by: Gaijinco | last post by:
Hi! I was coding something which had some lines like these: int main(){ int n; scanf("%d",&n); fflush(stdin); int* ans = new int; int idx=0;
9
3009
by: PengYu.UT | last post by:
Hi, I feel argparse has some useful things that optparse doesn't have. But I can't find it argparse in python library reference. I'm wondering when it will be available in the python standard installation. Thanks, Peng
22
2283
by: Chris Thomasson | last post by:
I am thinking about using this technique for all the "local" memory pools in a paticular multi-threaded allocator algorithm I invented. Some more info on that can be found here: http://groups.google.com/group/comp.arch/browse_frm/thread/24c40d42a04ee855 Anyway, here is the code snippet: #include <cstdio> #include <cstddef>
4
1661
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: http://mail.python.org/pipermail/python-dev/2007-February/070921.html http://mail.python.org/pipermail/python-dev/2007-February/071155.html http://mail.python.org/pipermail/python-dev/2007-February/071181.html I'd love to have feedback on this PEP: - from a user's perspective (would you want to write...
0
2582
by: kreismaler | last post by:
I have some problems to understand the difference of using the STDOUT and using "anonymous pipes" as shown below: using System; using System.Diagnostics; using System.IO; namespace ProcessTest { class Program
0
1058
by: darrel | last post by:
I'm trying to set up a web.config file (2.0) so that all 404 errors are redirected to another page. What happens instead of the redirect, however, is that the browser tries loading the actual web.config xml file. This a) is wrong and b) seems like a huge security issue that IIS is actually serving the XML file. Any ideas what I'm doing wrong?
0
8285
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.