summaryrefslogtreecommitdiff
path: root/paramiko/util.py
diff options
context:
space:
mode:
authorJeff Forcier <jeff@bitprophet.org>2023-01-09 22:47:21 -0500
committerJeff Forcier <jeff@bitprophet.org>2023-01-09 23:26:00 -0500
commit9e83ca9d8acb5e9d41d45d46a0339e2f633e2dea (patch)
tree6cb926091b66800f98e75bff931db836bda7a4be /paramiko/util.py
parent7b8106e67c51e71fc4074f7851bbaebed4d5be0c (diff)
downloadparamiko-9e83ca9d8acb5e9d41d45d46a0339e2f633e2dea.tar.gz
Move b, u helpers to util module for now
Later on we can likely nuke many uses of these
Diffstat (limited to 'paramiko/util.py')
-rw-r--r--paramiko/util.py21
1 files changed, 21 insertions, 0 deletions
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))