Skip to content

Commit e50efcb

Browse files
committed
pythongh-80480 Deprecate array's 'u' type code
1 parent a17cd47 commit e50efcb

File tree

7 files changed

+46
-16
lines changed

7 files changed

+46
-16
lines changed

Doc/library/array.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Notes:
5555
``Py_UNICODE``. This change doesn't affect its behavior because
5656
``Py_UNICODE`` is alias of ``wchar_t`` since Python 3.3.
5757

58-
.. deprecated-removed:: 3.3 4.0
58+
.. deprecated-removed:: 3.3 3.14
5959

6060

6161
The actual representation of values is determined by the machine architecture

Doc/whatsnew/3.12.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,11 @@ Pending Removal in Python 3.14
205205

206206
(Contributed by Jason R. Coombs and Hugo van Kemenade in :gh:`93963`.)
207207

208+
APIs:
209+
208210
* Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable
209-
bases using the C API.
211+
bases using the C API (:gh:`95388`)
212+
* :mod:`array`'s ``'u'`` format code (:gh:`57281`)
210213

211214

212215
Pending Removal in Future Versions

Lib/test/test_array.py

+12-1
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.14",
107+
category=DeprecationWarning)
108+
98109
def test_error(self):
99110
self.assertRaises(TypeError, array_reconstructor,
100111
"", "b", 0, b"")

Lib/test/test_buffer.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3209,10 +3209,11 @@ def test_memoryview_compare_special_cases(self):
32093209
self.assertNotEqual(memoryview(nd), nd)
32103210

32113211
# Depends on issue #15625: the struct module does not understand 'u'.
3212-
a = array.array('u', 'xyz')
3213-
v = memoryview(a)
3214-
self.assertNotEqual(a, v)
3215-
self.assertNotEqual(v, a)
3212+
with self.assertRaises(DeprecationWarning):
3213+
a = array.array('u', 'xyz')
3214+
v = memoryview(a)
3215+
self.assertNotEqual(a, v)
3216+
self.assertNotEqual(v, a)
32163217

32173218
# Some ctypes format strings are unknown to the struct module.
32183219
if ctypes:

Lib/test/test_csv.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -882,14 +882,15 @@ def test_float_write(self):
882882

883883
def test_char_write(self):
884884
import array, string
885-
a = array.array('u', string.ascii_letters)
886-
887-
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
888-
writer = csv.writer(fileobj, dialect="excel")
889-
writer.writerow(a)
890-
expected = ",".join(a)+"\r\n"
891-
fileobj.seek(0)
892-
self.assertEqual(fileobj.read(), expected)
885+
with self.assertRaises(DeprecationWarning):
886+
a = array.array('u', string.ascii_letters)
887+
888+
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
889+
writer = csv.writer(fileobj, dialect="excel")
890+
writer.writerow(a)
891+
expected = ",".join(a)+"\r\n"
892+
fileobj.seek(0)
893+
self.assertEqual(fileobj.read(), expected)
893894

894895
class TestDialectValidity(unittest.TestCase):
895896
def test_quoting(self):

Lib/test/test_re.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1520,11 +1520,16 @@ def test_bug_6561(self):
15201520
def test_empty_array(self):
15211521
# SF buf 1647541
15221522
import array
1523-
for typecode in 'bBuhHiIlLfd':
1523+
for typecode in 'bBhHiIlLfd':
15241524
a = array.array(typecode)
15251525
self.assertIsNone(re.compile(b"bla").match(a))
15261526
self.assertEqual(re.compile(b"").match(a).groups(), ())
15271527

1528+
with self.assertWarns(DeprecationWarning):
1529+
a = array.array('u')
1530+
self.assertIsNone(re.compile(b"bla").match(a))
1531+
self.assertEqual(re.compile(b"").match(a).groups(), ())
1532+
15281533
def test_inline_flags(self):
15291534
# Bug #1700
15301535
upper_char = '\u1ea0' # Latin Capital Letter A with Dot Below

Modules/arraymodule.c

+9
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,15 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26252625
return NULL;
26262626
}
26272627

2628+
if (c == 'u') {
2629+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2630+
"The 'u' type code is deprecated and "
2631+
"will be removed in Python 3.14",
2632+
1)) {
2633+
return NULL;
2634+
}
2635+
}
2636+
26282637
if (initial && c != 'u') {
26292638
if (PyUnicode_Check(initial)) {
26302639
PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "

0 commit comments

Comments
 (0)