diff options
author | Jeff Forcier <jeff@bitprophet.org> | 2023-01-09 22:47:21 -0500 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2023-01-09 23:26:00 -0500 |
commit | 9e83ca9d8acb5e9d41d45d46a0339e2f633e2dea (patch) | |
tree | 6cb926091b66800f98e75bff931db836bda7a4be | |
parent | 7b8106e67c51e71fc4074f7851bbaebed4d5be0c (diff) |
Move b, u helpers to util module for now
Later on we can likely nuke many uses of these
-rw-r--r-- | paramiko/_winapi.py | 3 | ||||
-rw-r--r-- | paramiko/auth_handler.py | 2 | ||||
-rw-r--r-- | paramiko/ber.py | 2 | ||||
-rw-r--r-- | paramiko/buffered_pipe.py | 2 | ||||
-rw-r--r-- | paramiko/ed25519key.py | 2 | ||||
-rw-r--r-- | paramiko/file.py | 4 | ||||
-rw-r--r-- | paramiko/hostkeys.py | 4 | ||||
-rw-r--r-- | paramiko/message.py | 2 | ||||
-rw-r--r-- | paramiko/packet.py | 3 | ||||
-rw-r--r-- | paramiko/pkey.py | 3 | ||||
-rw-r--r-- | paramiko/sftp_client.py | 3 | ||||
-rw-r--r-- | paramiko/sftp_file.py | 2 | ||||
-rw-r--r-- | paramiko/sftp_server.py | 3 | ||||
-rw-r--r-- | paramiko/transport.py | 4 | ||||
-rw-r--r-- | paramiko/util.py | 21 | ||||
-rw-r--r-- | paramiko/win_pageant.py | 2 | ||||
-rw-r--r-- | tests/test_agent.py | 2 | ||||
-rw-r--r-- | tests/test_auth.py | 2 | ||||
-rw-r--r-- | tests/test_pkey.py | 3 |
19 files changed, 46 insertions, 23 deletions
diff --git a/paramiko/_winapi.py b/paramiko/_winapi.py index df02b114..1f55db91 100644 --- a/paramiko/_winapi.py +++ b/paramiko/_winapi.py @@ -9,7 +9,8 @@ in jaraco.windows and asking the author to port the fixes back here. import sys import ctypes.wintypes -from paramiko.py3compat import u, builtins +from paramiko.py3compat import builtins +from paramiko.util import u ###################### diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index db89670a..f18a3ed6 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -62,7 +62,7 @@ from paramiko.common import ( cMSG_USERAUTH_BANNER, ) from paramiko.message import Message -from paramiko.py3compat import b, u +from paramiko.util import b, u from paramiko.ssh_exception import ( SSHException, AuthenticationException, diff --git a/paramiko/ber.py b/paramiko/ber.py index a59a0f93..4e93e66f 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -16,9 +16,9 @@ # 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, byte_ord, byte_chr -from paramiko.py3compat import b import paramiko.util as util +from paramiko.util import b class BERException(Exception): diff --git a/paramiko/buffered_pipe.py b/paramiko/buffered_pipe.py index d825efdf..79854ada 100644 --- a/paramiko/buffered_pipe.py +++ b/paramiko/buffered_pipe.py @@ -25,7 +25,7 @@ read operations are blocking and can have a timeout set. import array import threading import time -from paramiko.py3compat import b +from paramiko.util import b class PipeTimeout(IOError): diff --git a/paramiko/ed25519key.py b/paramiko/ed25519key.py index c9e37e0a..c8c4da6c 100644 --- a/paramiko/ed25519key.py +++ b/paramiko/ed25519key.py @@ -23,7 +23,7 @@ import nacl.signing from paramiko.message import Message from paramiko.pkey import PKey, OPENSSH_AUTH_MAGIC, _unpad_openssh -from paramiko.py3compat import b +from paramiko.util import b from paramiko.ssh_exception import SSHException, PasswordRequiredException diff --git a/paramiko/file.py b/paramiko/file.py index eeb2b8c6..51a6d409 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -24,9 +24,9 @@ from paramiko.common import ( linefeed_byte, cr_byte_value, ) -from paramiko.py3compat import u, bytes_types, text_type +from paramiko.py3compat import bytes_types, text_type -from paramiko.util import ClosingContextManager +from paramiko.util import ClosingContextManager, u class BufferedFile(ClosingContextManager): diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index f1b4a936..91877c25 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -28,11 +28,11 @@ else: from hashlib import sha1 from hmac import HMAC -from paramiko.py3compat import b, u, encodebytes, decodebytes +from paramiko.py3compat import encodebytes, decodebytes from paramiko.dsskey import DSSKey from paramiko.rsakey import RSAKey -from paramiko.util import get_logger, constant_time_bytes_eq +from paramiko.util import get_logger, constant_time_bytes_eq, b, u from paramiko.ecdsakey import ECDSAKey from paramiko.ed25519key import Ed25519Key from paramiko.ssh_exception import SSHException diff --git a/paramiko/message.py b/paramiko/message.py index 2b5cfb0e..b555e2cd 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -25,7 +25,7 @@ from io import BytesIO from paramiko import util from paramiko.common import zero_byte, max_byte, one_byte -from paramiko.py3compat import u +from paramiko.util import u class Message(object): diff --git a/paramiko/packet.py b/paramiko/packet.py index 914e8971..a8db2582 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -38,8 +38,7 @@ from paramiko.common import ( zero_byte, byte_ord, ) -from paramiko.py3compat import u -from paramiko.util import asbytes +from paramiko.util import asbytes, u from paramiko.ssh_exception import SSHException, ProxyCommandFailure from paramiko.message import Message diff --git a/paramiko/pkey.py b/paramiko/pkey.py index 0566efc0..254f8086 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -35,8 +35,9 @@ from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.ciphers import algorithms, modes, Cipher from paramiko import util +from paramiko.util import u, b from paramiko.common import o600 -from paramiko.py3compat import u, b, encodebytes, decodebytes, string_types +from paramiko.py3compat import encodebytes, decodebytes, string_types from paramiko.ssh_exception import SSHException, PasswordRequiredException from paramiko.message import Message diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 5bb86e54..d4396399 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -28,7 +28,6 @@ from paramiko import util from paramiko.channel import Channel from paramiko.message import Message from paramiko.common import INFO, DEBUG, o777 -from paramiko.py3compat import b, u from paramiko.sftp import ( BaseSFTP, CMD_OPENDIR, @@ -66,7 +65,7 @@ from paramiko.sftp import ( from paramiko.sftp_attr import SFTPAttributes from paramiko.ssh_exception import SSHException from paramiko.sftp_file import SFTPFile -from paramiko.util import ClosingContextManager +from paramiko.util import ClosingContextManager, b, u def _to_unicode(s): diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 74255708..0975cba3 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -30,7 +30,7 @@ import time from paramiko.common import DEBUG from paramiko.file import BufferedFile -from paramiko.py3compat import u +from paramiko.util import u from paramiko.sftp import ( CMD_CLOSE, CMD_READ, diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 34cb85cf..b93c8168 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -36,8 +36,9 @@ from paramiko.sftp import ( from paramiko.sftp_si import SFTPServerInterface from paramiko.sftp_attr import SFTPAttributes from paramiko.common import DEBUG -from paramiko.py3compat import string_types, bytes_types, b +from paramiko.py3compat import string_types, bytes_types from paramiko.server import SubsystemHandler +from paramiko.util import b # known hash algorithms for the "check-file" extension diff --git a/paramiko/transport.py b/paramiko/transport.py index fae1babb..6abfe5d8 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -101,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, b, input +from paramiko.py3compat import string_types, input from paramiko.rsakey import RSAKey from paramiko.ecdsakey import ECDSAKey from paramiko.server import ServerInterface @@ -113,7 +113,7 @@ from paramiko.ssh_exception import ( IncompatiblePeer, ProxyCommandFailure, ) -from paramiko.util import retry_on_signal, ClosingContextManager, clamp_value +from paramiko.util import retry_on_signal, ClosingContextManager, clamp_value, b # for thread cleanup diff --git a/paramiko/util.py b/paramiko/util.py index a9865ec3..5e2f6392 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -318,3 +318,24 @@ def asbytes(s): # byte-y or buffer-y that everything will work out (or that callers # are capable of handling whatever it is.) return s + + +# TODO: clean this up / force callers to assume bytes OR unicode +def b(s, encoding="utf8"): + """cast unicode or bytes to bytes""" + if isinstance(s, bytes): + return s + elif isinstance(s, str): + return s.encode(encoding) + else: + raise TypeError("Expected unicode or bytes, got {!r}".format(s)) + +# TODO: clean this up / force callers to assume bytes OR unicode +def u(s, encoding="utf8"): + """cast bytes or unicode to unicode""" + if isinstance(s, bytes): + return s.decode(encoding) + elif isinstance(s, str): + return s + else: + raise TypeError("Expected unicode or bytes, got {!r}".format(s)) diff --git a/paramiko/win_pageant.py b/paramiko/win_pageant.py index b733d813..93f74621 100644 --- a/paramiko/win_pageant.py +++ b/paramiko/win_pageant.py @@ -26,7 +26,7 @@ import ctypes.wintypes import platform import struct from paramiko.common import zero_byte -from paramiko.py3compat import b +from paramiko.util import b try: import _thread as thread # Python 3.x diff --git a/tests/test_agent.py b/tests/test_agent.py index c3973bbb..01602d0e 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -8,7 +8,7 @@ from paramiko.agent import ( SSH_AGENT_RSA_SHA2_512, AgentKey, ) -from paramiko.py3compat import b +from paramiko.util import b class ChaosAgent: diff --git a/tests/test_auth.py b/tests/test_auth.py index 0f0a6169..4a960deb 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -35,7 +35,7 @@ from paramiko import ( AuthenticationException, ) from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL -from paramiko.py3compat import u +from paramiko.util import u from .loop import LoopSocket from .util import _support, slow diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 1ed021f9..604bbfa6 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -37,7 +37,8 @@ from paramiko import ( util, SSHException, ) -from paramiko.py3compat import b, bytes +from paramiko.py3compat import bytes +from paramiko.util import b from paramiko.common import o600, byte_chr from cryptography.exceptions import UnsupportedAlgorithm |