python sys.stdout and C++ iostreams::cout

2013-01-17 Thread Utpal Sarkar
Hi,

I was assuming that sys.stdout would be referencing the same physical stream as 
iostreams::cout running in the same process, but this doesn't seem to be the 
case.
The following code, which makes a call to a C++ function with a python wrapper 
called "write", that writes to cout:

from cStringIO import StringIO
import sys
orig_stdout = sys.stdout
sys.stdout = stringout = StringIO()
write("cout") # wrapped C++ function that writes to cout
print "-" * 40
print "stdout"
sys.stdout = orig_stdout
print stringout.getvalue()

immediately writes "cout" to the console, then the separator "---...", and 
finally, as the return value of stringout.getvalue(), the string "stdout".
My intention was to capture in stringout also the string written to cout from 
C++.
Does anyone know what is going on, and if so, how I can capture what is written 
to cout in a python string?

Thanks in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python sys.stdout and C++ iostreams::cout

2013-01-17 Thread Utpal Sarkar
Thanks a lot Chris and Nobody! I'll have a look at dup2 for a start.

> > I was assuming that sys.stdout would be referencing the same physical 
> > stream as iostreams::cout running in the same process, but this doesn't 
> > seem to be the case.
> 
> 
> 
> That's more-or-less true, but there will likely be separate buffering,
> 
> so even without redirection you might see some oddities. But the
> 
> problem with your code is that you're not actually redirecting stdout
> 
> in any way; you're catching, at a fairly high level, everything that
> 
> Python would otherwise have sent there.
> 
> 
> 
> Is there any way that you can get the C++ code to offer a way to
> 
> redirect its output? Otherwise, you're going to have to fiddle around
> 
> with the usual mess of I/O redirection (with dup2), and you can only
> 
> send it to what the OS sees as a file (so, no StringIO buffer). So to
> 
> achieve your goal, you may need either a temporary physical file, or
> 
> some sort of pipe (and worry about reading from it before it fills up,
> 
> etc, etc). There may be alternatives, but in any case, the easiest way
> 
> is going to be with some assistance from the C++ function.
> 
> 
> 
> ChrisA

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


non-owning references?

2009-07-24 Thread Utpal Sarkar
Hi,

I'm not sure the subject describes what I'm looking for, but the
question is the following:
Is there a way I can tell a variable that the object it is pointing
too is not owned by it, in the sense that if it is the only reference
to the object it can be garbage collected?
I want this for what is essentially a singleton class, so that on
first instantiation the object is created and a reference is kept in
the class, that is used to return the same object in subsequent
instantiations. When all instances go out of scope, the reference in
the class is still there, preventing it from being garbage collected,
but since the instance can be huge, I would like it to be.

Thanks,

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