Skip to content

Commit bc09e60

Browse files
committed
gh-64373: Convert _functools to Argument Clinic
1 parent 95e271b commit bc09e60

File tree

6 files changed

+582
-83
lines changed

6 files changed

+582
-83
lines changed

Include/internal/pycore_global_strings.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ struct _Py_global_strings {
263263
STRUCT_FOR_ID(c_call)
264264
STRUCT_FOR_ID(c_exception)
265265
STRUCT_FOR_ID(c_return)
266+
STRUCT_FOR_ID(cache_info_type)
266267
STRUCT_FOR_ID(cached_statements)
267268
STRUCT_FOR_ID(cadata)
268269
STRUCT_FOR_ID(cafile)
@@ -454,10 +455,12 @@ struct _Py_global_strings {
454455
STRUCT_FOR_ID(maxdigits)
455456
STRUCT_FOR_ID(maxevents)
456457
STRUCT_FOR_ID(maxmem)
458+
STRUCT_FOR_ID(maxsize)
457459
STRUCT_FOR_ID(maxsplit)
458460
STRUCT_FOR_ID(maxvalue)
459461
STRUCT_FOR_ID(memLevel)
460462
STRUCT_FOR_ID(memlimit)
463+
STRUCT_FOR_ID(memo)
461464
STRUCT_FOR_ID(message)
462465
STRUCT_FOR_ID(metaclass)
463466
STRUCT_FOR_ID(method)
@@ -468,6 +471,7 @@ struct _Py_global_strings {
468471
STRUCT_FOR_ID(modules)
469472
STRUCT_FOR_ID(mro)
470473
STRUCT_FOR_ID(msg)
474+
STRUCT_FOR_ID(mycmp)
471475
STRUCT_FOR_ID(n)
472476
STRUCT_FOR_ID(n_arg)
473477
STRUCT_FOR_ID(n_fields)
@@ -584,6 +588,7 @@ struct _Py_global_strings {
584588
STRUCT_FOR_ID(src_dir_fd)
585589
STRUCT_FOR_ID(stacklevel)
586590
STRUCT_FOR_ID(start)
591+
STRUCT_FOR_ID(state)
587592
STRUCT_FOR_ID(statement)
588593
STRUCT_FOR_ID(status)
589594
STRUCT_FOR_ID(stderr)
@@ -623,12 +628,14 @@ struct _Py_global_strings {
623628
STRUCT_FOR_ID(twice)
624629
STRUCT_FOR_ID(txt)
625630
STRUCT_FOR_ID(type)
631+
STRUCT_FOR_ID(typed)
626632
STRUCT_FOR_ID(tz)
627633
STRUCT_FOR_ID(uid)
628634
STRUCT_FOR_ID(unlink)
629635
STRUCT_FOR_ID(unraisablehook)
630636
STRUCT_FOR_ID(uri)
631637
STRUCT_FOR_ID(usedforsecurity)
638+
STRUCT_FOR_ID(user_function)
632639
STRUCT_FOR_ID(value)
633640
STRUCT_FOR_ID(values)
634641
STRUCT_FOR_ID(version)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_functools.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import gc
1818
from weakref import proxy
1919
import contextlib
20+
from inspect import Signature
2021

2122
from test.support import import_helper
2223
from test.support import threading_helper
@@ -434,6 +435,16 @@ def __str__(self):
434435
self.assertIn('astr', r)
435436
self.assertIn("['sth']", r)
436437

438+
def test_partial_signature(self):
439+
self.assertEqual(
440+
str(Signature.from_callable(self.partial.__reduce__)),
441+
'(self, /)',
442+
)
443+
self.assertEqual(
444+
str(Signature.from_callable(self.partial.__setstate__)),
445+
'(self, /, state)',
446+
)
447+
437448

438449
class TestPartialPy(TestPartial, unittest.TestCase):
439450
partial = py_functools.partial
@@ -941,6 +952,9 @@ def mycmp(x, y):
941952
self.assertRaises(TypeError, hash, k)
942953
self.assertNotIsInstance(k, collections.abc.Hashable)
943954

955+
def test_cmp_to_signature(self):
956+
self.assertEqual(str(Signature.from_callable(self.cmp_to_key)), '(mycmp)')
957+
944958

945959
@unittest.skipUnless(c_functools, 'requires the C _functools module')
946960
class TestCmpToKeyC(TestCmpToKey, unittest.TestCase):
@@ -1853,6 +1867,13 @@ def test_staticmethod(x):
18531867
for ref in refs:
18541868
self.assertIsNone(ref())
18551869

1870+
def test_common_signatures(self):
1871+
def orig(): ...
1872+
lru = self.module.lru_cache(1)(orig)
1873+
1874+
self.assertEqual(str(Signature.from_callable(lru.cache_info)), '()')
1875+
self.assertEqual(str(Signature.from_callable(lru.cache_clear)), '()')
1876+
18561877

18571878
@py_functools.lru_cache()
18581879
def py_cached_func(x, y):
@@ -1890,6 +1911,13 @@ def cached_meth(self, x, y):
18901911
def cached_staticmeth(x, y):
18911912
return 3 * x + y
18921913

1914+
def test_c_only_signatures(self):
1915+
lru = self.module._lru_cache_wrapper
1916+
1917+
self.assertEqual(str(Signature.from_callable(lru.__copy__)), '(self, /)')
1918+
self.assertEqual(str(Signature.from_callable(lru.__deepcopy__)), '(self, /, memo)')
1919+
self.assertEqual(str(Signature.from_callable(lru.__reduce__)), '(self, /)')
1920+
18931921

18941922
class TestSingleDispatch(unittest.TestCase):
18951923
def test_simple_overloads(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert :mod:`_functools` to argument clinic.

0 commit comments

Comments
 (0)