Skip to content

Commit 023c9ba

Browse files
Merge branch 'main' into bug/146096-fix-base-exception-group-repr
2 parents 5fdaa02 + 89a154a commit 023c9ba

24 files changed

+249
-64
lines changed

Doc/c-api/file.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ the :mod:`io` APIs instead.
123123
124124
Write object *obj* to file object *p*. The only supported flag for *flags* is
125125
:c:macro:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
126-
instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the
127-
appropriate exception will be set.
126+
instead of the :func:`repr`.
127+
128+
If *obj* is ``NULL``, write the string ``"<NULL>"``.
128129
130+
Return ``0`` on success or ``-1`` on failure; the
131+
appropriate exception will be set.
129132
130133
.. c:function:: int PyFile_WriteString(const char *s, PyObject *p)
131134

Doc/c-api/object.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ Object Protocol
363363
representation on success, ``NULL`` on failure. This is the equivalent of the
364364
Python expression ``repr(o)``. Called by the :func:`repr` built-in function.
365365
366+
If argument is ``NULL``, return the string ``'<NULL>'``.
367+
366368
.. versionchanged:: 3.4
367369
This function now includes a debug assertion to help ensure that it
368370
does not silently discard an active exception.
@@ -377,6 +379,8 @@ Object Protocol
377379
a string similar to that returned by :c:func:`PyObject_Repr` in Python 2.
378380
Called by the :func:`ascii` built-in function.
379381
382+
If argument is ``NULL``, return the string ``'<NULL>'``.
383+
380384
.. index:: string; PyObject_Str (C function)
381385
382386
@@ -387,6 +391,8 @@ Object Protocol
387391
Python expression ``str(o)``. Called by the :func:`str` built-in function
388392
and, therefore, by the :func:`print` function.
389393
394+
If argument is ``NULL``, return the string ``'<NULL>'``.
395+
390396
.. versionchanged:: 3.4
391397
This function now includes a debug assertion to help ensure that it
392398
does not silently discard an active exception.
@@ -402,6 +408,8 @@ Object Protocol
402408
a TypeError is raised when *o* is an integer instead of a zero-initialized
403409
bytes object.
404410
411+
If argument is ``NULL``, return the :class:`bytes` object ``b'<NULL>'``.
412+
405413
406414
.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
407415

Doc/c-api/unicode.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ object.
18671867
On success, return ``0``.
18681868
On error, set an exception, leave the writer unchanged, and return ``-1``.
18691869
1870-
.. c:function:: int PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *writer, Py_UCS4 *str, Py_ssize_t size)
1870+
.. c:function:: int PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *writer, const Py_UCS4 *str, Py_ssize_t size)
18711871
18721872
Writer the UCS4 string *str* into *writer*.
18731873
@@ -1887,6 +1887,8 @@ object.
18871887
18881888
Call :c:func:`PyObject_Repr` on *obj* and write the output into *writer*.
18891889
1890+
If *obj* is ``NULL``, write the string ``"<NULL>"`` into *writer*.
1891+
18901892
On success, return ``0``.
18911893
On error, set an exception, leave the writer unchanged, and return ``-1``.
18921894

Doc/whatsnew/3.15.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ The following standard library modules have been updated to accept
221221
:func:`eval` and :func:`exec` accept :class:`!frozendict` for *globals*, and
222222
:func:`type` and :meth:`str.maketrans` accept :class:`!frozendict` for *dict*.
223223

224+
Code checking for :class:`dict` type using ``isinstance(arg, dict)`` can be
225+
updated to ``isinstance(arg, (dict, frozendict))`` to accept also the
226+
:class:`!frozendict` type, or to ``isinstance(arg, collections.abc.Mapping)``
227+
to accept also other mapping types such as :class:`~types.MappingProxyType`.
228+
224229
.. seealso:: :pep:`814` for the full specification and rationale.
225230

226231
(Contributed by Victor Stinner and Donghee Na in :gh:`141510`.)

Include/cpython/unicodeobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ PyAPI_FUNC(int) PyUnicodeWriter_WriteWideChar(
496496
Py_ssize_t size);
497497
PyAPI_FUNC(int) PyUnicodeWriter_WriteUCS4(
498498
PyUnicodeWriter *writer,
499-
Py_UCS4 *str,
499+
const Py_UCS4 *str,
500500
Py_ssize_t size);
501501

