]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-80480: Emit DeprecationWarning for array's 'u' type code (#95760)
authorHugo van Kemenade <hugovk@users.noreply.github.com>
Sun, 11 Jun 2023 09:17:35 +0000 (12:17 +0300)
committerGitHub <noreply@github.com>
Sun, 11 Jun 2023 09:17:35 +0000 (03:17 -0600)
Doc/library/array.rst
Doc/whatsnew/3.13.rst
Lib/test/test_array.py
Lib/test/test_buffer.py
Lib/test/test_re.py
Misc/NEWS.d/next/Library/2022-08-07-11-10-26.gh-issue-80480.IFccj3.rst [new file with mode: 0644]
Modules/arraymodule.c

index 1f5810b35d2d86e2f1901d7a2cdb714964b2509c..0afc217642a75678e5585b26f5a38946b6d9c4bf 100644 (file)
@@ -57,7 +57,7 @@ Notes:
       ``Py_UNICODE``. This change doesn't affect its behavior because
       ``Py_UNICODE`` is alias of ``wchar_t`` since Python 3.3.
 
-   .. deprecated-removed:: 3.3 4.0
+   .. deprecated-removed:: 3.3 3.16
       Please migrate to ``'w'`` typecode.
 
 
index e3090f1fb7f51a804f489aedf117417aed119416..e6504b0152acc2f0aca1a57c1d790060008d32ff 100644 (file)
@@ -134,6 +134,11 @@ Deprecated
   They will be removed in Python 3.15.
   (Contributed by Victor Stinner in :gh:`105096`.)
 
+* :mod:`array`'s ``'u'`` format code, deprecated in docs since Python 3.3,
+  emits :exc:`DeprecationWarning` since 3.13
+  and will be removed in Python 3.16.
+  Use the ``'w'`` format code instead.
+  (contributed by Hugo van Kemenade in :gh:`80480`)
 
 Removed
 =======
index a94d04f6515e2797e0a3081b498f8590864c9871..f6bf9e6c5ea871cf9efe89d09fd04c989773d48c 100755 (executable)
@@ -13,11 +13,14 @@ import pickle
 import operator
 import struct
 import sys
+import warnings
 
 import array
 from array import _array_reconstructor as array_reconstructor
 
-sizeof_wchar = array.array('u').itemsize
+with warnings.catch_warnings():
+    warnings.simplefilter('ignore', DeprecationWarning)
+    sizeof_wchar = array.array('u').itemsize
 
 
 class ArraySubclass(array.array):
@@ -93,8 +96,16 @@ UTF16_BE = 19
 UTF32_LE = 20
 UTF32_BE = 21
 
+
 class ArrayReconstructorTest(unittest.TestCase):
 
+    def setUp(self):
+        warnings.filterwarnings(
+            "ignore",
+            message="The 'u' type code is deprecated and "
+                    "will be removed in Python 3.16",
+            category=DeprecationWarning)
+
     def test_error(self):
         self.assertRaises(TypeError, array_reconstructor,
                           "", "b", 0, b"")
@@ -1208,10 +1219,16 @@ class UnicodeTest(StringTest, unittest.TestCase):
         self.assertRaises(ValueError, a.tounicode)
         self.assertRaises(ValueError, str, a)
 
+    def test_typecode_u_deprecation(self):
+        with self.assertWarns(DeprecationWarning):
+            array.array("u")
+
+
 class UCS4Test(UnicodeTest):
     typecode = 'w'
     minitemsize = 4
 
+
 class NumberTest(BaseTest):
 
     def test_extslice(self):
index 94fc9d4436b717bed552a2a3d6172fb822f33acf..8d6902e004689b0b5663a41c454d947239279c13 100644 (file)
@@ -24,6 +24,7 @@ import warnings
 import sys, array, io, os
 from decimal import Decimal
 from fractions import Fraction
+from test.support import warnings_helper
 
 try:
     from _testbuffer import *
@@ -3217,12 +3218,6 @@ class TestBufferProtocol(unittest.TestCase):
         nd[0] = (-1, float('nan'))
         self.assertNotEqual(memoryview(nd), nd)
 
-        # Depends on issue #15625: the struct module does not understand 'u'.
-        a = array.array('u', 'xyz')
-        v = memoryview(a)
-        self.assertNotEqual(a, v)
-        self.assertNotEqual(v, a)
-
         # Some ctypes format strings are unknown to the struct module.
         if ctypes:
             # format: "T{>l:x:>l:y:}"
@@ -3236,6 +3231,15 @@ class TestBufferProtocol(unittest.TestCase):
             self.assertNotEqual(point, a)
             self.assertRaises(NotImplementedError, a.tolist)
 
+    @warnings_helper.ignore_warnings(category=DeprecationWarning)  # gh-80480 array('u')
+    def test_memoryview_compare_special_cases_deprecated_u_type_code(self):
+
+        # Depends on issue #15625: the struct module does not understand 'u'.
+        a = array.array('u', 'xyz')
+        v = memoryview(a)
+        self.assertNotEqual(a, v)
+        self.assertNotEqual(v, a)
+
     def test_memoryview_compare_ndim_zero(self):
 
         nd1 = ndarray(1729, shape=[], format='@L')
index 11628a236ade9a3b72989420dc7d7945df99e62b..d1575dc2c347856ca5f0fea288d6d6351209d14e 100644 (file)
@@ -1,7 +1,7 @@
 from test.support import (gc_collect, bigmemtest, _2G,
                           cpython_only, captured_stdout,
                           check_disallow_instantiation, is_emscripten, is_wasi,
-                          SHORT_TIMEOUT)
+                          warnings_helper, SHORT_TIMEOUT)
 import locale
 import re
 import string
@@ -1522,10 +1522,11 @@ class ReTests(unittest.TestCase):
         for x in not_decimal_digits:
             self.assertIsNone(re.match(r'^\d$', x))
 
+    @warnings_helper.ignore_warnings(category=DeprecationWarning)  # gh-80480 array('u')
     def test_empty_array(self):
         # SF buf 1647541
         import array
-        for typecode in 'bBuhHiIlLfd':
+        for typecode in 'bBhuwHiIlLfd':
             a = array.array(typecode)
             self.assertIsNone(re.compile(b"bla").match(a))
             self.assertEqual(re.compile(b"").match(a).groups(), ())
diff --git a/Misc/NEWS.d/next/Library/2022-08-07-11-10-26.gh-issue-80480.IFccj3.rst b/Misc/NEWS.d/next/Library/2022-08-07-11-10-26.gh-issue-80480.IFccj3.rst
new file mode 100644 (file)
index 0000000..2d4956f
--- /dev/null
@@ -0,0 +1,2 @@
+Emit :exc:`DeprecationWarning` for :mod:`array`'s ``'u'`` type code,
+deprecated in docs since Python 3.3.
index 16e3739eb26fc31ca8669cf5553b9aae7ccb3cc7..1a5993819b2e13dc6a42974fd15586f0eff1bc8b 100644 (file)
@@ -2679,6 +2679,15 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
+    if (c == 'u') {
+        if (PyErr_WarnEx(PyExc_DeprecationWarning,
+                         "The 'u' type code is deprecated and "
+                         "will be removed in Python 3.16",
+                         1)) {
+            return NULL;
+        }
+    }
+
     bool is_unicode = c == 'u' || c == 'w';
 
     if (initial && !is_unicode) {