Reputation: 2297
Following up on a previous question ( subprocess: PyDev console vs. cmd.exe ), is there a way to change where PyDev sends stdout--namely to a tty terminal?
I've come across several instances where not having a tty terminal has been limiting. For the case of the subprocess module, I can use the CREATE_NEW_CONSOLE flag, but in other instances, such as in this question ( Print \r correctly in console ), the PyDev console doesn't seem to support using escape characters.
Any thoughts are appreciated.
Upvotes: 3
Views: 1987
Reputation: 25362
This is currently a limitation in Eclipse... (which PyDev inherits).
Aptana Studio does have a terminal view which could probably be used as a replacement, but there are no plans to do so for now.
Answering comment below, to create a new shell from a running Python program it's possible to use the code below:
import subprocess
import sys
import os
args = [sys.executable] + sys.argv
new_environ = os.environ.copy()
if hasattr(subprocess, 'CREATE_NEW_CONSOLE'):
popen = subprocess.Popen(args, env=new_environ, creationflags=subprocess.CREATE_NEW_CONSOLE)
exit_code = popen.wait()
else:
#On Linux, CREATE_NEW_CONSOLE is not available, thus, we use xterm itself.
args = ['xterm', '-e'] + args
popen = subprocess.Popen(args, env=new_environ)
popen.wait() #This exit code will always be 0 when xterm is executed.
Upvotes: 0
Reputation: 19377
I normally deal with issues like this through the logging
module in the standard library, which is quite good, but I'll assume you have good reason for wanting this.
I would be surprised if the PyDev console supported full terminal emulation. At least under Helios on Windows, I've had no problem with Unicode display, but terminal escapes are another matter.
If you know specifically which terminal you want to use, you can run sleep 3600
in it and then do this in your test driver:
import sys
def redirect_terminal(ttypath):
term = open(ttypath, 'w+')
sys.stdout = term
sys.stderr = term
Trying this in the interactive interpreter, which will likely be a bit different from running it in PyDev, I get this in the initial terminal (note local echo and prompt still returned here):
>>> redirect_terminal('/dev/pts/0')
>>> dir()
>>> raise TypeError
>>>
and this in the /dev/pts/0
terminal:
['__builtins__', '__doc__', '__name__', '__package__', 'redirect_terminal', 'sys']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError
While I did not try any terminal escapes here, they are just byte sequences that get printed like any other, so they should be printed on the remote terminal.
I cannot manage to collect input from a different terminal in the interactive interpreter. When I try, input is still read from the initial terminal.
Upvotes: 1