Skip to content

Commit 2c27120

Browse files
authored
Merge branch 'main' into multi_inputs
2 parents 7505f2b + ab11c09 commit 2c27120

File tree

8 files changed

+148
-56
lines changed

8 files changed

+148
-56
lines changed

Doc/c-api/unicode.rst

Lines changed: 120 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ Unicode Type
3131
These are the basic Unicode object types used for the Unicode implementation in
3232
Python:
3333

34+
.. c:var:: PyTypeObject PyUnicode_Type
35+
36+
This instance of :c:type:`PyTypeObject` represents the Python Unicode type. It
37+
is exposed to Python code as :py:class:`str`.
38+
39+
3440
.. c:type:: Py_UCS4
3541
Py_UCS2
3642
Py_UCS1
@@ -42,19 +48,6 @@ Python:
4248
.. versionadded:: 3.3
4349

4450

45-
.. c:type:: Py_UNICODE
46-
47-
This is a typedef of :c:type:`wchar_t`, which is a 16-bit type or 32-bit type
48-
depending on the platform.
49-
50-
.. versionchanged:: 3.3
51-
In previous versions, this was a 16-bit type or a 32-bit type depending on
52-
whether you selected a "narrow" or "wide" Unicode version of Python at
53-
build time.
54-
55-
.. deprecated-removed:: 3.13 3.15
56-
57-
5851
.. c:type:: PyASCIIObject
5952
PyCompactUnicodeObject
6053
PyUnicodeObject
@@ -66,12 +59,6 @@ Python:
6659
.. versionadded:: 3.3
6760

6861

69-
.. c:var:: PyTypeObject PyUnicode_Type
70-
71-
This instance of :c:type:`PyTypeObject` represents the Python Unicode type. It
72-
is exposed to Python code as ``str``.
73-
74-
7562
The following APIs are C macros and static inlined functions for fast checks and
7663
access to internal read-only data of Unicode objects:
7764

