Skip to content

Commit 6efed27

Browse files
committed
Merge remote-tracking branch 'upstream/main' into gh-114258-getitem
2 parents b2ce555 + 6e1bd24 commit 6efed27

28 files changed

Lines changed: 496 additions & 96 deletions

‎Doc/c-api/descriptor.rst‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,38 +72,34 @@ found in the dictionary of type objects.
7272
methods on a type, and correspond to :class:`types.MethodDescriptorType`
7373
objects in Python.
7474
75+
.. c:var:: PyTypeObject PyWrapperDescr_Type
7576
76-
.. c:struct:: wrapperbase
77+
The type object for wrapper descriptor objects created by
78+
:c:func:`PyDescr_NewWrapper` and :c:func:`PyWrapper_New`.
7779
78-
Describes a slot wrapper used by :c:func:`PyDescr_NewWrapper`.
80+
Wrapper descriptors are used internally to expose special methods
81+
such as ``__repr__`` or ``__add__``, which are implemented by type slots.
82+
The type is exposed in Python as :class:`types.WrapperDescriptorType`.
7983
80-
Each ``wrapperbase`` record stores the Python-visible name and metadata for a
81-
special method implemented by a type slot, together with the wrapper
82-
function used to adapt that slot to Python's calling convention.
8384
84-
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
85+
.. `wrapperbase` & `PyWrapperFlag_KEYWORDS` show up in search and index,
86+
and link here, but are only referenced in prose:
8587
86-
Create a new wrapper descriptor for extension type *type* from the
87-
:c:struct:`wrapperbase` structure *base* and the wrapped slot function
88-
pointer
89-
*wrapped*.
88+
.. c:struct:: wrapperbase
89+
:no-typesetting:
9090
91-
Wrapper descriptors expose special methods implemented by type slots. This
92-
is the same kind of descriptor that CPython creates for slot-based special
93-
methods such as ``__repr__`` or ``__add__``, and it appears in Python as a
94-
:class:`types.WrapperDescriptorType` object.
91+
.. c:macro:: PyWrapperFlag_KEYWORDS
92+
:no-typesetting:
9593
96-
On success, return a :term:`strong reference` to the descriptor. Return
97-
``NULL`` with an exception set on failure.
94+
.. c:function:: PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
9895
99-
.. c:var:: PyTypeObject PyWrapperDescr_Type
96+
Create a new wrapper descriptor of type :c:var:`PyWrapperDescr_Type`.
10097
101-
The type object for wrapper descriptor objects created by
102-
:c:func:`PyDescr_NewWrapper` and :c:func:`PyWrapper_New`. Wrapper
103-
descriptors are used internally to expose special methods implemented
104-
via wrapper structures, and appear in Python as
105-
:class:`types.WrapperDescriptorType` objects.
98+
This function is meant for internal use only; it and
99+
related API (``struct wrapperbase`` and the ``PyWrapperFlag_KEYWORDS``
100+
flag) are exposed publicly for backwards compatibility reasons.
106101
102+
See :c:func:`PyCMethod_New` for wrapping a C function.
107103
108104
.. c:function:: PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
109105
@@ -138,6 +134,30 @@ found in the dictionary of type objects.
138134
On success, return a :term:`strong reference` to the wrapper object. Return
139135
``NULL`` with an exception set on failure.
140136
137+
.. c:macro:: PyDescr_TYPE(descr)
138+
139+
Return a :term:`borrowed reference` to the type that the descriptor
140+
*descr* belongs to. This is accessible as :attr:`~object.__objclass__`
141+
in Python.
142+
143+
*descr* must be a descriptor object created by one of the
144+
``PyDescr_New*`` functions (an instance of
145+
:c:var:`PyMethodDescr_Type`, :c:var:`PyClassMethodDescr_Type`,
146+
:c:var:`PyMemberDescr_Type`, :c:var:`PyGetSetDescr_Type`, or
147+
:c:var:`PyWrapperDescr_Type`). If the passed descriptor is not
148+
an instance of one of these types, the behavior is undefined.
149+
150+
151+
.. c:macro:: PyDescr_NAME(descr)
152+
153+
Return the name of the descriptor *descr* as a :term:`borrowed reference`
154+
to a :class:`str` object. This is available as :attr:`~definition.__name__`
155+
in Python.
156+
157+
*descr* must be a descriptor object created by one of the ``PyDescr_New*``
158+
functions. If not, the behavior is undefined.
159+
160+
141161
.. c:macro:: PyDescr_COMMON
142162
143163
This is a macro including the common fields for a

‎Lib/argparse.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Author: Steven J. Bethard <steven.bethard@gmail.com>.
2-
# New maintainer as of 29 August 2019: Raymond Hettinger <raymond.hettinger@gmail.com>
32

