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

cpython difference in bind(), connect() arguments #8957

Closed
jepler opened this issue Feb 20, 2024 · 5 comments
Closed

cpython difference in bind(), connect() arguments #8957

jepler opened this issue Feb 20, 2024 · 5 comments

Comments

@jepler
Copy link
Member

jepler commented Feb 20, 2024

CircuitPython version

9.0.0-beta

Code/REPL

>>> s = pool.socket() # or socket.socket
>>> b = bytearray(b'0.0.0.0')
>>> s.connect((b, 443))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert 'bytearray' object to str implicitly

Behavior

In standard Python, the bytearray is accepted (and the connection fails, because there's no listener):

Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux
>>> b = bytearray(b'0.0.0.0')
>>> s.connect((b, 443))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ConnectionRefusedError: [Errno 111] Connection refused

Similar for bind():

# circuitpython
>>> s.bind((bytearray(), 1234))
TypeError: can't convert 'bytearray' object to str implicitly

vs

# cpython 3.11
>>> s.bind((bytearray(), 1234))
>>> 

Description

No response

Additional information

I ran into this while working on #8954. In that PR I was converting the bind/connect argument to a bytearray (because this let me avoid an allocation) but it didn't work.

@jepler jepler added the bug label Feb 20, 2024
@jepler
Copy link
Member Author

jepler commented Feb 20, 2024

While working on this it might be nice to make the empty hostname function the same as '0.0.0.0', again for compatibility with standard python.

@tannewt
Copy link
Member

tannewt commented Feb 20, 2024

What is the advantage of allowing bytearray ASCII? This seems to go against the idea that strings are for bytes + encoding.

@tannewt tannewt added this to the Long term milestone Feb 20, 2024
@jepler
Copy link
Member Author

jepler commented Feb 22, 2024

standard Python accepts a bytearray here, even if it's surprising. if we can accept a bytearray without (say) code bloat, I don't see why not to.

@tannewt
Copy link
Member

tannewt commented Feb 22, 2024

Where does the bytearray come from? It should be a string. We don't do many things that CPython allows. Only the reverse needs to be true. (That CPython is ok with what we do.)

@jepler
Copy link
Member Author

jepler commented Mar 6, 2024

In the draft ssl-anything PR I arrange to just pass the original objects down, instead of unparsing them and rebuilding them. So except for compatiblity with a weird nook of cpython this is moot for me.

@jepler jepler closed this as completed Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants