mirror of
https://github.com/python/cpython.git
synced 2026-10-28 10:25:05 +08:00
gh-114258: Use Argument Clinic for `__bool__` (GH-160057)
This commit is contained in:
1 parent
3a3d39c7df
commit
c58b7136b2
6 files changed
+53
-33
No files matched your search
@@ -15,7 +15,6 @@ import os.path
|
||||
import re
|
||||
import sys
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import warnings
|
||||
|
||||
test_tools.skip_if_missing('clinic')
|
||||
@@ -35,7 +34,7 @@ with test_tools.imports_under_tool('clinic'):
|
||||
from libclinic.block_parser import Block, BlockParser
|
||||
from libclinic.codegen import BlockPrinter, Destination
|
||||
from libclinic.dsl_parser import DSLParser
|
||||
from libclinic.slots import SlotSpec, SLOTS
|
||||
from libclinic.slots import SLOTS
|
||||
from libclinic.cli import parse_file, Clinic
|
||||
|
||||
|
||||
@@ -856,9 +855,8 @@ class ClinicWholeFileTest(TestCase):
|
||||
self.assertNotIn(text, generated)
|
||||
|
||||
def test_slot_with_parameters_and_return_type(self):
|
||||
# Slots which take arguments or return something else than an
|
||||
# object are not supported yet; check the machinery with them.
|
||||
slots = {'__bool__': SlotSpec(return_converter='int')}
|
||||
# Check the machinery with slots which take arguments or return
|
||||
# something else than an object.
|
||||
block = """
|
||||
/*[clinic input]
|
||||
output everything block
|
||||
@@ -874,8 +872,7 @@ class ClinicWholeFileTest(TestCase):
|
||||
Foo.__bool__
|
||||
[clinic start generated code]*/
|
||||
"""
|
||||
with mock.patch.dict(SLOTS, slots):
|
||||
generated = self.clinic.parse(dedent(block))
|
||||
generated = self.clinic.parse(dedent(block))
|
||||
self.assertIn(dedent("""
|
||||
static int
|
||||
Foo_contains(PyObject *self, PyObject *value)
|
||||
|
||||
+8
-12
@@ -145,10 +145,16 @@ dbm_length_impl(dbmobject *self)
|
||||
return dp->di_size;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
@critical_section
|
||||
_dbm.dbm.__bool__ as dbm_bool
|
||||
[clinic start generated code]*/
|
||||
|
||||
static int
|
||||
dbm_bool_lock_held(PyObject *self)
|
||||
dbm_bool_impl(dbmobject *self)
|
||||
/*[clinic end generated code: output=b550ce1ae52d6ad1 input=212af765b6e60719]*/
|
||||
{
|
||||
dbmobject *dp = dbmobject_CAST(self);
|
||||
dbmobject *dp = self;
|
||||
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
|
||||
assert(state != NULL);
|
||||
|
||||
@@ -177,16 +183,6 @@ dbm_bool_lock_held(PyObject *self)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
dbm_bool(PyObject *self)
|
||||
{
|
||||
int result;
|
||||
Py_BEGIN_CRITICAL_SECTION(self);
|
||||
result = dbm_bool_lock_held(self);
|
||||
Py_END_CRITICAL_SECTION();
|
||||
return result;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
@critical_section
|
||||
_dbm.dbm.__getitem__ as dbm_subscript
|
||||
|
||||
+8
-12
@@ -189,10 +189,16 @@ gdbm_length_impl(gdbmobject *self)
|
||||
return dp->di_size;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
@critical_section
|
||||
_gdbm.gdbm.__bool__ as gdbm_bool
|
||||
[clinic start generated code]*/
|
||||
|
||||
static int
|
||||
gdbm_bool_lock_held(PyObject *op)
|
||||
gdbm_bool_impl(gdbmobject *self)
|
||||
/*[clinic end generated code: output=91316ca18acb945c input=bf2878ff5e311848]*/
|
||||
{
|
||||
gdbmobject *dp = _gdbmobject_CAST(op);
|
||||
gdbmobject *dp = self;
|
||||
_gdbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
|
||||
if (dp->di_dbm == NULL) {
|
||||
PyErr_SetString(state->gdbm_error, "GDBM object has already been closed");
|
||||
@@ -219,16 +225,6 @@ gdbm_bool_lock_held(PyObject *op)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
gdbm_bool(PyObject *op)
|
||||
{
|
||||
int result;
|
||||
Py_BEGIN_CRITICAL_SECTION(op);
|
||||
result = gdbm_bool_lock_held(op);
|
||||
Py_END_CRITICAL_SECTION();
|
||||
return result;
|
||||
}
|
||||
|
||||
// Wrapper function for PyArg_Parse(o, "s#", &d.dptr, &d.size).
|
||||
// This function is needed to support PY_SSIZE_T_CLEAN.
|
||||
// Return 1 on success, same to PyArg_Parse().
|
||||
|
||||
Generated
+16
-1
@@ -21,6 +21,21 @@ dbm_length(PyObject *self)
|
||||
return return_value;
|
||||
}
|
||||
|
||||
static int
|
||||
dbm_bool_impl(dbmobject *self);
|
||||
|
||||
static int
|
||||
dbm_bool(PyObject *self)
|
||||
{
|
||||
int return_value = -1;
|
||||
|
||||
Py_BEGIN_CRITICAL_SECTION(self);
|
||||
return_value = dbm_bool_impl((dbmobject *)self);
|
||||
Py_END_CRITICAL_SECTION();
|
||||
|
||||
return return_value;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
dbm_subscript_impl(dbmobject *self, PyObject *key);
|
||||
|
||||
@@ -309,4 +324,4 @@ skip_optional:
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
/*[clinic end generated code: output=23fc8f92cd569b86 input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=4fa1cc840de2abd3 input=a9049054013a1b77]*/
|
||||
Generated
+16
-1
@@ -21,6 +21,21 @@ gdbm_length(PyObject *self)
|
||||
return return_value;
|
||||
}
|
||||
|
||||
static int
|
||||
gdbm_bool_impl(gdbmobject *self);
|
||||
|
||||
static int
|
||||
gdbm_bool(PyObject *self)
|
||||
{
|
||||
int return_value = -1;
|
||||
|
||||
Py_BEGIN_CRITICAL_SECTION(self);
|
||||
return_value = gdbm_bool_impl((gdbmobject *)self);
|
||||
Py_END_CRITICAL_SECTION();
|
||||
|
||||
return return_value;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
gdbm_subscript_impl(gdbmobject *self, PyObject *key);
|
||||
|
||||
@@ -461,4 +476,4 @@ skip_optional:
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
/*[clinic end generated code: output=8cc26c8a6790ecac input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=f4c81f6e2181a474 input=a9049054013a1b77]*/
|
||||
@@ -39,6 +39,7 @@ class SlotSpec:
|
||||
# not be in dsl_parser.unsupported_special_methods.
|
||||
SLOTS: Final[dict[str, SlotSpec]] = {
|
||||
'__add__': SlotSpec(return_converter='object'),
|
||||
'__bool__': SlotSpec(return_converter='int'),
|
||||
'__contains__': SlotSpec(return_converter='int'),
|
||||
'__getitem__': SlotSpec(return_converter='object'),
|
||||
'__iadd__': SlotSpec(return_converter='object'),
|
||||
|
||||
Reference in new issue
Block a user