43
"""Command-line parsing library
54

‎Lib/test/test_build_details.py‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ def test_libpython_static(self):
191191
with open(value['static'], 'rb') as f:
192192
self.assertEqual(f.read(8), b'!<arch>\n')
193193

194+
@needs_installed_python
195+
@unittest.skipIf(
196+
is_android or is_apple_mobile,
197+
"Android and iOS run tests via a custom testbed method that doesn't ship libpython"
198+
)
199+
@unittest.skipIf(
200+
sys.platform == 'cygwin',
201+
"Cygwin installs the DLL in BINDIR and the import library in LIBPL, "
202+
"not in LIBDIR"
203+
)
204+
def test_libpython_dynamic(self):
205+
value = self.data.get('libpython', {})
206+
if 'dynamic' not in value:
207+
self.skipTest("No dynamic libpython")
208+
209+
if sysconfig._installation_is_relocated():
210+
self.skipTest("Installation is relocated")
211+
212+
self.assertTrue(os.path.isfile(value['dynamic']))
213+
194214

195215
@unittest.skipIf(
196216
generate_build_details is None,
@@ -217,6 +237,24 @@ def test_round_trip(self):
217237
self.assertEqual(data_abs_path['base_prefix'],
218238
data_rel_path['base_prefix'])
219239

240+
self.check_relative_paths(data_abs_path, data_rel_path)
241+
242+
def test_round_trip_with_config_file_path(self):
243+
data_abs_path = self.build_details_absolute_paths
244+
base_prefix = data_abs_path['base_prefix']
245+
config_dir = os.path.join(base_prefix, 'lib', 'python3.X')
246+
data_rel_path = self.build_details_absolute_paths
247+
generate_build_details.make_paths_relative(
248+
data_rel_path,
249+
config_path=os.path.join(config_dir, 'build-details.json'),
250+
)
251+
252+
# base_prefix is relative to the config file's directory...
253+
self.assertEqual(data_rel_path['base_prefix'], os.path.join('..', '..'))
254+
# ...while the other paths are still relative to base_prefix.
255+
self.check_relative_paths(data_abs_path, data_rel_path)
256+
257+
def check_relative_paths(self, data_abs_path, data_rel_path):
220258
base_prefix = data_abs_path['base_prefix']
221259

222260
top_level_keys = ('base_interpreter',)
@@ -303,8 +341,14 @@ def test_framework(self):
303341
# a libpython3.X.a symlink to the framework's shared library instead.
304342
libpython = self.generate_libpython(
305343
LDLIBRARY='Python.framework/Versions/3.X/Python',
306-
STATIC_LIBPYTHON=1, PYTHONFRAMEWORK='Python')
344+
STATIC_LIBPYTHON=1, PYTHONFRAMEWORK='Python',
345+
PYTHONFRAMEWORKPREFIX='/Library/Frameworks')
307346
self.assertNotIn('static', libpython)
347+
# The dynamic library is the framework binary, which is installed at
348+
# PYTHONFRAMEWORKPREFIX/LDLIBRARY.
349+
self.assertEqual(
350+
libpython['dynamic'],
351+
'/Library/Frameworks/Python.framework/Versions/3.X/Python')
308352

309353

310354
if __name__ == '__main__':

‎Lib/test/test_clinic.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,7 @@ class Foo "FooObject *" "&Foo_Type"
858858
def test_slot_with_parameters_and_return_type(self):
859859
# Slots which take arguments or return something else than an
860860
# object are not supported yet; check the machinery with them.
861-
slots = {
862-
'__contains__': SlotSpec(return_converter='int'),
863-
'__bool__': SlotSpec(return_converter='int'),
864-
}
861+
slots = {'__bool__': SlotSpec(return_converter='int')}
865862
block = """
866863
/*[clinic input]
867864
output everything block
@@ -3356,6 +3353,20 @@ class Foo "FooObject *" ""
33563353
self.assertEqual(function.return_converter.type, "PyObject *")
33573354
self.assertEqual(list(function.parameters), ["self"])
33583355

3356+
def test_slot_coexist_is_a_method(self):
3357+
# dict.__contains__ is a METH_COEXIST method, not the slot.
3358+
function = self.parse_function("""
3359+
module foo
3360+
class Foo "FooObject *" ""
3361+
@coexist
3362+
Foo.__contains__
3363+
key: object
3364+
/
3365+
""", signatures_in_block=3, function_index=2)
3366+
self.assertIs(function.kind, FunctionKind.CALLABLE)
3367+
self.assertIsNone(function.slot)
3368+
self.assertIn("METH_COEXIST", function.methoddef_flags)
3369+
33593370
def test_slot_parameters(self):
33603371
# The parameter of __getitem__ gives the slot (Py_ssize_t for
33613372
# sq_item, object for mp_subscript) and is not parsed.
@@ -3392,8 +3403,6 @@ def test_slot_errors(self):
33923403
"'__next__' must be a normal method"),
33933404
("@getter\nFoo.__next__",
33943405
"'__next__' must be a normal method"),
3395-
("@coexist\nFoo.__next__",
3396-
"@coexist cannot be applied to '__next__'"),
33973406
("@vectorcall\nFoo.__next__",
33983407
"@vectorcall can only be used with __init__ and __new__"),
33993408
("Foo.__next__ -> int",

‎Lib/test/test_dict.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,21 @@ def test_hash(self):
19901990
with self.assertRaisesRegex(TypeError, "unhashable type: 'list'"):
19911991
hash(fd)
19921992

1993+
@support.skip_if_huge_c_stack(500_000)
1994+
@support.skip_wasi_stack_overflow()
1995+
@support.skip_emscripten_stack_overflow()
1996+
def test_hash_deeply_nested(self):
1997+
# This should raise a RecursionError and not crash.
1998+
# Nesting through *values*: unlike keys, values are not hashed at
1999+
# construction time, so hashing recurses the full depth.
2000+
# See https://github.com/python/cpython/issues/154318.
2001+
fd = frozendict({0: 0})
2002+
for _ in range(500_000):
2003+
fd = frozendict({0: fd})
2004+
with self.assertRaisesRegex(RecursionError,
2005+
"while hashing a frozendict"):
2006+
hash(fd)
2007+
19932008
@support.cpython_only
19942009
def test_hash_cpython(self):
19952010
# Check that hash(frozendict) implementation is:

0 commit comments

Comments
 (0)