Skip to content

Commit 67748f1

Browse files
[3.12] gh-106970: Fix Argument Clinic 'destination <name> clear' command (GH-106972) (#106983)
Add test for the 'destination <name> clear' command, and the 'destination' directive in general. Fix two bugs in 'destination <name> clear' command: 1. The text attribute of the allocator is called 'text', not '_text' 2. Return after processing the 'clear' command, instead of proceeding directly to the fail(). (cherry picked from commit 3372bcb) Co-authored-by: Erlend E. Aasland <erlend@python.org>
1 parent 9cbde7c commit 67748f1

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

Lib/test/clinic.test.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4954,3 +4954,59 @@ Test_meth_coexist(TestObj *self, PyObject *Py_UNUSED(ignored))
49544954
static PyObject *
49554955
Test_meth_coexist_impl(TestObj *self)
49564956
/*[clinic end generated code: output=808a293d0cd27439 input=2a1d75b5e6fec6dd]*/
4957+
4958+
4959+
/*[clinic input]
4960+
output push
4961+
output preset buffer
4962+
[clinic start generated code]*/
4963+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5bff3376ee0df0b5]*/
4964+
4965+
/*[clinic input]
4966+
buffer_clear
4967+
a: int
4968+
We'll call 'destination buffer clear' after this.
4969+
4970+
Argument Clinic's buffer preset puts most generated code into the
4971+
'buffer' destination, except from 'impl_definition', which is put into
4972+
the 'block' destination, so we should expect everything but
4973+
'impl_definition' to be cleared.
4974+
[clinic start generated code]*/
4975+
4976+
static PyObject *
4977+
buffer_clear_impl(PyObject *module, int a)
4978+
/*[clinic end generated code: output=f14bba74677e1846 input=a4c308a6fdab043c]*/
4979+
4980+
/*[clinic input]
4981+
destination buffer clear
4982+
output pop
4983+
[clinic start generated code]*/
4984+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f20d06adb8252084]*/
4985+
4986+
4987+
/*[clinic input]
4988+
output push
4989+
destination test1 new buffer
4990+
output everything suppress
4991+
output docstring_definition test1
4992+
[clinic start generated code]*/
4993+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5a77c454970992fc]*/
4994+
4995+
/*[clinic input]
4996+
new_dest
4997+
a: int
4998+
Only this docstring should be outputted to test1.
4999+
[clinic start generated code]*/
5000+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da5af421ed8996ed]*/
5001+
5002+
/*[clinic input]
5003+
dump test1
5004+
output pop
5005+
[clinic start generated code]*/
5006+
5007+
PyDoc_STRVAR(new_dest__doc__,
5008+
"new_dest($module, /, a)\n"
5009+
"--\n"
5010+
"\n"
5011+
"Only this docstring should be outputted to test1.");
5012+
/*[clinic end generated code: output=9cac703f51d90e84 input=090db8df4945576d]*/

Lib/test/test_clinic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ def test_cpp_monitor_fail_no_matching_if(self):
249249
out = self.expect_failure(raw)
250250
self.assertEqual(out, msg)
251251

252+
def test_unknown_destination_command(self):
253+
out = self.expect_failure("""
254+
/*[clinic input]
255+
destination buffer nosuchcommand
256+
[clinic start generated code]*/
257+
""")
258+
msg = "unknown destination command 'nosuchcommand'"
259+
self.assertIn(msg, out)
260+
252261

253262
class ClinicGroupPermuterTest(TestCase):
254263
def _test(self, l, m, r, output):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix bugs in the Argument Clinic ``destination <name> clear`` command; the
2+
destination buffers would never be cleared, and the ``destination``
3+
directive parser would simply continue to the fault handler after processing
4+
the command. Patch by Erlend E. Aasland.

Tools/clinic/clinic.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,7 +1906,7 @@ def __getitem__(self, i):
19061906

19071907
def clear(self):
19081908
for ta in self._array:
1909-
ta._text.clear()
1909+
ta.text.clear()
19101910

19111911
def dump(self):
19121912
texts = [ta.output() for ta in self._array]
@@ -4366,13 +4366,13 @@ def directive_destination(
43664366
command: str,
43674367
*args
43684368
) -> None:
4369-
if command == 'new':
4370-
self.clinic.add_destination(name, *args)
4371-
return
4372-
4373-
if command == 'clear':
4374-
self.clinic.get_destination(name).clear()
4375-
fail("unknown destination command", repr(command))
4369+
match command:
4370+
case "new":
4371+
self.clinic.add_destination(name, *args)
4372+
case "clear":
4373+
self.clinic.get_destination(name).clear()
4374+
case _:
4375+
fail("unknown destination command", repr(command))
43764376

43774377

43784378
def directive_output(

0 commit comments

Comments
 (0)