Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-80480 Emit DeprecationWarning for array's 'u' type code
  • Loading branch information
hugovk committed Aug 7, 2022
commit 7b020ce8e9b3b14359e0a492ddb179b23e1a988b
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
7 changes: 6 additions & 1 deletion Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,13 @@ 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, deprecated in docs since Python 3.3,
emits :exc:`DeprecationWarning` since 3.12
(contributed by Hugo van Kemenade in :gh:`80480`)


Pending Removal in Future Versions
Expand Down
22 changes: 21 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 Expand Up @@ -1205,6 +1216,15 @@ def test_issue17223(self):
self.assertRaises(ValueError, a.tounicode)
self.assertRaises(ValueError, str, a)

def test_typecode_u_deprecation(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", DeprecationWarning)
array.array("u")
self.assertGreaterEqual(len(w), 1)
for warning in w:
self.assertIs(warning.category, DeprecationWarning)


class NumberTest(BaseTest):

def test_extslice(self):
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Emit :exc:`DeprecationWarning` for :mod:`array`'s ``'u'`` type code,
deprecated in docs since Python 3.3.
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