Repository navigation
Accept str and bytes subclasses in to_bytes - #109
Open
niklastheiler wants to merge 2 commits into
Open
niklastheiler wants to merge 2 commits into
niklastheiler wants to merge 2 commits into
Conversation
to_bytes() is declared to return bytes, which Cython enforces as an exact type. Subclasses of bytes, or the result of encode() on a str subclass that returns a bytes subclass, were returned as-is and raised "TypeError: Expected bytes, got <subclass>" in every API using to_bytes, e.g. Session.options_set, Session.userauth_password and import_*_file. Wrap both branches in bytes(), which returns an exact bytes object as-is without a copy, so the common case is unchanged. Resolves ParallelSSH#106 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CI builds from the committed C files without Cython, so the to_bytes fix was not part of the tested build. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #106.
to_bytes()is declaredcdef bytes, so Cython requires an exactbytesreturn value. Both branches could hand back a subclass unchanged: the input itself for abytessubclass, or the result ofencode()on astrsubclass. Every API usingto_bytes()then raisedTypeError: Expected bytes, got <subclass>, for exampleSession.options_set,Session.userauth_passwordandimport_pubkey_file.This happens in practice with Ansible.
AnsibleUnsafeText.encode()returnsAnsibleUnsafeBytes, so NETCONF over ncclient's libssh transport fails (ansible-collections/ansible.netcommon#815).Change
ssh/utils.pyx: both branches now go throughbytes(). For an exactbytesobject,bytes()returns the same object without a copy, so the common case does not change.ssh/utils.c: regenerated with Cython 3.1.4, since CI builds from the committed C files.tests/test_session.py:test_str_and_bytes_subclassescoversoptions_set,userauth_passwordandimport_pubkey_filewith abytessubclass and with astrsubclass whoseencode()returns abytessubclass. The test needs no SSH server.Testing
Built locally on Python 3.12, Linux x86_64, with the embedded libssh:
TypeError: Expected bytes, got _BytesSubclass.test_should_not_segfaultandtest_set_timeout. This also holds for a build from the committed C files without Cython.