Skip to content

Commit

Permalink
pythongh-80480 Deprecate array's 'u' type code
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Aug 7, 2022
1 parent 489be0d commit 4a68155
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Doc/library/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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.14


The actual representation of values is determined by the machine architecture
Expand Down
5 changes: 4 additions & 1 deletion Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ Pending Removal in Python 3.14

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

APIs:

* Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable
bases using the C API.
bases using the C API (:gh:`95388`)
* :mod:`array`'s ``'u'`` format code (:gh:`57281`)


Pending Removal in Future Versions
Expand Down
13 changes: 12 additions & 1 deletion Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
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):
Expand Down Expand Up @@ -93,8 +96,16 @@ def test_empty(self):
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.14",
category=DeprecationWarning)

def test_error(self):
self.assertRaises(TypeError, array_reconstructor,
"", "b", 0, b"")
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import sys, array, io, os
from decimal import Decimal
from fractions import Fraction
from test.support import warnings_helper

try:
from _testbuffer import *
Expand Down Expand Up @@ -3174,6 +3175,7 @@ def cmptest(testcase, a, b, m, singleitem):
self.assertEqual(m.tobytes(), a.tobytes())
cmptest(self, a, b, m, singleitem)

@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-80480 array('u')
def test_memoryview_compare_special_cases(self):

a = array.array('L', [1, 2, 3])
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ def test_float_write(self):
fileobj.seek(0)
self.assertEqual(fileobj.read(), expected)

@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-80480 array('u')
def test_char_write(self):
import array, string
a = array.array('u', string.ascii_letters)
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -1517,10 +1517,11 @@ def test_bug_6561(self):
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 'bBhuHiIlLfd':
a = array.array(typecode)
self.assertIsNone(re.compile(b"bla").match(a))
self.assertEqual(re.compile(b"").match(a).groups(), ())
Expand Down
9 changes: 9 additions & 0 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,15 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
return NULL;

if (c == 'u') {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"The 'u' type code is deprecated and "
"will be removed in Python 3.14",
1)) {
return NULL;
}
}

if (PySys_Audit("array.__new__", "CO",
c, initial ? initial : Py_None) < 0) {
return NULL;
Expand Down

0 comments on commit 4a68155

Please sign in to comment.