Skip to content

Commit cc87948

Browse files
authored
gh-80480: Emit DeprecationWarning for array's 'u' type code (#95760)
1 parent 3a314f7 commit cc87948

File tree

7 files changed

+48
-10
lines changed

7 files changed

+48
-10
lines changed

Doc/library/array.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Notes:
5757
``Py_UNICODE``. This change doesn't affect its behavior because
5858
``Py_UNICODE`` is alias of ``wchar_t`` since Python 3.3.
5959

60-
.. deprecated-removed:: 3.3 4.0
60+
.. deprecated-removed:: 3.3 3.16
6161
Please migrate to ``'w'`` typecode.
6262

6363

Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ Deprecated
134134
They will be removed in Python 3.15.
135135
(Contributed by Victor Stinner in :gh:`105096`.)
136136

137+
* :mod:`array`'s ``'u'`` format code, deprecated in docs since Python 3.3,
138+
emits :exc:`DeprecationWarning` since 3.13
139+
and will be removed in Python 3.16.
140+
Use the ``'w'`` format code instead.
141+
(contributed by Hugo van Kemenade in :gh:`80480`)
137142

138143
Removed
139144
=======

Lib/test/test_array.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
import operator
1414
import struct
1515
import sys
16+
import warnings
1617

1718
import array
1819
from array import _array_reconstructor as array_reconstructor
1920

20-
sizeof_wchar = array.array('u').itemsize
21+
with warnings.catch_warnings():
22+
warnings.simplefilter('ignore', DeprecationWarning)
23+
sizeof_wchar = array.array('u').itemsize
2124

2225

2326
class ArraySubclass(array.array):
@@ -93,8 +96,16 @@ def test_empty(self):
9396
UTF32_LE = 20
9497
UTF32_BE = 21
9598

99+
96100
class ArrayReconstructorTest(unittest.TestCase):
97101

102+
def setUp(self):
103+
warnings.filterwarnings(
104+
"ignore",
105+
message="The 'u' type code is deprecated and "
106+
"will be removed in Python 3.16",
107+
category=DeprecationWarning)
108+
98109
def test_error(self):
99110
self.assertRaises(TypeError, array_reconstructor,
100111
"", "b", 0, b"")
@@ -1208,10 +1219,16 @@ def test_issue17223(self):
12081219
self.assertRaises(ValueError, a.tounicode)
12091220
self.assertRaises(ValueError, str, a)
12101221

1222+
def test_typecode_u_deprecation(self):
1223+
with self.assertWarns(DeprecationWarning):
1224+
array.array("u")
1225+
1226+
12111227
class UCS4Test(UnicodeTest):
12121228
typecode = 'w'
12131229
minitemsize = 4
12141230

1231+
12151232
class NumberTest(BaseTest):
12161233

12171234
def test_extslice(self):

Lib/test/test_buffer.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import sys, array, io, os
2525
from decimal import Decimal
2626
from fractions import Fraction
27+
from test.support import warnings_helper
2728

2829
try:
2930
from _testbuffer import *
@@ -3217,12 +3218,6 @@ def test_memoryview_compare_special_cases(self):
32173218
nd[0] = (-1, float('nan'))
32183219
self.assertNotEqual(memoryview(nd), nd)
32193220

3220-
# Depends on issue #15625: the struct module does not understand 'u'.
3221-
a = array.array('u', 'xyz')
3222-
v = memoryview(a)
3223-
self.assertNotEqual(a, v)
3224-
self.assertNotEqual(v, a)
3225-
32263221
# Some ctypes format strings are unknown to the struct module.
32273222
if ctypes:
32283223
# format: "T{>l:x:>l:y:}"
@@ -3236,6 +3231,15 @@ class BEPoint(ctypes.BigEndianStructure):
32363231
self.assertNotEqual(point, a)
32373232
self.assertRaises(NotImplementedError, a.tolist)
32383233

3234+
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-80480 array('u')
3235+
def test_memoryview_compare_special_cases_deprecated_u_type_code(self):
3236+
3237+
# Depends on issue #15625: the struct module does not understand 'u'.
3238+
a = array.array('u', 'xyz')
3239+
v = memoryview(a)
3240+
self.assertNotEqual(a, v)
3241+
self.assertNotEqual(v, a)
3242+
32393243
def test_memoryview_compare_ndim_zero(self):
32403244

32413245
nd1 = ndarray(1729, shape=[], format='@L')

Lib/test/test_re.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from test.support import (gc_collect, bigmemtest, _2G,
22
cpython_only, captured_stdout,
33
check_disallow_instantiation, is_emscripten, is_wasi,
4-
SHORT_TIMEOUT)
4+
warnings_helper, SHORT_TIMEOUT)
55
import locale
66
import re
77
import string
@@ -1522,10 +1522,11 @@ def test_bug_6561(self):
15221522
for x in not_decimal_digits:
15231523
self.assertIsNone(re.match(r'^\d$', x))
15241524

1525+
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-80480 array('u')
15251526
def test_empty_array(self):
15261527
# SF buf 1647541
15271528
import array
1528-
for typecode in 'bBuhHiIlLfd':
1529+
for typecode in 'bBhuwHiIlLfd':
15291530
a = array.array(typecode)
15301531
self.assertIsNone(re.compile(b"bla").match(a))
15311532
self.assertEqual(re.compile(b"").match(a).groups(), ())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Emit :exc:`DeprecationWarning` for :mod:`array`'s ``'u'`` type code,
2+
deprecated in docs since Python 3.3.

Modules/arraymodule.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,6 +2679,15 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26792679
return NULL;
26802680
}
26812681

2682+
if (c == 'u') {
2683+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2684+
"The 'u' type code is deprecated and "
2685+
"will be removed in Python 3.16",
2686+
1)) {
2687+
return NULL;
2688+
}
2689+
}
2690+
26822691
bool is_unicode = c == 'u' || c == 'w';
26832692

26842693
if (initial && !is_unicode) {

0 commit comments

Comments
 (0)