diff options
author | Jeff Forcier <jeff@bitprophet.org> | 2023-01-12 21:17:00 -0500 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2023-01-12 21:44:59 -0500 |
commit | d04966dcec2f8e34f0ec0aea216ea098673ae90b (patch) | |
tree | e203bcda882e2b90124e209db4ffa0d5c990a516 | |
parent | 714350c4dd459577898d72a59edc3213a99443d6 (diff) |
Swap around __bytes__/__str__ for some classes
Plus related updates to tests, docstrings, changelog
-rw-r--r-- | paramiko/message.py | 5 | ||||
-rw-r--r-- | paramiko/pkey.py | 3 | ||||
-rw-r--r-- | sites/www/changelog.rst | 8 | ||||
-rw-r--r-- | tests/test_message.py | 6 |
4 files changed, 17 insertions, 5 deletions
diff --git a/paramiko/message.py b/paramiko/message.py index d02f7855..cdefe4f8 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -54,10 +54,7 @@ class Message(object): else: self.packet = BytesIO() - def __str__(self): - """ - Return the byte stream content of this message, as a string/bytes obj. - """ + def __bytes__(self): return self.asbytes() def __repr__(self): diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 058d1258..d5ea27b6 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -110,6 +110,7 @@ class PKey(object): """ pass + # TODO 4.0: just merge into __bytes__ (everywhere) def asbytes(self): """ Return a string of an SSH `.Message` made up of the public part(s) of @@ -118,7 +119,7 @@ class PKey(object): """ return bytes() - def __str__(self): + def __bytes__(self): return self.asbytes() # noinspection PyUnresolvedReferences diff --git a/sites/www/changelog.rst b/sites/www/changelog.rst index 595e2fde..63cb6d60 100644 --- a/sites/www/changelog.rst +++ b/sites/www/changelog.rst @@ -2,6 +2,14 @@ Changelog ========= +- :bug:`- major` A handful of lower-level classes (notably + `paramiko.message.Message` and `paramiko.pkey.PKey`) previously returned + `bytes` objects from their implementation of ``__str__``, even under Python + 3; and there was never any ``__bytes__`` method. + + These issues have been fixed by renaming ``__str__`` to ``__bytes__`` and + relying on Python's default "stringification returns the output of + ``__repr__``" behavior re: any real attempts to ``str()`` such objects. - :support:`-` ``paramiko.common.asbytes`` has been moved to `paramiko.util.asbytes`. diff --git a/tests/test_message.py b/tests/test_message.py index bd2d5795..3c5f961b 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -105,3 +105,9 @@ class MessageTest(unittest.TestCase): self.assertEqual(msg.get_adaptive_int(), 5) self.assertEqual(msg.get_so_far(), self.__d[:4]) self.assertEqual(msg.get_remainder(), self.__d[4:]) + + def test_bytes_str_and_repr(self): + msg = Message(self.__d) + assert str(msg) == f"paramiko.Message({self.__d!r})" + assert repr(msg) == str(msg) + assert bytes(msg) == msg.asbytes() == self.__d |