summaryrefslogtreecommitdiffstats
path: root/Lib/encodings/hex_codec.py
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2001-05-15 12:00:02 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2001-05-15 12:00:02 (GMT)
commit2d9204199fe8913cca9890f1822413d981587ee5 (patch)
treef0734f9c8721508ebbd472cbc46abd9aa66c44dd /Lib/encodings/hex_codec.py
parent2e0a654f6edeb58bef3cccffa42c2a236117a88c (diff)
downloadcpython-2d9204199fe8913cca9890f1822413d981587ee5.zip
cpython-2d9204199fe8913cca9890f1822413d981587ee5.tar.gz
cpython-2d9204199fe8913cca9890f1822413d981587ee5.tar.bz2
This patch changes the way the string .encode() method works slightly
and introduces a new method .decode(). The major change is that strg.encode() will no longer try to convert Unicode returns from the codec into a string, but instead pass along the Unicode object as-is. The same is now true for all other codec return types. The underlying C APIs were changed accordingly. Note that even though this does have the potential of breaking existing code, the chances are low since conversion from Unicode previously took place using the default encoding which is normally set to ASCII rendering this auto-conversion mechanism useless for most Unicode encodings. The good news is that you can now use .encode() and .decode() with much greater ease and that the door was opened for better accessibility of the builtin codecs. As demonstration of the new feature, the patch includes a few new codecs which allow string to string encoding and decoding (rot13, hex, zip, uu, base64). Written by Marc-Andre Lemburg. Copyright assigned to the PSF.
Diffstat (limited to 'Lib/encodings/hex_codec.py')
-rw-r--r--Lib/encodings/hex_codec.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/Lib/encodings/hex_codec.py b/Lib/encodings/hex_codec.py
new file mode 100644
index 0000000..ab7d86f
--- /dev/null
+++ b/Lib/encodings/hex_codec.py
@@ -0,0 +1,60 @@
+""" Python 'hex_codec' Codec - 2-digit hex content transfer encoding
+
+ Unlike most of the other codecs which target Unicode, this codec
+ will return Python string objects for both encode and decode.
+
+ Written by Marc-Andre Lemburg (mal@lemburg.com).
+
+"""
+import codecs, binascii
+
+### Codec APIs
+
+def hex_encode(input,errors='strict'):
+
+ """ Encodes the object input and returns a tuple (output
+ object, length consumed).
+
+ errors defines the error handling to apply. It defaults to
+ 'strict' handling which is the only currently supported
+ error handling for this codec.
+
+ """
+ assert errors == 'strict'
+ output = binascii.b2a_hex(input)
+ return (output, len(input))
+
+def hex_decode(input,errors='strict'):
+
+ """ Decodes the object input and returns a tuple (output
+ object, length consumed).
+
+ input must be an object which provides the bf_getreadbuf
+ buffer slot. Python strings, buffer objects and memory
+ mapped files are examples of objects providing this slot.
+
+ errors defines the error handling to apply. It defaults to
+ 'strict' handling which is the only currently supported
+ error handling for this codec.
+
+ """
+ assert errors == 'strict'
+ output = binascii.a2b_hex(input)
+ return (output, len(input))
+
+class Codec(codecs.Codec):
+
+ encode = hex_encode
+ decode = hex_decode
+
+class StreamWriter(Codec,codecs.StreamWriter):
+ pass
+
+class StreamReader(Codec,codecs.StreamReader):
+ pass
+
+### encodings module API
+
+def getregentry():
+
+ return (hex_encode,hex_decode,StreamReader,StreamWriter)