stdout custom

C

castironpi

The code should have extra colons.
.... def __init__( self, old ):
.... self._old= old
.... def write( self, s ):
.... self._old.write( ':' )
.... return self._old.write( s )
.... def flush( self ):
.... self._old.flush()
....
Specifically, before the prompts. Where does the prompt write come
from; why doesn't it honor my settings of sys.stdout and sys.stderr?
 
G

Gabriel Genellina

Specifically, before the prompts. Where does the prompt write come
from; why doesn't it honor my settings of sys.stdout and sys.stderr?

The interactive interpreter uses directly the C predefined streams stdout
and stderr.
 
C

castironpi

The interactive interpreter uses directly the C predefined streams stdout  
and stderr.

Can I allocate a second console window, so I can place certain output
to that directly, and leave the original streams alone? I tried some
things in subprocess (Py 3a3 /WinXP) but they failed. I don't know if
it's supposed to be possible though, so I didn't press very hard or
keep the code. If it is, I can go repro where it went south. Is it?
 
G

Gabriel Genellina

Can I allocate a second console window, so I can place certain output
to that directly, and leave the original streams alone?  I tried some
things in subprocess (Py 3a3 /WinXP) but they failed.  I don't know if
it's supposed to be possible though, so I didn't press very hard or
keep the code.  If it is, I can go repro where it went south.  Is it?

Have you tried using the creationflags argument to subprocess.Popen?
Specially the CREATE_NEW_CONSOLE flag. See the Microsoft documentation
for CreateProcess at http://msdn2.microsoft.com/en-us/library/ms682425(VS.85).aspx
(Note that a process can be attached at most to one console)

If your goal is to output some debug information, try using
OutputDebugString + the DebugView utility from www.sysinternals.com
 
C

castironpi

Have you tried using the creationflags argument to subprocess.Popen?
Specially the CREATE_NEW_CONSOLE flag. See the Microsoft documentation
for CreateProcess athttp://msdn2.microsoft.com/en-us/library/ms682425(VS.85).aspx
(Note that a process can be attached at most to one console)

If your goal is to output some debug information, try using
OutputDebugString + the DebugView utility fromwww.sysinternals.com

One console per process is fine, but I tried using 'cmd.exe',
'cmd.exe /K', and 'more.com' (fully specified in c/windows/system32)
as separate processes. The sign is the console window splashes up and
vanishes right away.
0

------------------------Couple other symptoms.
'f.write' is not recognized as an internal or external command,
operable program or batch file.
f.write( b'abc' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Programs\Python\lib\io.py", line 1240, in write
s.__class__.__name__)
TypeError: can't write bytes to text streamf.write( 'abc' )
3
^Z



5000
09871234

------------------------
f.write('2'*2000) 2000
p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl ags= 16 )
p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl ags= 16 )
p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl ags= 16 )
p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl ags= 16 )
p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl ags= 16 )
p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl ags= 16 )
f= os.fdopen( q[0], 'a' )
p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl ags= 16 )
p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl ags= 16 )
f.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Programs\Python\lib\io.py", line 1378, in read
res += decoder.decode(self.buffer.read(), True)
File "C:\Programs\Python\lib\io.py", line 564, in read
self._unsupported("read")
File "C:\Programs\Python\lib\io.py", line 240, in _unsupported
(self.__class__.__name__, name))
io.UnsupportedOperation: BufferedWriter.read() not supportedTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Programs\Python\lib\io.py", line 1384, in read
readahead, pending = self._read_chunk()
File "C:\Programs\Python\lib\io.py", line 1277, in _read_chunk
readahead = self.buffer.read1(self._CHUNK_SIZE)
AttributeError: 'BufferedWriter' object has no attribute 'read1'Traceback (most recent call last):
ags=0 )
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
 
T

Tim Golden

I've rather lost track of what you're trying to do, but I would
second Gabriel's suggestion of the standard Windows method of
debug output: using OutputDebugString. There's an example here:

http://timgolden.me.uk/python/win32_how_do_i/capture-OutputDebugString.html

and it shouldn't be too hard to wrap it in a file-like
object for stderr-substitution use, say.

