diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-02-24 14:43:03 +0200 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-02-24 14:43:03 +0200 |
commit | 94ee389308ec9e0e07b3f7a944d5179aba540c5e (patch) | |
tree | 80bc231aff27723119beacbcfa2654b90f793060 /Lib/codecs.py | |
parent | 20f8728bf0cce877c1908b15ddc59e2d1011ad0f (diff) | |
download | cpython-94ee389308ec9e0e07b3f7a944d5179aba540c5e.tar.gz cpython-94ee389308ec9e0e07b3f7a944d5179aba540c5e.zip |
Issue #19619: Blacklist non-text codecs in method API
str.encode, bytes.decode and bytearray.decode now use an
internal API to throw LookupError for known non-text encodings,
rather than attempting the encoding or decoding operation and
then throwing a TypeError for an unexpected output type.
The latter mechanism remains in place for third party non-text
encodings.
Backported changeset d68df99d7a57.
Diffstat (limited to 'Lib/codecs.py')
-rw-r--r-- | Lib/codecs.py | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/Lib/codecs.py b/Lib/codecs.py index 01ae0f3ea63..c2065dafa27 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -73,9 +73,19 @@ BOM64_BE = BOM_UTF32_BE ### Codec base classes (defining the API) class CodecInfo(tuple): + """Codec details when looking up the codec registry""" + + # Private API to allow Python 3.4 to blacklist the known non-Unicode + # codecs in the standard library. A more general mechanism to + # reliably distinguish test encodings from other codecs will hopefully + # be defined for Python 3.5 + # + # See http://bugs.python.org/issue19619 + _is_text_encoding = True # Assume codecs are text encodings by default def __new__(cls, encode, decode, streamreader=None, streamwriter=None, - incrementalencoder=None, incrementaldecoder=None, name=None): + incrementalencoder=None, incrementaldecoder=None, name=None, + *, _is_text_encoding=None): self = tuple.__new__(cls, (encode, decode, streamreader, streamwriter)) self.name = name self.encode = encode @@ -84,6 +94,8 @@ class CodecInfo(tuple): self.incrementaldecoder = incrementaldecoder self.streamwriter = streamwriter self.streamreader = streamreader + if _is_text_encoding is not None: + self._is_text_encoding = _is_text_encoding return self def __repr__(self): |