summaryrefslogtreecommitdiff
path: root/passlib/utils
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-06-20 12:39:17 -0400
committerEli Collins <elic@assurancetechnologies.com>2011-06-20 12:39:17 -0400
commit1456ccd435d6b71950c2c0aab148938e09db6651 (patch)
tree41f77153bf8b5bb0494e7a3ea4b530afd5e259f7 /passlib/utils
parent0ad4f023487be628c7d163aa1e2c7775d3d94b5f (diff)
downloadpasslib-1456ccd435d6b71950c2c0aab148938e09db6651.tar.gz
apache module rewritten for py3 compat
* added 'encoding' kwd to Htpasswd, Htdigest constructors, allowing user/realm encoding to be specified. * treats file as bytes internally * added UTs for encoding-specific behavior * added render_bytes() util helper - py2/3 compatible replacement for using % formatting with bytes
Diffstat (limited to 'passlib/utils')
-rw-r--r--passlib/utils/__init__.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index e0c683f..e1ffc34 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -488,6 +488,26 @@ def bjoin_ints(values):
#bjoin_ints = bytes
# end Py3k #
+def render_bytes(source, *args):
+ """helper for using formatting operator with bytes.
+
+ this function is motivated by the fact that
+ :class:`bytes` instances do not support % or {} formatting under python 3.
+ this function is an attempt to provide a replacement
+ that will work uniformly under python 2 & 3.
+
+ it converts everything to unicode (including bytes arguments),
+ then encodes the result to latin-1.
+ """
+ if isinstance(source, bytes):
+ source = source.decode("latin-1")
+ def adapt(arg):
+ if isinstance(arg, bytes):
+ return arg.decode("latin-1")
+ return arg
+ result = source % tuple(adapt(arg) for arg in args)
+ return result.encode("latin-1")
+
#=================================================================================
#numeric helpers
#=================================================================================