-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-80480: remove deprecated 'u' type code (array) #149535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,15 +14,10 @@ | |
| import operator | ||
| import struct | ||
| import sys | ||
| import warnings | ||
|
|
||
| import array | ||
| from array import _array_reconstructor as array_reconstructor | ||
|
|
||
| with warnings.catch_warnings(): | ||
| warnings.simplefilter('ignore', DeprecationWarning) | ||
| sizeof_wchar = array.array('u').itemsize | ||
|
|
||
|
|
||
| class ArraySubclass(array.array): | ||
| pass | ||
|
|
@@ -32,7 +27,7 @@ def __init__(self, typecode, newarg=None): | |
| array.array.__init__(self) | ||
|
|
||
| typecodes = ( | ||
| 'u', 'w', 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', | ||
| 'w', 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', | ||
| 'f', 'd', 'q', 'Q', 'e', 'Zf', 'Zd') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to maintain such list manually rather |
||
|
|
||
|
|
||
|
|
@@ -135,14 +130,6 @@ def test_typecodes(self): | |
|
|
||
| class ArrayReconstructorTest(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.enterContext(warnings.catch_warnings()) | ||
| 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"") | ||
|
|
@@ -242,12 +229,11 @@ def test_unicode(self): | |
| ) | ||
| for testcase in testcases: | ||
| mformat_code, encoding = testcase | ||
| for c in 'uw': | ||
| a = array.array(c, teststr) | ||
| b = array_reconstructor( | ||
| array.array, c, mformat_code, teststr.encode(encoding)) | ||
| self.assertEqual(a, b, | ||
| msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) | ||
| a = array.array('w', teststr) | ||
| b = array_reconstructor( | ||
| array.array, 'w', mformat_code, teststr.encode(encoding)) | ||
| self.assertEqual(a, b, | ||
| msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) | ||
|
|
||
|
|
||
| class BaseTest: | ||
|
|
@@ -259,14 +245,6 @@ class BaseTest: | |
| # outside: An entry that is not in example | ||
| # minitemsize: the minimum guaranteed itemsize | ||
|
|
||
| def setUp(self): | ||
| self.enterContext(warnings.catch_warnings()) | ||
| warnings.filterwarnings( | ||
| "ignore", | ||
| message="The 'u' type code is deprecated and " | ||
| "will be removed in Python 3.16", | ||
| category=DeprecationWarning) | ||
|
|
||
| def assertEntryEqual(self, entry1, entry2): | ||
| self.assertEqual(entry1, entry2) | ||
|
|
||
|
|
@@ -299,7 +277,7 @@ def test_buffer_info(self): | |
| self.assertEqual(bi[1], len(a)) | ||
|
|
||
| def test_byteswap(self): | ||
| if self.typecode in ('u', 'w'): | ||
| if self.typecode == 'w': | ||
| example = '\U00100100' | ||
| else: | ||
| example = self.example | ||
|
|
@@ -1167,7 +1145,7 @@ def test_buffer(self): | |
| self.assertEqual(m.tobytes(), expected) | ||
| self.assertRaises(BufferError, a.frombytes, a.tobytes()) | ||
| self.assertEqual(m.tobytes(), expected) | ||
| if self.typecode in ('u', 'w'): | ||
| if self.typecode == 'w': | ||
| self.assertRaises(BufferError, a.fromunicode, a.tounicode()) | ||
| self.assertEqual(m.tobytes(), expected) | ||
| self.assertRaises(BufferError, operator.imul, a, 2) | ||
|
|
@@ -1223,7 +1201,7 @@ def test_sizeof_without_buffer(self): | |
| support.check_sizeof(self, a, basesize) | ||
|
|
||
| def test_initialize_with_unicode(self): | ||
| if self.typecode not in ('u', 'w'): | ||
| if self.typecode != 'w': | ||
| with self.assertRaises(TypeError) as cm: | ||
| a = array.array(self.typecode, 'foo') | ||
| self.assertIn("cannot use a str", str(cm.exception)) | ||
|
|
@@ -1232,7 +1210,6 @@ def test_initialize_with_unicode(self): | |
| self.assertIn("cannot use a unicode array", str(cm.exception)) | ||
| else: | ||
| a = array.array(self.typecode, "foo") | ||
| a = array.array(self.typecode, array.array('u', 'foo')) | ||
| a = array.array(self.typecode, array.array('w', 'foo')) | ||
|
|
||
| @support.cpython_only | ||
|
|
@@ -1258,12 +1235,12 @@ def test_setitem(self): | |
| self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) | ||
|
|
||
| class UnicodeTest(StringTest, unittest.TestCase): | ||
| typecode = 'u' | ||
| typecode = 'w' | ||
| example = '\x01\u263a\x00\ufeff' | ||
| smallerexample = '\x01\u263a\x00\ufefe' | ||
| biggerexample = '\x01\u263a\x01\ufeff' | ||
| outside = str('\x33') | ||
| minitemsize = sizeof_wchar | ||
| minitemsize = 4 | ||
|
|
||
| def test_unicode(self): | ||
| self.assertRaises(TypeError, array.array, 'b', 'foo') | ||
|
|
@@ -1285,36 +1262,6 @@ def test_unicode(self): | |
|
|
||
| self.assertRaises(TypeError, a.fromunicode) | ||
|
|
||
| def test_issue17223(self): | ||
| if self.typecode == 'u' and sizeof_wchar == 2: | ||
| # PyUnicode_FromUnicode() cannot fail with 16-bit wchar_t | ||
| self.skipTest("specific to 32-bit wchar_t") | ||
|
|
||
| # this used to crash | ||
| # U+FFFFFFFF is an invalid code point in Unicode 6.0 | ||
| invalid_str = b'\xff\xff\xff\xff' | ||
|
|
||
| a = array.array(self.typecode, invalid_str) | ||
| self.assertRaises(ValueError, a.tounicode) | ||
| self.assertRaises(ValueError, str, a) | ||
|
|
||
| def test_typecode_u_deprecation(self): | ||
| with self.assertWarns(DeprecationWarning): | ||
| array.array("u") | ||
|
|
||
| def test_empty_string_mem_leak_gh140474(self): | ||
| with warnings.catch_warnings(): | ||
| warnings.simplefilter('ignore', DeprecationWarning) | ||
| for _ in range(1000): | ||
| a = array.array('u', '') | ||
| self.assertEqual(len(a), 0) | ||
| self.assertEqual(a.typecode, 'u') | ||
|
|
||
|
|
||
| class UCS4Test(UnicodeTest): | ||
| typecode = 'w' | ||
| minitemsize = 4 | ||
|
|
||
|
|
||
| class NumberTest(BaseTest): | ||
|
|
||
|
|
||
| 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, linked_to_musl, | ||
| warnings_helper, SHORT_TIMEOUT, Stopwatch, requires_resource) | ||
| SHORT_TIMEOUT, Stopwatch, requires_resource) | ||
| import locale | ||
| import re | ||
| import string | ||
|
|
@@ -1780,11 +1780,10 @@ 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 'bBhuwHiIlLfd': | ||
| for typecode in 'bBhwHiIlLfd': | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to not reuse |
||
| a = array.array(typecode) | ||
| self.assertIsNone(re.compile(b"bla").match(a)) | ||
| self.assertEqual(re.compile(b"").match(a).groups(), ()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Remove deprecated ``'u'`` type code (:c:type:`wchar_t`) for the :mod:`array` | ||
| module. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you suggest replacing
'u'with'w'on Python 3.13 and newer? It's good to suggest a solution for impacted projects. Explain thatwis always 4 bytes.Same remark for the NEWS entry (copy/paste the updated doc there.)