Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 9 additions & 22 deletions Doc/library/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ defined:
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'B'`` | unsigned char | int | 1 | |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'u'`` | wchar_t | Unicode character | 2 | \(1) |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'w'`` | Py_UCS4 | Unicode character | 4 | \(2) |
| ``'w'`` | Py_UCS4 | Unicode character | 4 | \(1) |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'h'`` | signed short | int | 2 | |
+-----------+--------------------+-------------------+-----------------------+-------+
Expand All @@ -42,35 +40,24 @@ defined:
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'Q'`` | unsigned long long | int | 8 | |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'e'`` | _Float16 | float | 2 | \(3) |
| ``'e'`` | _Float16 | float | 2 | \(2) |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'f'`` | float | float | 4 | |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'d'`` | double | float | 8 | |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'Zf'`` | float complex | complex | 8 | \(4) |
| ``'Zf'`` | float complex | complex | 8 | \(3) |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'Zd'`` | double complex | complex | 16 | \(4) |
| ``'Zd'`` | double complex | complex | 16 | \(3) |
+-----------+--------------------+-------------------+-----------------------+-------+


Notes:

(1)
It can be 16 bits or 32 bits depending on the platform.

.. versionchanged:: 3.9
``array('u')`` now uses :c:type:`wchar_t` as C type instead of deprecated
``Py_UNICODE``. This change doesn't affect its behavior because
``Py_UNICODE`` is alias of :c:type:`wchar_t` since Python 3.3.

.. deprecated-removed:: 3.3 3.16
Please migrate to ``'w'`` typecode.

(2)
.. versionadded:: 3.13

(3)
(2)
The IEEE 754 binary16 "half precision" type was introduced in the 2008
revision of the `IEEE 754 standard <ieee 754 standard_>`_.
This type is not widely supported by C compilers. It's available
Expand All @@ -79,7 +66,7 @@ Notes:

.. versionadded:: 3.15

(4)
(3)
Complex types (``Zf`` and ``Zd``) are available unconditionally,
regardless on support for complex types (the Annex G of the C11 standard)
by the C compiler.
Expand Down Expand Up @@ -220,7 +207,7 @@ The module defines the following type:
.. method:: fromunicode(ustr, /)

Extends this array with data from the given Unicode string.
The array must have type code ``'u'`` or ``'w'``; otherwise a :exc:`ValueError` is raised.
The array must have type code ``'w'``; otherwise a :exc:`ValueError` is raised.
Use ``array.frombytes(unicodestring.encode(enc))`` to append Unicode data to an
array of some other type.

Expand Down Expand Up @@ -288,15 +275,15 @@ The module defines the following type:

.. method:: tounicode()

Convert the array to a Unicode string. The array must have a type ``'u'`` or ``'w'``;
Convert the array to a Unicode string. The array must have a type ``'w'``;
otherwise a :exc:`ValueError` is raised. Use ``array.tobytes().decode(enc)`` to
obtain a Unicode string from an array of some other type.


The string representation of array objects has the form
``array(typecode, initializer)``.
The *initializer* is omitted if the array is empty, otherwise it is
a Unicode string if the *typecode* is ``'u'`` or ``'w'``, otherwise it is
a Unicode string if the *typecode* is ``'w'``, otherwise it is
a list of numbers.
The string representation is guaranteed to be able to be converted back to an
array with the same type and value using :func:`eval`, so long as the
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ annotationlib
Use :meth:`annotationlib.ForwardRef.evaluate`
or :func:`typing.evaluate_forward_ref` instead.

array
-----

* The ``'u'`` format code (:c:type:`wchar_t`) which has been deprecated in
documentation since Python 3.3 and at runtime since Python 3.13.

sysconfig
---------

Expand Down
75 changes: 11 additions & 64 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')


Expand Down Expand Up @@ -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"")
Expand Down Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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')
Expand All @@ -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):

Expand Down
10 changes: 0 additions & 10 deletions Lib/test/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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 @@ -3261,15 +3260,6 @@ class BEPoint(ctypes.BigEndianStructure):
self.assertNotEqual(point, a)
self.assertRaises(NotImplementedError, a.tolist)

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

# Depends on issue #15625: the struct module does not understand 'u'.
a = array.array('u', 'xyz')
v = memoryview(a)
self.assertNotEqual(a, v)
self.assertNotEqual(v, a)

def test_memoryview_compare_ndim_zero(self):

nd1 = ndarray(1729, shape=[], format='@L')
Expand Down
5 changes: 2 additions & 3 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, linked_to_musl,
warnings_helper, SHORT_TIMEOUT, Stopwatch, requires_resource)
SHORT_TIMEOUT, Stopwatch, requires_resource)
import locale
import re
import string
Expand Down Expand Up @@ -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':
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 @@
Remove deprecated ``'u'`` type code (:c:type:`wchar_t`) for the :mod:`array`
module.
Loading
Loading