Skip to content

Commit 8d7fde6

Browse files
gh-116417: Argument Clinic: test generated Limited C API code for float args (#116573)
1 parent 4704e55 commit 8d7fde6

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

Lib/test/test_clinic.py

+56-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
from clinic import DSLParser
2222

2323

24-
def _make_clinic(*, filename='clinic_tests'):
24+
def _make_clinic(*, filename='clinic_tests', limited_capi=False):
2525
clang = clinic.CLanguage(filename)
26-
c = clinic.Clinic(clang, filename=filename, limited_capi=False)
26+
c = clinic.Clinic(clang, filename=filename, limited_capi=limited_capi)
2727
c.block_parser = clinic.BlockParser('', clang)
2828
return c
2929

@@ -3614,6 +3614,46 @@ def test_depr_multi(self):
36143614
self.assertRaises(TypeError, fn, a="a", b="b", c="c", d="d", e="e", f="f", g="g")
36153615

36163616

3617+
class LimitedCAPIOutputTests(unittest.TestCase):
3618+
3619+
def setUp(self):
3620+
self.clinic = _make_clinic(limited_capi=True)
3621+
3622+
@staticmethod
3623+
def wrap_clinic_input(block):
3624+
return dedent(f"""
3625+
/*[clinic input]
3626+
output everything buffer
3627+
{block}
3628+
[clinic start generated code]*/
3629+
/*[clinic input]
3630+
dump buffer
3631+
[clinic start generated code]*/
3632+
""")
3633+
3634+
def test_limited_capi_float(self):
3635+
block = self.wrap_clinic_input("""
3636+
func
3637+
f: float
3638+
/
3639+
""")
3640+
generated = self.clinic.parse(block)
3641+
self.assertNotIn("PyFloat_AS_DOUBLE", generated)
3642+
self.assertIn("float f;", generated)
3643+
self.assertIn("f = (float) PyFloat_AsDouble", generated)
3644+
3645+
def test_limited_capi_double(self):
3646+
block = self.wrap_clinic_input("""
3647+
func
3648+
f: double
3649+
/
3650+
""")
3651+
generated = self.clinic.parse(block)
3652+
self.assertNotIn("PyFloat_AS_DOUBLE", generated)
3653+
self.assertIn("double f;", generated)
3654+
self.assertIn("f = PyFloat_AsDouble", generated)
3655+
3656+
36173657
try:
36183658
import _testclinic_limited
36193659
except ImportError:
@@ -3644,6 +3684,20 @@ def test_my_int_sum(self):
36443684
with self.assertRaises(TypeError):
36453685
_testclinic_limited.my_int_sum(1, "str")
36463686

3687+
def test_my_double_sum(self):
3688+
for func in (
3689+
_testclinic_limited.my_float_sum,
3690+
_testclinic_limited.my_double_sum,
3691+
):
3692+
with self.subTest(func=func.__name__):
3693+
self.assertEqual(func(1.0, 2.5), 3.5)
3694+
with self.assertRaises(TypeError):
3695+
func()
3696+
with self.assertRaises(TypeError):
3697+
func(1)
3698+
with self.assertRaises(TypeError):
3699+
func(1., "2")
3700+
36473701

36483702

36493703
class PermutationTests(unittest.TestCase):

0 commit comments

Comments
 (0)