502502
PyAPI_FUNC(int) PyUnicodeWriter_WriteStr(

Lib/sysconfig/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,10 @@ def get_platform():
665665
666666
For other non-POSIX platforms, currently just returns :data:`sys.platform`."""
667667
if os.name == 'nt':
668-
if 'amd64' in sys.version.lower():
669-
return 'win-amd64'
670-
if '(arm)' in sys.version.lower():
671-
return 'win-arm32'
672-
if '(arm64)' in sys.version.lower():
673-
return 'win-arm64'
668+
import _sysconfig
669+
platform = _sysconfig.get_platform()
670+
if platform:
671+
return platform
674672
return sys.platform
675673

676674
if os.name != "posix" or not hasattr(os, 'uname'):

Lib/test/test_capi/test_list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ def test_list_extend(self):
350350
# CRASHES list_extend(NULL, [])
351351
# CRASHES list_extend([], NULL)
352352

353+
def test_uninitialized_list_repr(self):
354+
lst = _testlimitedcapi.list_new(3)
355+
self.assertEqual(repr(lst), '[<NULL>, <NULL>, <NULL>]')
356+
353357

354358
if __name__ == "__main__":
355359
unittest.main()

Lib/test/test_capi/test_tuple.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def test_tuple_new(self):
7373
self.assertRaises(SystemError, tuple_new, PY_SSIZE_T_MIN)
7474
self.assertRaises(MemoryError, tuple_new, PY_SSIZE_T_MAX)
7575

76+
7677
def test_tuple_fromarray(self):
7778
# Test PyTuple_FromArray()
7879
tuple_fromarray = _testcapi.tuple_fromarray
@@ -357,6 +358,10 @@ def my_iter():
357358
self.assertEqual(tuple(my_iter()), (TAG, *range(10)))
358359
self.assertEqual(tuples, [])
359360

361+
def test_uninitialized_tuple_repr(self):
362+
tup = _testlimitedcapi.tuple_new(3)
363+
self.assertEqual(repr(tup), '(<NULL>, <NULL>, <NULL>)')
364+
360365

361366
if __name__ == "__main__":
362367
unittest.main()

Lib/test/test_capi/test_unicode.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,13 @@ def test_basic(self):
17791779
self.assertEqual(writer.finish(),
17801780
"var=long value 'repr'")
17811781

1782+
def test_repr_null(self):
1783+
writer = self.create_writer(0)
1784+
writer.write_utf8(b'var=', -1)
1785+
writer.write_repr(NULL)
1786+
self.assertEqual(writer.finish(),
1787+
"var=<NULL>")
1788+
17821789
def test_utf8(self):
17831790
writer = self.create_writer(0)
17841791
writer.write_utf8(b"ascii", -1)

Lib/test/test_sysconfig.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shutil
88
import json
99
import textwrap
10+
from unittest.mock import patch
1011
from copy import copy
1112

1213
from test import support
@@ -247,19 +248,15 @@ def test_get_platform(self):
247248
self.assertIsInstance(actual_platform, str)
248249
self.assertTrue(actual_platform)
249250

250-
# windows XP, 32bits
251+
# Windows
251252
os.name = 'nt'
252-
sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
253-
'[MSC v.1310 32 bit (Intel)]')
254-
sys.platform = 'win32'
255-
self.assertEqual(get_platform(), 'win32')
256-
257-
# windows XP, amd64
258-
os.name = 'nt'
259-
sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
260-
'[MSC v.1310 32 bit (Amd64)]')
261-
sys.platform = 'win32'
262-
self.assertEqual(get_platform(), 'win-amd64')
253+
with patch('_sysconfig.get_platform', create=True, return_value='win32'):
254+
self.assertEqual(get_platform(), 'win32')
255+
with patch('_sysconfig.get_platform', create=True, return_value='win-amd64'):
256+
self.assertEqual(get_platform(), 'win-amd64')
257+
sys.platform = 'test-plaform'
258+
with patch('_sysconfig.get_platform', create=True, return_value=None):
259+
self.assertEqual(get_platform(), 'test-plaform')
263260

264261
# macbook
265262
os.name = 'posix'

0 commit comments

Comments
 (0)