Obviously there are 1,001 other ways of doing IPC but since this
one's ready-made you might as well use it. You can distinguish
between different processes' outputs by virtue of the PID which
is the first item on the mmap.

TJG
 
C

castironpi

I've rather lost track of what you're trying to do, but I would
second Gabriel's suggestion of the standard Windows method of
debug output: using OutputDebugString. There's an example here:

http://timgolden.me.uk/python/win32_how_do_i/capture-OutputDebugStrin...

and it shouldn't be too hard to wrap it in a file-like
object for stderr-substitution use, say.

Obviously there are 1,001 other ways of doing IPC but since this
one's ready-made you might as well use it. You can distinguish
between different processes' outputs by virtue of the PID which
is the first item on the mmap.

TJG

I want a handle to another window.

Create B with a command.
___ ___
|A | |B |
|___| |___|

B.stdin (the stdin to B).write( '?' )
___ ___
|A | |B? |
|___| |___|

A.stdout.write( '*' )
___ ___
|A* | |B? |
|___| |___|

A big tease is good too.
 
C

castironpi

Can I allocate a second console window, so I can place certain output
I want a handle to another window.

Create B with a command.
 ___   ___
|A  | |B  |
|___| |___|

B.stdin (the stdin to B).write( '?' )
 ___   ___
|A  | |B? |
|___| |___|

A.stdout.write( '*' )
 ___   ___
|A* | |B? |
|___| |___|

This is a little weird. I visited your link, but couldn't make any
sense of it, so I tried something else myself. I'm not even sure what
it accomplishes, but if you're on a different tack or way ahead of me,
that can happen. However, it might be closer than I think to what I
want-- my next step is to CreateProcess in the separate executable...
then try to merge in back into python and subprocess.

Now I've tried:
5

and I get the message box (lower), but no ':' sentinel nor the
output. However the p.stdin.write call still returns if I close the
new console window and call it after.

astdin.cpp:

#include <windows.h>
#include <iostream>
#include <string>

using namespace std;

DWORD WINAPI ThreadProc( LPVOID ) {
while (1)
{
Sleep( 1000 );
cout<< ':';
}
}

int main() {
MessageBox( NULL, "none", NULL, 0 );
cout<< "Ok"<< endl;
CreateThread( NULL, 0, ThreadProc, NULL, 0, NULL );
while (1)
{
string s;
cin>> s;
cout<< '>';
cout<< s;
}
return 0;
}
 
G

Gabriel Genellina

One console per process is fine, but I tried using 'cmd.exe',
'cmd.exe /K', and 'more.com' (fully specified in c/windows/system32)
as separate processes. The sign is the console window splashes up and
vanishes right away.

Apparently you have to either redirect stdout AND stdin, or none of them.
This worked for me:

p = subprocess.Popen('c:\\windows\\system32\\cmd.exe',
stdout=subprocess.PIPE, stdin=subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_CONSOLE)
p.communicate("dir\n")
 
C

castironpi

Can I allocate a second console window, so I can place certain output
Apparently you have to either redirect stdout AND stdin, or none of them.  
This worked for me:

p = subprocess.Popen('c:\\windows\\system32\\cmd.exe',
         stdout=subprocess.PIPE, stdin=subprocess.PIPE,
         creationflags=subprocess.CREATE_NEW_CONSOLE)
p.communicate("dir\n")

This worked for me.

import subprocess
p = subprocess.Popen('c:\\windows\\system32\\cmd.exe',
stdout=subprocess.PIPE, stdin=subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_CONSOLE)

import time
import threading
def th():
while 1:
time.sleep( .01 )
s= p.stdout.read(1)
print( ':', end= '' )
if not s: continue
print( s.decode(), end= '' )


import thread
thread.start_new_thread( th, () )
time.sleep( 2 )
p.stdin.write( b'c:\\windows\\system32\\more.com\n' )
p.stdin.flush()
print ( 'abc' )
while 1:
time.sleep( 1 )
p.stdin.write( b'abc\n' )
p.stdin.flush()
print ( 'abc' )
p.wait()
time.sleep( 10 )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top