pywinpty
pywinpty copied to clipboard
Question: Does `PTY.write()` take in unicode string or bytes?
Hi there, First, thanks for this awesome library! However, I have a minor confusion about the input type of PTY.write() and I'd appreciate your explanation.
from winpty import PTY
cols, rows = 80, 25
process = PTY(cols, rows)
process.spawn("python.exe")
process.write('print(123)\r\n') # <-- line in question
print(process.read(), end="", flush=True)
Through reading the README.md example and the doc-string here, I think process.write takes in a python bytes
string, meaning I need to add a b
in front of the string literal. However, if I do so, I get the following TypeError
TypeError: argument 'to_write': 'bytes' object cannot be converted to 'PyString'
The only way I can make it work is to pass a unicode string (default string for python3).
This is fine in most circumstances, as I can just decode the bytes into unicode string. However, in cases where we want to pass in certain control characters, e.g., ARROW UP (\xe0H
) on my device and terminal, I cannot decode the bytes it into unicode. Is there a workaround?
FYI, I'm using an ARM-based Windows 11 via Parallels on a M2 MacBook Pro. The python version is 3.12.1. My python environment is pretty pristine with only the following packages installed
colorama==0.4.6
flaky==3.7.0
iniconfig==2.0.0
packaging==23.2
pexpect==4.9.0
pluggy==1.3.0
ptyprocess==0.7.0
pytest==7.4.3
pytest-lazy-fixture==0.6.3
pywinpty==2.0.12
All the above packages are installed using pip install
.