summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeff Forcier <jeff@bitprophet.org>2023-01-09 19:32:57 -0500
committerJeff Forcier <jeff@bitprophet.org>2023-01-09 23:26:00 -0500
commit3e87f05b57213cc539670f85ac7ea8ccddb9e5c4 (patch)
tree5f3442839198de82f1376dbc53a0916856a8ebb9
parent768f3e238b26127cee5cf5d4d18a17a40c349c9e (diff)
Migrate some byte related helpers around
I feel like we should be able to just nuke byte_chr and friends at this point, but it's not entirely obvious, so let's rock that boat later.
-rw-r--r--paramiko/agent.py5
-rw-r--r--paramiko/ber.py4
-rw-r--r--paramiko/common.py56
-rw-r--r--paramiko/kex_curve25519.py3
-rw-r--r--paramiko/kex_ecdh_nist.py3
-rw-r--r--paramiko/kex_gex.py3
-rw-r--r--paramiko/kex_group1.py4
-rw-r--r--paramiko/kex_gss.py5
-rw-r--r--paramiko/message.py4
-rw-r--r--paramiko/packet.py5
-rw-r--r--paramiko/primes.py3
-rw-r--r--paramiko/sftp.py5
-rw-r--r--paramiko/transport.py3
-rw-r--r--paramiko/util.py24
-rw-r--r--sites/www/changelog.rst21
-rw-r--r--tests/loop.py2
-rw-r--r--tests/test_pkey.py4
-rw-r--r--tests/test_transport.py3
-rw-r--r--tests/test_util.py2
19 files changed, 91 insertions, 68 deletions
diff --git a/paramiko/agent.py b/paramiko/agent.py
index 17eb4568..31a16e2e 100644
--- a/paramiko/agent.py
+++ b/paramiko/agent.py
@@ -29,13 +29,12 @@ import time
import tempfile
import stat
from select import select
-from paramiko.common import asbytes, io_sleep
-from paramiko.py3compat import byte_chr
+from paramiko.common import io_sleep, byte_chr
from paramiko.ssh_exception import SSHException, AuthenticationException
from paramiko.message import Message
from paramiko.pkey import PKey
-from paramiko.util import retry_on_signal
+from paramiko.util import retry_on_signal, asbytes
cSSH2_AGENTC_REQUEST_IDENTITIES = byte_chr(11)
SSH2_AGENT_IDENTITIES_ANSWER = 12
diff --git a/paramiko/ber.py b/paramiko/ber.py
index a064e6b1..28737c55 100644
--- a/paramiko/ber.py
+++ b/paramiko/ber.py
@@ -15,8 +15,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-from paramiko.common import max_byte, zero_byte
-from paramiko.py3compat import b, byte_ord, byte_chr, long
+from paramiko.common import max_byte, zero_byte, byte_ord, byte_chr
+from paramiko.py3compat import b, long
import paramiko.util as util
diff --git a/paramiko/common.py b/paramiko/common.py
index cf6972d5..3721efe4 100644
--- a/paramiko/common.py
+++ b/paramiko/common.py
@@ -20,7 +20,25 @@
Common constants and global variables.
"""
import logging
-from paramiko.py3compat import byte_chr, PY2, long, b
+import struct
+
+#
+# Formerly of py3compat.py. May be fully delete'able with a deeper look?
+#
+
+def byte_chr(c):
+ assert isinstance(c, int)
+ return struct.pack("B", c)
+
+def byte_mask(c, mask):
+ assert isinstance(c, int)
+ return struct.pack("B", c & mask)
+
+def byte_ord(c):
+ # In case we're handed a string instead of an int.
+ if not isinstance(c, int):
+ c = ord(c)
+ return c
(
MSG_DISCONNECT,
@@ -184,38 +202,12 @@ max_byte = byte_chr(0xff)
cr_byte = byte_chr(13)
linefeed_byte = byte_chr(10)
crlf = cr_byte + linefeed_byte
+cr_byte_value = 13
+linefeed_byte_value = 10
+
-if PY2:
- cr_byte_value = cr_byte
- linefeed_byte_value = linefeed_byte
-else:
- cr_byte_value = 13
- linefeed_byte_value = 10
-
-
-def asbytes(s):
- """
- Coerce to bytes if possible or return unchanged.
- """
- try:
- # Attempt to run through our version of b(), which does the Right Thing
- # for string/unicode/buffer (Py2) or bytes/str (Py3), and raises
- # TypeError if it's not one of those types.
- return b(s)
- except TypeError:
- try:
- # If it wasn't a string/byte/buffer type object, try calling an
- # asbytes() method, which many of our internal classes implement.
- return s.asbytes()
- except AttributeError:
- # Finally, just do nothing & assume this object is sufficiently
- # byte-y or buffer-y that everything will work out (or that callers
- # are capable of handling whatever it is.)
- return s
-
-
-xffffffff = long(0xffffffff)
-x80000000 = long(0x80000000)
+xffffffff = 0xffffffff
+x80000000 = 0x80000000
o666 = 438
o660 = 432
o644 = 420
diff --git a/paramiko/kex_curve25519.py b/paramiko/kex_curve25519.py
index 3420fb4f..8f66609a 100644
--- a/paramiko/kex_curve25519.py
+++ b/paramiko/kex_curve25519.py
@@ -9,7 +9,8 @@ from cryptography.hazmat.primitives.asymmetric.x25519 import (
)
from paramiko.message import Message
-from paramiko.py3compat import byte_chr, long
+from paramiko.common import byte_chr
+from paramiko.py3compat import long
from paramiko.ssh_exception import SSHException
diff --git a/paramiko/kex_ecdh_nist.py b/paramiko/kex_ecdh_nist.py
index 19de2431..eb9eff74 100644
--- a/paramiko/kex_ecdh_nist.py
+++ b/paramiko/kex_ecdh_nist.py
@@ -4,8 +4,9 @@ RFC 5656, Section 4
"""
from hashlib import sha256, sha384, sha512
+from paramiko.common import byte_chr
from paramiko.message import Message
-from paramiko.py3compat import byte_chr, long
+from paramiko.py3compat import long
from paramiko.ssh_exception import SSHException
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import ec
diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py
index e6ed2392..7bf24ddd 100644
--- a/paramiko/kex_gex.py
+++ b/paramiko/kex_gex.py
@@ -26,9 +26,8 @@ import os
from hashlib import sha1, sha256
from paramiko import util
-from paramiko.common import DEBUG
+from paramiko.common import DEBUG, byte_chr, byte_ord, byte_mask
from paramiko.message import Message
-from paramiko.py3compat import byte_chr, byte_ord, byte_mask
from paramiko.ssh_exception import SSHException
diff --git a/paramiko/kex_group1.py b/paramiko/kex_group1.py
index 78894566..0dd3c26f 100644
--- a/paramiko/kex_group1.py
+++ b/paramiko/kex_group1.py
@@ -25,9 +25,9 @@ import os
from hashlib import sha1
from paramiko import util
-from paramiko.common import max_byte, zero_byte
+from paramiko.common import max_byte, zero_byte, byte_chr, byte_mask
from paramiko.message import Message
-from paramiko.py3compat import byte_chr, long, byte_mask
+from paramiko.py3compat import long
from paramiko.ssh_exception import SSHException
diff --git a/paramiko/kex_gss.py b/paramiko/kex_gss.py
index 08e5d787..55f3f5e7 100644
--- a/paramiko/kex_gss.py
+++ b/paramiko/kex_gss.py
@@ -40,10 +40,11 @@ This module provides GSS-API / SSPI Key Exchange as defined in :rfc:`4462`.
import os
from hashlib import sha1
-from paramiko.common import DEBUG, max_byte, zero_byte
+from paramiko.common import (
+ DEBUG, max_byte, zero_byte, byte_chr, byte_mask, byte_ord,
+)
from paramiko import util
from paramiko.message import Message
-from paramiko.py3compat import byte_chr, byte_mask, byte_ord
from paramiko.ssh_exception import SSHException
diff --git a/paramiko/message.py b/paramiko/message.py
index 6095d5de..f48e170a 100644
--- a/paramiko/message.py
+++ b/paramiko/message.py
@@ -23,7 +23,7 @@ Implementation of an SSH2 "message".
import struct
from paramiko import util
-from paramiko.common import zero_byte, max_byte, one_byte, asbytes
+from paramiko.common import zero_byte, max_byte, one_byte
from paramiko.py3compat import long, BytesIO, u, integer_types
@@ -270,7 +270,7 @@ class Message(object):
:param str s: string to add
"""
- s = asbytes(s)
+ s = util.asbytes(s)
self.add_int(len(s))
self.packet.write(s)
return self
diff --git a/paramiko/packet.py b/paramiko/packet.py
index af78e312..914e8971 100644
--- a/paramiko/packet.py
+++ b/paramiko/packet.py
@@ -32,13 +32,14 @@ from paramiko import util
from paramiko.common import (
linefeed_byte,
cr_byte_value,
- asbytes,
MSG_NAMES,
DEBUG,
xffffffff,
zero_byte,
+ byte_ord,
)
-from paramiko.py3compat import u, byte_ord
+from paramiko.py3compat import u
+from paramiko.util import asbytes
from paramiko.ssh_exception import SSHException, ProxyCommandFailure
from paramiko.message import Message
diff --git a/paramiko/primes.py b/paramiko/primes.py
index 564ab26f..cb7f8ca2 100644
--- a/paramiko/primes.py
+++ b/paramiko/primes.py
@@ -23,7 +23,8 @@ Utility functions for dealing with primes.
import os
from paramiko import util
-from paramiko.py3compat import byte_mask, long
+from paramiko.common import byte_mask
+from paramiko.py3compat import long
from paramiko.ssh_exception import SSHException
diff --git a/paramiko/sftp.py b/paramiko/sftp.py
index cfed9028..2e9dff66 100644
--- a/paramiko/sftp.py
+++ b/paramiko/sftp.py
@@ -21,9 +21,8 @@ import socket
import struct
from paramiko import util
-from paramiko.common import asbytes, DEBUG
+from paramiko.common import DEBUG, byte_chr, byte_ord
from paramiko.message import Message
-from paramiko.py3compat import byte_chr, byte_ord
(
@@ -191,7 +190,7 @@ class BaseSFTP(object):
return out
def _send_packet(self, t, packet):
- packet = asbytes(packet)
+ packet = util.asbytes(packet)
out = struct.pack(">I", len(packet) + 1) + byte_chr(t) + packet
if self.ultra_debug:
self._log(DEBUG, util.format_binary(out, "OUT: "))
diff --git a/paramiko/transport.py b/paramiko/transport.py
index 68cc195d..930ba4d4 100644
--- a/paramiko/transport.py
+++ b/paramiko/transport.py
@@ -86,6 +86,7 @@ from paramiko.common import (
MSG_NAMES,
MSG_EXT_INFO,
cMSG_EXT_INFO,
+ byte_ord,
)
from paramiko.compress import ZlibCompressor, ZlibDecompressor
from paramiko.dsskey import DSSKey
@@ -100,7 +101,7 @@ from paramiko.kex_gss import KexGSSGex, KexGSSGroup1, KexGSSGroup14
from paramiko.message import Message
from paramiko.packet import Packetizer, NeedRekeyException
from paramiko.primes import ModulusPack
-from paramiko.py3compat import string_types, long, byte_ord, b, input, PY2
+from paramiko.py3compat import string_types, long, b, input, PY2
from paramiko.rsakey import RSAKey
from paramiko.ecdsakey import ECDSAKey
from paramiko.server import ServerInterface
diff --git a/paramiko/util.py b/paramiko/util.py
index 4267caf1..096b74ff 100644
--- a/paramiko/util.py
+++ b/paramiko/util.py
@@ -29,8 +29,7 @@ import traceback
import threading
import logging
-from paramiko.common import DEBUG, zero_byte, xffffffff, max_byte
-from paramiko.py3compat import PY2, long, byte_chr, byte_ord, b
+from paramiko.common import DEBUG, zero_byte, xffffffff, max_byte, byte_ord, byte_chr
from paramiko.config import SSHConfig
@@ -302,3 +301,24 @@ class ClosingContextManager(object):
def clamp_value(minimum, val, maximum):
return max(minimum, min(val, maximum))
+
+
+def asbytes(s):
+ """
+ Coerce to bytes if possible or return unchanged.
+ """
+ try:
+ # Attempt to run through our version of b(), which does the Right Thing
+ # for string/unicode/buffer (Py2) or bytes/str (Py3), and raises
+ # TypeError if it's not one of those types.
+ return b(s)
+ except TypeError:
+ try:
+ # If it wasn't a string/byte/buffer type object, try calling an
+ # asbytes() method, which many of our internal classes implement.
+ return s.asbytes()
+ except AttributeError:
+ # Finally, just do nothing & assume this object is sufficiently
+ # byte-y or buffer-y that everything will work out (or that callers
+ # are capable of handling whatever it is.)
+ return s
diff --git a/sites/www/changelog.rst b/sites/www/changelog.rst
index 774cde27..595e2fde 100644
--- a/sites/www/changelog.rst
+++ b/sites/www/changelog.rst
@@ -2,21 +2,28 @@
Changelog
=========
+- :support:`-` ``paramiko.common.asbytes`` has been moved to
+ `paramiko.util.asbytes`.
+
+ .. warning::
+ This change is backwards incompatible.
+
- :support:`-` Remove the now irrelevant ``paramiko.py3compat`` module.
.. warning::
- This change is backwards incompatible if you were importing anything from
- this module. Such references should be easily search-and-replaced with
- their modern Python 3.6+ equivalents.
+ This change is backwards incompatible. Such references should be
+ search-and-replaced with their modern Python 3.6+ equivalents; in some
+ cases, still-useful methods or values have been moved to `paramiko.util`
+ (most) or `paramiko.common` (``byte_*``).
- :support:`-` Drop support for Python versions less than 3.6, including Python
2. So long and thanks for all the fish!
.. warning::
- This change is backwards compatible if you are still on older Python
- versions. However, our packaging metadata has been updated to include
- ``python_requires``, so this should not cause breakage unless you're on an
- old installation method that can't read this metadata.
+ This change is backwards compatible. However, our packaging metadata has
+ been updated to include ``python_requires``, so this should not cause
+ breakage unless you're on an old installation method that can't read this
+ metadata.
.. note::
As part of this change, our dependencies have been updated; eg we now
diff --git a/tests/loop.py b/tests/loop.py
index 6de4b164..51dc6308 100644
--- a/tests/loop.py
+++ b/tests/loop.py
@@ -19,7 +19,7 @@
import socket
import threading
-from paramiko.common import asbytes
+from paramiko.util import asbytes
class LoopSocket(object):
diff --git a/tests/test_pkey.py b/tests/test_pkey.py
index a2376fa9..bc1503ab 100644
--- a/tests/test_pkey.py
+++ b/tests/test_pkey.py
@@ -36,8 +36,8 @@ from paramiko import (
util,
SSHException,
)
-from paramiko.py3compat import StringIO, byte_chr, b, bytes, PY2
-from paramiko.common import o600
+from paramiko.py3compat import StringIO, b, bytes, PY2
+from paramiko.common import o600, byte_chr
from cryptography.exceptions import UnsupportedAlgorithm
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateNumbers
diff --git a/tests/test_transport.py b/tests/test_transport.py
index 98a7d30d..5263c038 100644
--- a/tests/test_transport.py
+++ b/tests/test_transport.py
@@ -57,8 +57,9 @@ from paramiko.common import (
MSG_USERAUTH_SUCCESS,
cMSG_CHANNEL_WINDOW_ADJUST,
cMSG_UNIMPLEMENTED,
+ byte_chr,
)
-from paramiko.py3compat import bytes, byte_chr
+from paramiko.py3compat import bytes
from paramiko.message import Message
from .util import needs_builtin, _support, requires_sha1_signing, slow
diff --git a/tests/test_util.py b/tests/test_util.py
index 0e485759..1869a7bb 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -29,7 +29,7 @@ import unittest
import paramiko
import paramiko.util
from paramiko.util import safe_string
-from paramiko.py3compat import byte_ord
+from paramiko.common import byte_ord
test_hosts_file = """\