@@ -87,16 +74,6 @@ access to internal read-only data of Unicode objects:
8774
subtype. This function always succeeds.
8875
8976
90-
.. c:function:: int PyUnicode_READY(PyObject *unicode)
91-
92-
Returns ``0``. This API is kept only for backward compatibility.
93-
94-
.. versionadded:: 3.3
95-
96-
.. deprecated:: 3.10
97-
This API does nothing since Python 3.12.
98-
99-
10077
.. c:function:: Py_ssize_t PyUnicode_GET_LENGTH(PyObject *unicode)
10178
10279
Return the length of the Unicode string, in code points. *unicode* has to be a
@@ -149,12 +126,16 @@ access to internal read-only data of Unicode objects:
149126
.. c:function:: void PyUnicode_WRITE(int kind, void *data, \
150127
Py_ssize_t index, Py_UCS4 value)
151128
152-
Write into a canonical representation *data* (as obtained with
153-
:c:func:`PyUnicode_DATA`). This function performs no sanity checks, and is
154-
intended for usage in loops. The caller should cache the *kind* value and
155-
*data* pointer as obtained from other calls. *index* is the index in
156-
the string (starts at 0) and *value* is the new code point value which should
157-
be written to that location.
129+
Write the code point *value* to the given zero-based *index* in a string.
130+
131+
The *kind* value and *data* pointer must have been obtained from a
132+
string using :c:func:`PyUnicode_KIND` and :c:func:`PyUnicode_DATA`
133+
respectively. You must hold a reference to that string while calling
134+
:c:func:`!PyUnicode_WRITE`. All requirements of
135+
:c:func:`PyUnicode_WriteChar` also apply.
136+
137+
The function performs no checks for any of its requirements,
138+
and is intended for usage in loops.
158139
159140
.. versionadded:: 3.3
160141
@@ -196,6 +177,14 @@ access to internal read-only data of Unicode objects:
196177
is not ready.
197178
198179
180+
.. c:function:: unsigned int PyUnicode_IS_ASCII(PyObject *unicode)
181+
182+
Return true if the string only contains ASCII characters.
183+
Equivalent to :py:meth:`str.isascii`.
184+
185+
.. versionadded:: 3.2
186+
187+
199188
Unicode Character Properties
200189
""""""""""""""""""""""""""""
201190
@@ -330,11 +319,29 @@ APIs:
330319
to be placed in the string. As an approximation, it can be rounded up to the
331320
nearest value in the sequence 127, 255, 65535, 1114111.
332321
333-
This is the recommended way to allocate a new Unicode object. Objects
334-
created using this function are not resizable.
335-
336322
On error, set an exception and return ``NULL``.
337323
324+
After creation, the string can be filled by :c:func:`PyUnicode_WriteChar`,
325+
:c:func:`PyUnicode_CopyCharacters`, :c:func:`PyUnicode_Fill`,
326+
:c:func:`PyUnicode_WRITE` or similar.
327+
Since strings are supposed to be immutable, take care to not “use” the
328+
result while it is being modified. In particular, before it's filled
329+
with its final contents, a string:
330+
331+
- must not be hashed,
332+
- must not be :c:func:`converted to UTF-8 <PyUnicode_AsUTF8AndSize>`,
333+
or another non-"canonical" representation,
334+
- must not have its reference count changed,
335+
- must not be shared with code that might do one of the above.
336+
337+
This list is not exhaustive. Avoiding these uses is your responsibility;
338+
Python does not always check these requirements.
339+
340+
To avoid accidentally exposing a partially-written string object, prefer
341+
using the :c:type:`PyUnicodeWriter` API, or one of the ``PyUnicode_From*``
342+
functions below.
343+
344+
338345
.. versionadded:: 3.3
339346
340347
@@ -636,6 +643,9 @@ APIs:
636643
possible. Returns ``-1`` and sets an exception on error, otherwise returns
637644
the number of copied characters.
638645
646+
The string must not have been “used” yet.
647+
See :c:func:`PyUnicode_New` for details.
648+
639649
.. versionadded:: 3.3
640650
641651
@@ -648,6 +658,9 @@ APIs:
648658
Fail if *fill_char* is bigger than the string maximum character, or if the
649659
string has more than 1 reference.
650660
661+
The string must not have been “used” yet.
662+
See :c:func:`PyUnicode_New` for details.
663+
651664
Return the number of written character, or return ``-1`` and raise an
652665
exception on error.
653666
@@ -657,15 +670,16 @@ APIs:
657670
.. c:function:: int PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, \
658671
Py_UCS4 character)
659672
660-
Write a character to a string. The string must have been created through
661-
:c:func:`PyUnicode_New`. Since Unicode strings are supposed to be immutable,
662-
the string must not be shared, or have been hashed yet.
673+
Write a *character* to the string *unicode* at the zero-based *index*.
674+
Return ``0`` on success, ``-1`` on error with an exception set.
663675
664676
This function checks that *unicode* is a Unicode object, that the index is
665-
not out of bounds, and that the object can be modified safely (i.e. that it
666-
its reference count is one).
677+
not out of bounds, and that the object's reference count is one).
678+
See :c:func:`PyUnicode_WRITE` for a version that skips these checks,
679+
making them your responsibility.
667680
668-
Return ``0`` on success, ``-1`` on error with an exception set.
681+
The string must not have been “used” yet.
682+
See :c:func:`PyUnicode_New` for details.
669683
670684
.. versionadded:: 3.3
671685
@@ -1649,6 +1663,20 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
16491663
Strings interned this way are made :term:`immortal`.
16501664
16511665
1666+
.. c:function:: unsigned int PyUnicode_CHECK_INTERNED(PyObject *str)
1667+
1668+
Return a non-zero value if *str* is interned, zero if not.
1669+
The *str* argument must be a string; this is not checked.
1670+
This function always succeeds.
1671+
1672+
.. impl-detail::
1673+
1674+
A non-zero return value may carry additional information
1675+
about *how* the string is interned.
1676+
The meaning of such non-zero values, as well as each specific string's
1677+
intern-related details, may change between CPython versions.
1678+
1679+
16521680
PyUnicodeWriter
16531681
^^^^^^^^^^^^^^^
16541682
@@ -1769,8 +1797,8 @@ object.
17691797
*size* is the string length in bytes. If *size* is equal to ``-1``, call
17701798
``strlen(str)`` to get the string length.
17711799
1772-
*errors* is an error handler name, such as ``"replace"``. If *errors* is
1773-
``NULL``, use the strict error handler.
1800+
*errors* is an :ref:`error handler <error-handlers>` name, such as
1801+
``"replace"``. If *errors* is ``NULL``, use the strict error handler.
17741802
17751803
If *consumed* is not ``NULL``, set *\*consumed* to the number of decoded
17761804
bytes on success.
@@ -1781,3 +1809,49 @@ object.
17811809
On error, set an exception, leave the writer unchanged, and return ``-1``.
17821810
17831811
See also :c:func:`PyUnicodeWriter_WriteUTF8`.
1812+
1813+
Deprecated API
1814+
^^^^^^^^^^^^^^
1815+
1816+
The following API is deprecated.
1817+
1818+
.. c:type:: Py_UNICODE
1819+
1820+
This is a typedef of :c:type:`wchar_t`, which is a 16-bit type or 32-bit type
1821+
depending on the platform.
1822+
Please use :c:type:`wchar_t` directly instead.
1823+
1824+
.. versionchanged:: 3.3
1825+
In previous versions, this was a 16-bit type or a 32-bit type depending on
1826+
whether you selected a "narrow" or "wide" Unicode version of Python at
1827+
build time.
1828+
1829+
.. deprecated-removed:: 3.13 3.15
1830+
1831+
1832+
.. c:function:: int PyUnicode_READY(PyObject *unicode)
1833+
1834+
Do nothing and return ``0``.
1835+
This API is kept only for backward compatibility, but there are no plans
1836+
to remove it.
1837+
1838+
.. versionadded:: 3.3
1839+
1840+
.. deprecated:: 3.10
1841+
This API does nothing since Python 3.12.
1842+
Previously, this needed to be called for each string created using
1843+
the old API (:c:func:`!PyUnicode_FromUnicode` or similar).
1844+
1845+
1846+
.. c:function:: unsigned int PyUnicode_IS_READY(PyObject *unicode)
1847+
1848+
Do nothing and return ``1``.
1849+
This API is kept only for backward compatibility, but there are no plans
1850+
to remove it.
1851+
1852+
.. versionadded:: 3.3
1853+
1854+
.. deprecated:: next
1855+
This API does nothing since Python 3.12.
1856+
Previously, this could be called to check if
1857+
:c:func:`PyUnicode_READY` is necessary.

Include/cpython/unicodeobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static inline unsigned int PyUnicode_CHECK_INTERNED(PyObject *op) {
205205
}
206206
#define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
207207

208-
/* For backward compatibility */
208+
/* For backward compatibility. Soft-deprecated. */
209209
static inline unsigned int PyUnicode_IS_READY(PyObject* Py_UNUSED(op)) {
210210
return 1;
211211
}
@@ -398,7 +398,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_New(
398398
Py_UCS4 maxchar /* maximum code point value in the string */
399399
);
400400

401-
/* For backward compatibility */
401+
/* For backward compatibility. Soft-deprecated. */
402402
static inline int PyUnicode_READY(PyObject* Py_UNUSED(op))
403403
{
404404
return 0;

Lib/calendar.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ def formatmonthname(self, theyear, themonth, withyear=True):
648648
return super().formatmonthname(theyear, themonth, withyear)
649649

650650

651-
class _CLIDemoCalendar(LocaleTextCalendar):
651+
class _CLIDemoCalendar(TextCalendar):
652652
def __init__(self, highlight_day=None, *args, **kwargs):
653653
super().__init__(*args, **kwargs)
654654
self.highlight_day = highlight_day
@@ -752,6 +752,12 @@ def formatyear(self, theyear, w=2, l=1, c=6, m=3):
752752
return ''.join(v)
753753

754754

755+
class _CLIDemoLocaleCalendar(LocaleTextCalendar, _CLIDemoCalendar):
756+
def __init__(self, highlight_day=None, *args, **kwargs):
757+
super().__init__(*args, **kwargs)
758+
self.highlight_day = highlight_day
759+
760+
755761
# Support for old module level interface
756762
c = TextCalendar()
757763

@@ -893,7 +899,7 @@ def main(args=None):
893899
write(cal.formatyearpage(options.year, **optdict))
894900
else:
895901
if options.locale:
896-
cal = _CLIDemoCalendar(highlight_day=today, locale=locale)
902+
cal = _CLIDemoLocaleCalendar(highlight_day=today, locale=locale)
897903
else:
898904
cal = _CLIDemoCalendar(highlight_day=today)
899905
cal.setfirstweekday(options.first_weekday)

Lib/test/test_ipaddress.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,8 @@ class ComparisonTests(unittest.TestCase):
894894
v6net = ipaddress.IPv6Network(1)
895895
v6intf = ipaddress.IPv6Interface(1)
896896
v6addr_scoped = ipaddress.IPv6Address('::1%scope')
897-
v6net_scoped= ipaddress.IPv6Network('::1%scope')
898-
v6intf_scoped= ipaddress.IPv6Interface('::1%scope')
897+
v6net_scoped = ipaddress.IPv6Network('::1%scope')
898+
v6intf_scoped = ipaddress.IPv6Interface('::1%scope')
899899

900900
v4_addresses = [v4addr, v4intf]
901901
v4_objects = v4_addresses + [v4net]
@@ -1083,6 +1083,7 @@ def setUp(self):
10831083
self.ipv6_scoped_interface = ipaddress.IPv6Interface(
10841084
'2001:658:22a:cafe:200:0:0:1%scope/64')
10851085
self.ipv6_scoped_network = ipaddress.IPv6Network('2001:658:22a:cafe::%scope/64')
1086+
self.ipv6_with_ipv4_part = ipaddress.IPv6Interface('::1.2.3.4')
10861087

10871088
def testRepr(self):
10881089
self.assertEqual("IPv4Interface('1.2.3.4/32')",
@@ -1713,6 +1714,8 @@ def testEqual(self):
17131714

17141715
self.assertTrue(self.ipv6_scoped_interface ==
17151716
ipaddress.IPv6Interface('2001:658:22a:cafe:200::1%scope/64'))
1717+
self.assertTrue(self.ipv6_with_ipv4_part ==
1718+
ipaddress.IPv6Interface('0000:0000:0000:0000:0000:0000:0102:0304'))
17161719
self.assertFalse(self.ipv6_scoped_interface ==
17171720
ipaddress.IPv6Interface('2001:658:22a:cafe:200::1%scope/63'))
17181721
self.assertFalse(self.ipv6_scoped_interface ==
@@ -2195,6 +2198,7 @@ def testIPVersion(self):
21952198
self.assertEqual(self.ipv4_address.version, 4)
21962199
self.assertEqual(self.ipv6_address.version, 6)
21972200
self.assertEqual(self.ipv6_scoped_address.version, 6)
2201+
self.assertEqual(self.ipv6_with_ipv4_part.version, 6)
21982202

21992203
def testMaxPrefixLength(self):
22002204
self.assertEqual(ipaddress.IPv4Address.max_prefixlen, 32)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Only apply locale to :ref:`calendar CLI <calendar-cli>` when set via
2+
``--locale`` and not via ``LANG`` environment variable.

Python/ceval_gil.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ take_gil(PyThreadState *tstate)
325325
while (_Py_atomic_load_int_relaxed(&gil->locked)) {
326326
unsigned long saved_switchnum = gil->switch_number;
327327

328-
unsigned long interval = (gil->interval >= 1 ? gil->interval : 1);
328+
unsigned long interval = _Py_atomic_load_ulong_relaxed(&gil->interval);
329+
if (interval < 1) {
330+
interval = 1;
331+
}
329332
int timed_out = 0;
330333
COND_TIMED_WAIT(gil->cond, gil->mutex, interval, timed_out);
331334

@@ -420,15 +423,15 @@ void _PyEval_SetSwitchInterval(unsigned long microseconds)
420423
PyInterpreterState *interp = _PyInterpreterState_GET();
421424
struct _gil_runtime_state *gil = interp->ceval.gil;
422425
assert(gil != NULL);
423-
gil->interval = microseconds;
426+
_Py_atomic_store_ulong_relaxed(&gil->interval, microseconds);
424427
}
425428

426429
unsigned long _PyEval_GetSwitchInterval(void)
427430
{
428431
PyInterpreterState *interp = _PyInterpreterState_GET();
429432
struct _gil_runtime_state *gil = interp->ceval.gil;
430433
assert(gil != NULL);
431-
return gil->interval;
434+
return _Py_atomic_load_ulong_relaxed(&gil->interval);
432435
}
433436

434437

Tools/tsan/suppressions.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# This file contains suppressions for the default (with GIL) build.
22
# reference: https://github.com/google/sanitizers/wiki/ThreadSanitizerSuppressions
33

4+
# gh-124878: race condition when interpreter finalized while daemon thread runs
5+
race:free_threadstate
6+
47
# https://gist.github.com/mpage/daaf32b39180c1989572957b943eb665
58
thread:pthread_create

Tools/tsan/suppressions_free_threading.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# These entries are for warnings that trigger in a library function, as called
1111
# by a CPython function.
1212

13-
# https://gist.github.com/swtaarrs/08dfe7883b4c975c31ecb39388987a67
13+
# gh-124878: race condition when interpreter finalized while daemon thread runs
1414
race:free_threadstate
1515

1616
# These warnings trigger directly in a CPython function.

0 commit comments

Comments
 (0)