diff -r c2f1bb56760d Doc/library/codecs.rst --- a/Doc/library/codecs.rst Sun Nov 17 15:59:51 2013 +1000 +++ b/Doc/library/codecs.rst Sun Nov 17 17:36:28 2013 +1000 @@ -1223,62 +1223,66 @@ | | | .. deprecated:: 3.3 | +--------------------+---------+---------------------------+ -The following codecs provide :term:`bytes-like object` to :class:`bytes` -mappings. +The following codecs provide binary transforms: :term:`bytes-like object` +to :class:`bytes` mappings. -.. tabularcolumns:: |l|L|L| +.. tabularcolumns:: |l|L|L|L| -+----------------------+------------------------------+------------------------------+ -| Codec | Purpose | Encoder / decoder | -+======================+==============================+==============================+ -| base64_codec [#b64]_ | Convert operand to MIME | :meth:`base64.b64encode` / | -| | base64 (the result always | :meth:`base64.b64decode` | -| | includes a trailing | | -| | ``'\n'``) | | -| | | | -| | .. versionchanged:: 3.4 | | -| | accepts any | | -| | :term:`bytes-like object` | | -| | as input for encoding and | | -| | decoding | | -+----------------------+------------------------------+------------------------------+ -| bz2_codec | Compress the operand | :meth:`bz2.compress` / | -| | using bz2 | :meth:`bz2.decompress` | -+----------------------+------------------------------+------------------------------+ -| hex_codec | Convert operand to | :meth:`base64.b16encode` / | -| | hexadecimal | :meth:`base64.b16decode` | -| | representation, with two | | -| | digits per byte | | -+----------------------+------------------------------+------------------------------+ -| quopri_codec | Convert operand to MIME | :meth:`quopri.encodestring` /| -| | quoted printable | :meth:`quopri.decodestring` | -+----------------------+------------------------------+------------------------------+ -| uu_codec | Convert the operand using | :meth:`uu.encode` / | -| | uuencode | :meth:`uu.decode` | -+----------------------+------------------------------+------------------------------+ -| zlib_codec | Compress the operand | :meth:`zlib.compress` / | -| | using gzip | :meth:`zlib.decompress` | -+----------------------+------------------------------+------------------------------+ ++----------------------+------------------+------------------------------+------------------------------+ +| Codec | Aliases | Purpose | Encoder / decoder | ++======================+==================+==============================+==============================+ +| base64_codec [#b64]_ | base64, base_64 | Convert operand to MIME | :meth:`base64.b64encode` / | +| | | base64 (the result always | :meth:`base64.b64decode` | +| | | includes a trailing | | +| | | ``'\n'``) | | +| | | | | +| | | .. versionchanged:: 3.4 | | +| | | accepts any | | +| | | :term:`bytes-like object` | | +| | | as input for encoding and | | +| | | decoding | | ++----------------------+------------------+------------------------------+------------------------------+ +| bz2_codec | bz2 | Compress the operand | :meth:`bz2.compress` / | +| | | using bz2 | :meth:`bz2.decompress` | ++----------------------+------------------+------------------------------+------------------------------+ +| hex_codec | hex | Convert operand to | :meth:`base64.b16encode` / | +| | | hexadecimal | :meth:`base64.b16decode` | +| | | representation, with two | | +| | | digits per byte | | ++----------------------+------------------+------------------------------+------------------------------+ +| quopri_codec | quopri, | Convert operand to MIME | :meth:`quopri.encodestring` /| +| | quotedprintable, | quoted printable | :meth:`quopri.decodestring` | +| | quoted_printable | | | ++----------------------+------------------+------------------------------+------------------------------+ +| uu_codec | uu | Convert the operand using | :meth:`uu.encode` / | +| | | uuencode | :meth:`uu.decode` | ++----------------------+------------------+------------------------------+------------------------------+ +| zlib_codec | zip, zlib | Compress the operand | :meth:`zlib.compress` / | +| | | using gzip | :meth:`zlib.decompress` | ++----------------------+------------------+------------------------------+------------------------------+ .. [#b64] In addition to :term:`bytes-like objects `, ``'base64_codec'`` also accepts ASCII-only instances of :class:`str` for decoding +The following codec provides a text transform: a :class:`str` to :class:`str` +mapping. -The following codecs provide :class:`str` to :class:`str` mappings. +.. tabularcolumns:: |l|l|L| -.. tabularcolumns:: |l|L| - -+--------------------+---------------------------+ -| Codec | Purpose | -+====================+===========================+ -| rot_13 | Returns the Caesar-cypher | -| | encryption of the operand | -+--------------------+---------------------------+ ++--------------------+---------+---------------------------+ +| Codec | Aliases | Purpose | ++====================+=========+===========================+ +| rot_13 | rot13 | Returns the Caesar-cypher | +| | | encryption of the operand | ++--------------------+---------+---------------------------+ .. versionadded:: 3.2 - bytes-to-bytes and str-to-str codecs. + Restoration of the binary and text transforms. + +.. versionchanged:: 3.4 + Restoration of the aliases for the binary and text transforms. :mod:`encodings.idna` --- Internationalized Domain Names in Applications diff -r c2f1bb56760d Doc/whatsnew/3.4.rst --- a/Doc/whatsnew/3.4.rst Sun Nov 17 15:59:51 2013 +1000 +++ b/Doc/whatsnew/3.4.rst Sun Nov 17 17:36:28 2013 +1000 @@ -214,25 +214,38 @@ will be wrapped in a chained exception of the same type that mentions the name of the codec responsible for producing the error:: - >>> b"hello".decode("uu_codec") + >>> b"hello".decode("uu") ValueError: Missing "begin" line in input data The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 1, in - ValueError: decoding with 'uu_codec' codec failed (ValueError: Missing "begin" line in input data) + ValueError: decoding with 'uu' codec failed (ValueError: Missing "begin" line in input data) - >>> "hello".encode("bz2_codec") + >>> "hello".encode("bz2") TypeError: 'str' does not support the buffer interface The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 1, in - TypeError: encoding with 'bz2_codec' codec failed (TypeError: 'str' does not support the buffer interface) + TypeError: encoding with 'bz2' codec failed (TypeError: 'str' does not support the buffer interface) -(Contributed by Nick Coghlan in :issue:`17827` and :issue:`17828`) +Finally, as the example above shows, these changes mean we now feel +comfortable with restoring the convenience aliases for the non-Unicode +codecs that were themselves restored in Python 3.2. This means that encoding +binary data to and from its hexadecimal representation can now be written +as:: + + >>> from codecs import encode, decode + >>> encode(b"hello", "hex") + b'68656c6c6f' + >>> decode(b"68656c6c6f", "hex") + b'hello' + +(Contributed by Nick Coghlan in :issue:`7475`, :issue:`17827` and +:issue:`17828`) Other Language Changes diff -r c2f1bb56760d Lib/encodings/aliases.py --- a/Lib/encodings/aliases.py Sun Nov 17 15:59:51 2013 +1000 +++ b/Lib/encodings/aliases.py Sun Nov 17 17:36:28 2013 +1000 @@ -33,9 +33,9 @@ 'us' : 'ascii', 'us_ascii' : 'ascii', - ## base64_codec codec - #'base64' : 'base64_codec', - #'base_64' : 'base64_codec', + # base64_codec codec + 'base64' : 'base64_codec', + 'base_64' : 'base64_codec', # big5 codec 'big5_tw' : 'big5', @@ -45,8 +45,8 @@ 'big5_hkscs' : 'big5hkscs', 'hkscs' : 'big5hkscs', - ## bz2_codec codec - #'bz2' : 'bz2_codec', + # bz2_codec codec + 'bz2' : 'bz2_codec', # cp037 codec '037' : 'cp037', @@ -248,8 +248,8 @@ 'cp936' : 'gbk', 'ms936' : 'gbk', - ## hex_codec codec - #'hex' : 'hex_codec', + # hex_codec codec + 'hex' : 'hex_codec', # hp_roman8 codec 'roman8' : 'hp_roman8', @@ -450,13 +450,13 @@ 'cp154' : 'ptcp154', 'cyrillic_asian' : 'ptcp154', - ## quopri_codec codec - #'quopri' : 'quopri_codec', - #'quoted_printable' : 'quopri_codec', - #'quotedprintable' : 'quopri_codec', + # quopri_codec codec + 'quopri' : 'quopri_codec', + 'quoted_printable' : 'quopri_codec', + 'quotedprintable' : 'quopri_codec', - ## rot_13 codec - #'rot13' : 'rot_13', + # rot_13 codec + 'rot13' : 'rot_13', # shift_jis codec 'csshiftjis' : 'shift_jis', @@ -518,12 +518,12 @@ 'utf8_ucs2' : 'utf_8', 'utf8_ucs4' : 'utf_8', - ## uu_codec codec - #'uu' : 'uu_codec', + # uu_codec codec + 'uu' : 'uu_codec', - ## zlib_codec codec - #'zip' : 'zlib_codec', - #'zlib' : 'zlib_codec', + # zlib_codec codec + 'zip' : 'zlib_codec', + 'zlib' : 'zlib_codec', # temporary mac CJK aliases, will be replaced by proper codecs in 3.1 'x_mac_japanese' : 'shift_jis', diff -r c2f1bb56760d Lib/test/test_codecs.py --- a/Lib/test/test_codecs.py Sun Nov 17 15:59:51 2013 +1000 +++ b/Lib/test/test_codecs.py Sun Nov 17 17:36:28 2013 +1000 @@ -2275,18 +2275,29 @@ "quopri_codec", "hex_codec", ] + +transform_aliases = { + "base64_codec": ["base64", "base_64"], + "uu_codec": ["uu"], + "quopri_codec": ["quopri", "quoted_printable", "quotedprintable"], + "hex_codec": ["hex"], + "rot_13": ["rot13"], +} + try: import zlib except ImportError: pass else: bytes_transform_encodings.append("zlib_codec") + transform_aliases["zlib_codec"] = ["zip", "zlib"] try: import bz2 except ImportError: pass else: bytes_transform_encodings.append("bz2_codec") + transform_aliases["bz2_codec"] = ["bz2"] class TransformCodecTest(unittest.TestCase): @@ -2380,6 +2391,15 @@ with self.assertRaisesRegex(TypeError, msg): "just an example message".encode("rot_13") + # Ensure codec aliases from http://bugs.python.org/issue7475 work + def test_aliases(self): + for codec_name, aliases in transform_aliases.items(): + expected_name = codecs.lookup(codec_name).name + for alias in aliases: + with self.subTest(alias=alias): + info = codecs.lookup(alias) + self.assertEqual(info.name, expected_name) + # The codec system tries to wrap exceptions in order to ensure the error # mentions the operation being performed and the codec involved. We