Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Does PTY.write() take in unicode string or bytes? #377

Open
shuheng-liu opened this issue Dec 15, 2023 · 0 comments
Open

Question: Does PTY.write() take in unicode string or bytes? #377

shuheng-liu opened this issue Dec 15, 2023 · 0 comments

Comments

@shuheng-liu
Copy link

shuheng-liu commented Dec 15, 2023

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.

@shuheng-liu shuheng-liu changed the title Question: PTY.write() takes unicode string or bytes? Question: Does PTY.write() takes unicode string or bytes? Dec 15, 2023
@shuheng-liu shuheng-liu changed the title Question: Does PTY.write() takes unicode string or bytes? Question: Does PTY.write() take in unicode string or bytes? Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant