[Python-checkins] cpython: #13406: fix more deprecation warnings and move the deprecation of

ezio.melotti python-checkins at python.org
Thu Nov 17 11:23:42 CET 2011


http://hg.python.org/cpython/rev/fe2be7d35660
changeset:   73600:fe2be7d35660
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Thu Nov 17 12:23:34 2011 +0200
summary:
  #13406: fix more deprecation warnings and move the deprecation of unicode-internal earlier in the code.

files:
  Lib/test/test_codeccallbacks.py |  57 +++++++++++---------
  Lib/test/test_codecs.py         |  14 ++--
  Modules/_codecsmodule.c         |  10 +-
  3 files changed, 43 insertions(+), 38 deletions(-)


diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py
--- a/Lib/test/test_codeccallbacks.py
+++ b/Lib/test/test_codeccallbacks.py
@@ -205,33 +205,37 @@
         self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
 
     def test_decodeunicodeinternal(self):
-        self.assertRaises(
-            UnicodeDecodeError,
-            b"\x00\x00\x00\x00\x00".decode,
-            "unicode-internal",
-        )
+        with test.support.check_warnings(('unicode_internal codec has been '
+                                          'deprecated', DeprecationWarning)):
+            self.assertRaises(
+                UnicodeDecodeError,
+                b"\x00\x00\x00\x00\x00".decode,
+                "unicode-internal",
+            )
         if SIZEOF_WCHAR_T == 4:
             def handler_unicodeinternal(exc):
                 if not isinstance(exc, UnicodeDecodeError):
                     raise TypeError("don't know how to handle %r" % exc)
                 return ("\x01", 1)
 
-            self.assertEqual(
-                b"\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"),
-                "\u0000"
-            )
+            with test.support.check_warnings(('unicode_internal codec has been '
+                                              'deprecated', DeprecationWarning)):
+                self.assertEqual(
+                    b"\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"),
+                    "\u0000"
+                )
 
-            self.assertEqual(
-                b"\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"),
-                "\u0000\ufffd"
-            )
+                self.assertEqual(
+                    b"\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"),
+                    "\u0000\ufffd"
+                )
 
-            codecs.register_error("test.hui", handler_unicodeinternal)
+                codecs.register_error("test.hui", handler_unicodeinternal)
 
-            self.assertEqual(
-                b"\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"),
-                "\u0000\u0001\u0000"
-            )
+                self.assertEqual(
+                    b"\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"),
+                    "\u0000\u0001\u0000"
+                )
 
     def test_callbacks(self):
         def handler1(exc):
@@ -626,9 +630,8 @@
                 ("utf-7", b"+x-"),
                 ("unicode-internal", b"\x00"),
             ):
-                with warnings.catch_warnings():
+                with test.support.check_warnings():
                     # unicode-internal has been deprecated
-                    warnings.simplefilter("ignore", DeprecationWarning)
                     self.assertRaises(
                         TypeError,
                         bytes.decode,
@@ -850,11 +853,12 @@
             else:
                 raise TypeError("don't know how to handle %r" % exc)
         codecs.register_error("test.replacing", replacing)
-        with warnings.catch_warnings():
+
+        with test.support.check_warnings():
             # unicode-internal has been deprecated
-            warnings.simplefilter("ignore", DeprecationWarning)
             for (encoding, data) in baddata:
-                self.assertRaises(TypeError, data.decode, encoding, "test.replacing")
+                with self.assertRaises(TypeError):
+                    data.decode(encoding, "test.replacing")
 
         def mutating(exc):
             if isinstance(exc, UnicodeDecodeError):
@@ -865,8 +869,11 @@
         codecs.register_error("test.mutating", mutating)
         # If the decoder doesn't pick up the modified input the following
         # will lead to an endless loop
-        for (encoding, data) in baddata:
-            self.assertRaises(TypeError, data.decode, encoding, "test.replacing")
+        with test.support.check_warnings():
+            # unicode-internal has been deprecated
+            for (encoding, data) in baddata:
+                with self.assertRaises(TypeError):
+                    data.decode(encoding, "test.replacing")
 
 def test_main():
     test.support.run_unittest(CodecCallbackTest)
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -1062,10 +1062,8 @@
         self.assertEqual(("ab", 12), ignored)
 
     def test_encode_length(self):
-        with warnings.catch_warnings():
-            # unicode-internal has been deprecated
-            warnings.simplefilter("ignore", DeprecationWarning)
-
+        with support.check_warnings(('unicode_internal codec has been '
+                                     'deprecated', DeprecationWarning)):
             # Issue 3739
             encoder = codecs.getencoder("unicode_internal")
             self.assertEqual(encoder("a")[1], 1)
@@ -1528,10 +1526,8 @@
                 name = "latin_1"
             self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-"))
 
-            with warnings.catch_warnings():
+            with support.check_warnings():
                 # unicode-internal has been deprecated
-                warnings.simplefilter("ignore", DeprecationWarning)
-
                 (b, size) = codecs.getencoder(encoding)(s)
                 self.assertEqual(size, len(s), "%r != %r (encoding=%r)" % (size, len(s), encoding))
                 (chars, size) = codecs.getdecoder(encoding)(b)
@@ -1639,7 +1635,9 @@
     def test_bad_encode_args(self):
         for encoding in all_unicode_encodings:
             encoder = codecs.getencoder(encoding)
-            self.assertRaises(TypeError, encoder)
+            with support.check_warnings():
+                # unicode-internal has been deprecated
+                self.assertRaises(TypeError, encoder)
 
     def test_encoding_map_type_initialized(self):
         from encodings import cp1140
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -677,6 +677,11 @@
     const char *data;
     Py_ssize_t len, size;
 
+    if (PyErr_WarnEx(PyExc_DeprecationWarning,
+                     "unicode_internal codec has been deprecated",
+                     1))
+        return NULL;
+
     if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
                           &obj, &errors))
         return NULL;
@@ -687,11 +692,6 @@
         if (PyUnicode_READY(obj) < 0)
             return NULL;
 
-        if (PyErr_WarnEx(PyExc_DeprecationWarning,
-                         "unicode_internal codec has been deprecated",
-                         1))
-            return NULL;
-
         u = PyUnicode_AsUnicodeAndSize(obj, &len);
         if (u == NULL)
             return NULL;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list