Issue #19543: Emit deprecation warning for known non-text encodings.
Backported issues #19619: encode() and decode() methods and constructors
of str, unicode and bytearray classes now emit deprecation warning for known
non-text encodings when Python is ran with the -3 option.
Backported issues #20404: io.TextIOWrapper (and hence io.open()) now uses the
internal codec marking system added to emit deprecation warning for known non-text
encodings at stream construction time when Python is ran with the -3 option.
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index a7f4301..694b778 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -7,6 +7,7 @@
import os
import abc
import codecs
+import sys
import warnings
import errno
# Import thread instead of threading to reduce startup cost
@@ -1497,6 +1498,11 @@
if not isinstance(encoding, basestring):
raise ValueError("invalid encoding: %r" % encoding)
+ if sys.py3kwarning and not codecs.lookup(encoding)._is_text_encoding:
+ msg = ("%r is not a text encoding; "
+ "use codecs.open() to handle arbitrary codecs")
+ warnings.warnpy3k(msg % encoding, stacklevel=2)
+
if errors is None:
errors = "strict"
else: