diff --git a/Lib/test/clinic.test.c b/Lib/test/clinic.test.c index 3dca8b8d1ed9b99..2ac153ac43e7029 100644 --- a/Lib/test/clinic.test.c +++ b/Lib/test/clinic.test.c @@ -5431,6 +5431,12 @@ Test_property_set(PyObject *self, PyObject *value, void *Py_UNUSED(context)) { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'property' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } return_value = Test_property_set_impl((TestObj *)self, value); return return_value; @@ -5438,7 +5444,40 @@ Test_property_set(PyObject *self, PyObject *value, void *Py_UNUSED(context)) static int Test_property_set_impl(TestObj *self, PyObject *value) -/*[clinic end generated code: output=49f925ab2a33b637 input=3bc3f46a23c83a88]*/ +/*[clinic end generated code: output=ec103a151cf51d25 input=3bc3f46a23c83a88]*/ + +/*[clinic input] +@setter +@deleter +Test.settable_and_deletable +[clinic start generated code]*/ + +#if !defined(Test_settable_and_deletable_DOCSTR) +# define Test_settable_and_deletable_DOCSTR NULL +#endif +#if defined(TEST_SETTABLE_AND_DELETABLE_GETSETDEF) +# undef TEST_SETTABLE_AND_DELETABLE_GETSETDEF +# define TEST_SETTABLE_AND_DELETABLE_GETSETDEF {"settable_and_deletable", (getter)Test_settable_and_deletable_get, (setter)Test_settable_and_deletable_set, Test_settable_and_deletable_DOCSTR}, +#else +# define TEST_SETTABLE_AND_DELETABLE_GETSETDEF {"settable_and_deletable", NULL, (setter)Test_settable_and_deletable_set, NULL}, +#endif + +static int +Test_settable_and_deletable_set_impl(TestObj *self, PyObject *value); + +static int +Test_settable_and_deletable_set(PyObject *self, PyObject *value, void *Py_UNUSED(context)) +{ + int return_value; + + return_value = Test_settable_and_deletable_set_impl((TestObj *)self, value); + + return return_value; +} + +static int +Test_settable_and_deletable_set_impl(TestObj *self, PyObject *value) +/*[clinic end generated code: output=479986d499b2f56d input=f5647f3511b9daea]*/ /*[clinic input] @setter @@ -5463,6 +5502,12 @@ Test_setter_first_with_docstr_set(PyObject *self, PyObject *value, void *Py_UNUS { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'setter_first_with_docstr' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } return_value = Test_setter_first_with_docstr_set_impl((TestObj *)self, value); return return_value; @@ -5470,7 +5515,7 @@ Test_setter_first_with_docstr_set(PyObject *self, PyObject *value, void *Py_UNUS static int Test_setter_first_with_docstr_set_impl(TestObj *self, PyObject *value) -/*[clinic end generated code: output=5aaf44373c0af545 input=31a045ce11bbe961]*/ +/*[clinic end generated code: output=eac8bafcaa50aa51 input=31a045ce11bbe961]*/ /*[clinic input] @getter diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 1dc1c4eaaaba196..f0dc62967f6a776 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -794,6 +794,102 @@ def test_ignore_preprocessor_in_comments(self): """) self.clinic.parse(raw) + def test_getset_in_ifdef(self): + block = """ + /*[clinic input] + output everything block + class Foo "FooObject *" "&Foo_Type" + [clinic start generated code]*/ + #ifdef CONDITION + /*[clinic input] + @getter + Foo.property + [clinic start generated code]*/ + /*[clinic input] + @setter + Foo.property + [clinic start generated code]*/ + #endif + """ + generated = self.clinic.parse(dedent(block)) + self.assertIn("#if defined(CONDITION)", generated) + # The getset is undefined if the condition is false. + self.assertIn("#ifndef FOO_PROPERTY_GETSETDEF\n" + " #define FOO_PROPERTY_GETSETDEF\n" + "#endif /* !defined(FOO_PROPERTY_GETSETDEF) */", + generated) + + def test_getset_duplicate(self): + for annotation in "@getter", "@setter": + with self.subTest(annotation=annotation): + self.clinic = _make_clinic(filename="test.c") + block = f""" + /*[clinic input] + class Foo "FooObject *" "&Foo_Type" + [clinic start generated code]*/ + /*[clinic input] + {annotation} + Foo.property + [clinic start generated code]*/ + /*[clinic input] + {annotation} + Foo.property + [clinic start generated code]*/ + """ + kind = 'setter' if annotation == '@setter' else 'getter' + err = f"Cannot apply @{kind} to 'Foo.property' twice" + self.expect_failure(block, err, lineno=10) + + def test_getset_different_c_basename(self): + block = """ + /*[clinic input] + class Foo "FooObject *" "&Foo_Type" + [clinic start generated code]*/ + /*[clinic input] + @getter + Foo.property as foo_get + [clinic start generated code]*/ + /*[clinic input] + @setter + Foo.property as foo_set + [clinic start generated code]*/ + """ + err = "The accessors of 'Foo.property' must have the same C basename" + self.expect_failure(block, err, lineno=10) + + def test_setter_deletion_check(self): + block = """ + /*[clinic input] + output everything block + class Foo "FooObject *" "&Foo_Type" + [clinic start generated code]*/ + /*[clinic input] + @setter + Foo.property + [clinic start generated code]*/ + """ + generated = self.clinic.parse(dedent(block)) + self.assertIn("if (value == NULL) {", generated) + self.assertIn("\"attribute 'property' of '%.100s' objects " + "cannot be deleted\"", generated) + + def test_deleter(self): + # @deleter means that the setter is called with NULL to delete + # the attribute, so it checks the value itself. + block = """ + /*[clinic input] + output everything block + class Foo "FooObject *" "&Foo_Type" + [clinic start generated code]*/ + /*[clinic input] + @setter + @deleter + Foo.property + [clinic start generated code]*/ + """ + generated = self.clinic.parse(dedent(block)) + self.assertNotIn("if (value == NULL) {", generated) + def test_var_keyword_non_dict(self): err = "'var_keyword_object' is not a valid converter" block = """ @@ -2671,7 +2767,7 @@ class Foo "" "" {annotation} Foo.property -> int """ - expected_error = f"{annotation} method cannot define a return type" + expected_error = "@getter and @setter methods cannot define a return type" self.expect_failure(block, expected_error, lineno=3) block = f""" @@ -2682,7 +2778,7 @@ class Foo "" "" obj: int / """ - expected_error = f"{annotation} methods cannot define parameters" + expected_error = "@getter and @setter methods cannot define parameters" self.expect_failure(block, expected_error) def test_setter_docstring(self): @@ -2725,9 +2821,51 @@ class Foo "" "" {dup[1]} Foo.property -> int """ - expected_error = "Cannot apply both @getter and @setter to the same function!" + expected_error = (f"Can't set {dup[1]}, " + f"function is not a normal callable") self.expect_failure(block, expected_error, lineno=3) + def test_deleter_without_setter(self): + block = """ + module foo + class Foo "" "" + @deleter + Foo.property + """ + expected_error = "Can't set @deleter, @setter is not applied" + self.expect_failure(block, expected_error, lineno=2) + + block = """ + module foo + class Foo "" "" + @deleter + @setter + Foo.property + """ + self.expect_failure(block, expected_error, lineno=2) + + def test_deleter_twice(self): + block = """ + module foo + class Foo "" "" + @setter + @deleter + @deleter + Foo.property + """ + expected_error = "Cannot apply @deleter twice to the same function!" + self.expect_failure(block, expected_error, lineno=4) + + def test_setter_and_deleter(self): + function = self.parse_function(""" + module foo + class Foo "" "" + @setter + @deleter + Foo.property + """, signatures_in_block=3, function_index=2) + self.assertEqual(function.kind, FunctionKind.SETTER_AND_DELETER) + def test_getset_no_class(self): for annotation in "@getter", "@setter": with self.subTest(annotation=annotation): diff --git a/Misc/NEWS.d/next/Library/2026-08-14-12-46-07.gh-issue-113318.DYGQjo.rst b/Misc/NEWS.d/next/Library/2026-08-14-12-46-07.gh-issue-113318.DYGQjo.rst new file mode 100644 index 000000000000000..4cd4acd01886368 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-14-12-46-07.gh-issue-113318.DYGQjo.rst @@ -0,0 +1,5 @@ +Fix crashes when deleting an attribute whose setter is generated by Argument +Clinic and is not prepared for deletion, among them +:attr:`frame.f_trace_opcodes` and the ``context``, ``owner`` and ``session`` +attributes of ``_ssl._SSLSocket``. +Deleting such attribute now raises :exc:`AttributeError`. diff --git a/Misc/NEWS.d/next/Tools-Demos/2026-08-14-12-45-57.gh-issue-113318.G1B0oH.rst b/Misc/NEWS.d/next/Tools-Demos/2026-08-14-12-45-57.gh-issue-113318.G1B0oH.rst new file mode 100644 index 000000000000000..3ea0a37288fe880 --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2026-08-14-12-45-57.gh-issue-113318.G1B0oH.rst @@ -0,0 +1,6 @@ +Fix Argument Clinic for ``@getter`` and ``@setter`` in a preprocessor +conditional block. +It failed with an internal error. +Argument Clinic now also rejects the accessors of the same attribute with +different C basenames, and the same accessor defined twice, which silently +generated invalid or duplicated entries of :c:type:`PyGetSetDef`. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 41384b388142ccc..00b20901df69964 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1372,13 +1372,14 @@ _asyncio_Future__asyncio_future_blocking_get_impl(FutureObj *self) /*[clinic input] @critical_section @setter +@deleter _asyncio.Future._asyncio_future_blocking [clinic start generated code]*/ static int _asyncio_Future__asyncio_future_blocking_set_impl(FutureObj *self, PyObject *value) -/*[clinic end generated code: output=0686d1cb024a7453 input=3fd4a5f95df788b7]*/ +/*[clinic end generated code: output=0686d1cb024a7453 input=68cea090c8793dd4]*/ { if (future_ensure_alive(self)) { @@ -1420,12 +1421,13 @@ _asyncio_Future__log_traceback_get_impl(FutureObj *self) /*[clinic input] @critical_section @setter +@deleter _asyncio.Future._log_traceback [clinic start generated code]*/ static int _asyncio_Future__log_traceback_set_impl(FutureObj *self, PyObject *value) -/*[clinic end generated code: output=9ce8e19504f42f54 input=30ac8217754b08c2]*/ +/*[clinic end generated code: output=9ce8e19504f42f54 input=469dbdd15343d39f]*/ { if (value == NULL) { PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); @@ -1585,12 +1587,13 @@ _asyncio_Future__cancel_message_get_impl(FutureObj *self) /*[clinic input] @critical_section @setter +@deleter _asyncio.Future._cancel_message [clinic start generated code]*/ static int _asyncio_Future__cancel_message_set_impl(FutureObj *self, PyObject *value) -/*[clinic end generated code: output=0854b2f77bff2209 input=f461d17f2d891fad]*/ +/*[clinic end generated code: output=0854b2f77bff2209 input=68b3a24731dfb629]*/ { if (value == NULL) { PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); @@ -2443,12 +2446,13 @@ _asyncio_Task__log_destroy_pending_get_impl(TaskObj *self) /*[clinic input] @critical_section @setter +@deleter _asyncio.Task._log_destroy_pending [clinic start generated code]*/ static int _asyncio_Task__log_destroy_pending_set_impl(TaskObj *self, PyObject *value) -/*[clinic end generated code: output=7ebc030bb92ec5ce input=49b759c97d1216a4]*/ +/*[clinic end generated code: output=7ebc030bb92ec5ce input=31af83e8bf57ac6f]*/ { if (value == NULL) { PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index adfdf44e53604e3..34d3c1685c69fcb 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -598,13 +598,14 @@ _ctypes_CType_Type___pointer_type___get_impl(PyObject *self) /*[clinic input] @setter +@deleter _ctypes.CType_Type.__pointer_type__ [clinic start generated code]*/ static int _ctypes_CType_Type___pointer_type___set_impl(PyObject *self, PyObject *value) -/*[clinic end generated code: output=6259be8ea21693fa input=a05055fc7f4714b6]*/ +/*[clinic end generated code: output=6259be8ea21693fa input=7e24bceb1676349b]*/ { ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); StgInfo *info; @@ -1480,12 +1481,13 @@ class _ctypes.PyCArrayType_Type "CDataObject *" "clinic_state()->PyCArrayType_Ty /*[clinic input] @critical_section @setter +@deleter _ctypes.PyCArrayType_Type.raw [clinic start generated code]*/ static int _ctypes_PyCArrayType_Type_raw_set_impl(CDataObject *self, PyObject *value) -/*[clinic end generated code: output=cf9b2a9fd92e9ecb input=a3717561efc45efd]*/ +/*[clinic end generated code: output=cf9b2a9fd92e9ecb input=13881e1662127207]*/ { char *ptr; Py_ssize_t size; @@ -1550,12 +1552,13 @@ _ctypes_PyCArrayType_Type_value_get_impl(CDataObject *self) /*[clinic input] @critical_section @setter +@deleter _ctypes.PyCArrayType_Type.value [clinic start generated code]*/ static int _ctypes_PyCArrayType_Type_value_set_impl(CDataObject *self, PyObject *value) -/*[clinic end generated code: output=39ad655636a28dd5 input=e2e6385fc6ab1a29]*/ +/*[clinic end generated code: output=39ad655636a28dd5 input=167f0935cbb8d489]*/ { const char *ptr; Py_ssize_t size; @@ -3664,12 +3667,13 @@ _validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags, /*[clinic input] @critical_section @setter +@deleter _ctypes.CFuncPtr.errcheck [clinic start generated code]*/ static int _ctypes_CFuncPtr_errcheck_set_impl(PyCFuncPtrObject *self, PyObject *value) -/*[clinic end generated code: output=6580cf1ffdf3b9fb input=84930bb16c490b33]*/ +/*[clinic end generated code: output=6580cf1ffdf3b9fb input=bcd5d3ed1a0c36e9]*/ { if (value && !PyCallable_Check(value)) { PyErr_SetString(PyExc_TypeError, @@ -3701,13 +3705,14 @@ _ctypes_CFuncPtr_errcheck_get_impl(PyCFuncPtrObject *self) /*[clinic input] @setter +@deleter @critical_section _ctypes.CFuncPtr.restype [clinic start generated code]*/ static int _ctypes_CFuncPtr_restype_set_impl(PyCFuncPtrObject *self, PyObject *value) -/*[clinic end generated code: output=0be0a086abbabf18 input=683c3bef4562ccc6]*/ +/*[clinic end generated code: output=0be0a086abbabf18 input=ffc941a26dbb31f3]*/ { PyObject *checker; if (value == NULL) { @@ -3764,13 +3769,14 @@ _ctypes_CFuncPtr_restype_get_impl(PyCFuncPtrObject *self) /*[clinic input] @setter +@deleter @critical_section _ctypes.CFuncPtr.argtypes [clinic start generated code]*/ static int _ctypes_CFuncPtr_argtypes_set_impl(PyCFuncPtrObject *self, PyObject *value) -/*[clinic end generated code: output=596a36e2ae89d7d1 input=c4627573e980aa8b]*/ +/*[clinic end generated code: output=596a36e2ae89d7d1 input=fd012f1fd7cc35be]*/ { if (value == NULL || value == Py_None) { atomic_xsetref(&self->argtypes, NULL); @@ -5413,12 +5419,13 @@ class _ctypes.Simple "CDataObject *" "clinic_state()->Simple_Type" /*[clinic input] @critical_section @setter +@deleter _ctypes.Simple.value [clinic start generated code]*/ static int _ctypes_Simple_value_set_impl(CDataObject *self, PyObject *value) -/*[clinic end generated code: output=f267186118939863 input=977af9dc9e71e857]*/ +/*[clinic end generated code: output=f267186118939863 input=4e6c1143d17c2c3f]*/ { PyObject *result; diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index ea8ed2713d8a146..d48fb62ee0569b8 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -3399,12 +3399,13 @@ _io_TextIOWrapper__CHUNK_SIZE_get_impl(textio *self) /*[clinic input] @critical_section @setter +@deleter _io.TextIOWrapper._CHUNK_SIZE [clinic start generated code]*/ static int _io_TextIOWrapper__CHUNK_SIZE_set_impl(textio *self, PyObject *value) -/*[clinic end generated code: output=edb86d2db660a5ab input=32fc99861db02a0a]*/ +/*[clinic end generated code: output=edb86d2db660a5ab input=e7c44a734efeddc3]*/ { Py_ssize_t n; CHECK_ATTACHED_INT(self); diff --git a/Modules/_sqlite/clinic/cursor.c.h b/Modules/_sqlite/clinic/cursor.c.h index 3cad9f3aef5ecd5..689466b1c2b85a1 100644 --- a/Modules/_sqlite/clinic/cursor.c.h +++ b/Modules/_sqlite/clinic/cursor.c.h @@ -367,8 +367,14 @@ _sqlite3_Cursor_arraysize_set(PyObject *self, PyObject *value, void *Py_UNUSED(c { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'arraysize' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } return_value = _sqlite3_Cursor_arraysize_set_impl((pysqlite_Cursor *)self, value); return return_value; } -/*[clinic end generated code: output=a0e3ebba9e4d0ece input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e7b20358f8213fd7 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_ssl.c.h b/Modules/clinic/_ssl.c.h index e337ed2390a1fc4..62d52fc5f1aa5dd 100644 --- a/Modules/clinic/_ssl.c.h +++ b/Modules/clinic/_ssl.c.h @@ -386,6 +386,12 @@ _ssl__SSLSocket_context_set(PyObject *self, PyObject *value, void *Py_UNUSED(con { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'context' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLSocket_context_set_impl((PySSLSocket *)self, value); Py_END_CRITICAL_SECTION(); @@ -509,6 +515,12 @@ _ssl__SSLSocket_owner_set(PyObject *self, PyObject *value, void *Py_UNUSED(conte { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'owner' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLSocket_owner_set_impl((PySSLSocket *)self, value); Py_END_CRITICAL_SECTION(); @@ -914,6 +926,12 @@ _ssl__SSLSocket_session_set(PyObject *self, PyObject *value, void *Py_UNUSED(con { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'session' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLSocket_session_set_impl((PySSLSocket *)self, value); Py_END_CRITICAL_SECTION(); @@ -1342,6 +1360,12 @@ _ssl__SSLContext_verify_mode_set(PyObject *self, PyObject *value, void *Py_UNUSE { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'verify_mode' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext_verify_mode_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -1392,6 +1416,12 @@ _ssl__SSLContext_verify_flags_set(PyObject *self, PyObject *value, void *Py_UNUS { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'verify_flags' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext_verify_flags_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -1443,6 +1473,12 @@ _ssl__SSLContext_minimum_version_set(PyObject *self, PyObject *value, void *Py_U { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'minimum_version' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext_minimum_version_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -1494,6 +1530,12 @@ _ssl__SSLContext_maximum_version_set(PyObject *self, PyObject *value, void *Py_U { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'maximum_version' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext_maximum_version_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -1551,6 +1593,12 @@ _ssl__SSLContext_num_tickets_set(PyObject *self, PyObject *value, void *Py_UNUSE { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'num_tickets' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext_num_tickets_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -1633,6 +1681,12 @@ _ssl__SSLContext_options_set(PyObject *self, PyObject *value, void *Py_UNUSED(co { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'options' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext_options_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -1683,6 +1737,12 @@ _ssl__SSLContext__host_flags_set(PyObject *self, PyObject *value, void *Py_UNUSE { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute '_host_flags' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext__host_flags_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -1733,6 +1793,12 @@ _ssl__SSLContext_check_hostname_set(PyObject *self, PyObject *value, void *Py_UN { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'check_hostname' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext_check_hostname_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -2265,6 +2331,12 @@ _ssl__SSLContext_sni_callback_set(PyObject *self, PyObject *value, void *Py_UNUS { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'sni_callback' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = _ssl__SSLContext_sni_callback_set_impl((PySSLContext *)self, value); Py_END_CRITICAL_SECTION(); @@ -3326,4 +3398,4 @@ _ssl_enum_crls(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObje #ifndef _SSL_ENUM_CRLS_METHODDEF #define _SSL_ENUM_CRLS_METHODDEF #endif /* !defined(_SSL_ENUM_CRLS_METHODDEF) */ -/*[clinic end generated code: output=aef2e74b706c6106 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=3a5bdd8db17e32b1 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/frameobject.c.h b/Objects/clinic/frameobject.c.h index 327896f4b97c684..0ba1f3b6618ffef 100644 --- a/Objects/clinic/frameobject.c.h +++ b/Objects/clinic/frameobject.c.h @@ -265,6 +265,12 @@ frame_trace_opcodes_set(PyObject *self, PyObject *value, void *Py_UNUSED(context { int return_value; + if (value == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'f_trace_opcodes' of '%.100s' objects cannot be deleted", + Py_TYPE(self)->tp_name); + return -1; + } Py_BEGIN_CRITICAL_SECTION(self); return_value = frame_trace_opcodes_set_impl((PyFrameObject *)self, value); Py_END_CRITICAL_SECTION(); @@ -433,4 +439,4 @@ frame___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored)) return return_value; } -/*[clinic end generated code: output=74abf652547c0c11 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a35726b44114286a input=a9049054013a1b77]*/ diff --git a/Objects/exceptions.c b/Objects/exceptions.c index fb546ad2673576d..cc3e03baa4c7dfe 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -346,12 +346,13 @@ BaseException_args_get_impl(PyBaseExceptionObject *self) /*[clinic input] @critical_section @setter +@deleter BaseException.args [clinic start generated code]*/ static int BaseException_args_set_impl(PyBaseExceptionObject *self, PyObject *value) -/*[clinic end generated code: output=331137e11d8f9e80 input=2400047ea5970a84]*/ +/*[clinic end generated code: output=331137e11d8f9e80 input=177ad350c8b45219]*/ { PyObject *seq; if (value == NULL) { @@ -385,13 +386,14 @@ BaseException___traceback___get_impl(PyBaseExceptionObject *self) /*[clinic input] @critical_section @setter +@deleter BaseException.__traceback__ [clinic start generated code]*/ static int BaseException___traceback___set_impl(PyBaseExceptionObject *self, PyObject *value) -/*[clinic end generated code: output=a82c86d9f29f48f0 input=12676035676badad]*/ +/*[clinic end generated code: output=a82c86d9f29f48f0 input=53a1df586023d786]*/ { if (value == NULL) { PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted"); @@ -430,13 +432,14 @@ BaseException___context___get_impl(PyBaseExceptionObject *self) /*[clinic input] @critical_section @setter +@deleter BaseException.__context__ [clinic start generated code]*/ static int BaseException___context___set_impl(PyBaseExceptionObject *self, PyObject *value) -/*[clinic end generated code: output=b4cb52dcca1da3bd input=c0971adf47fa1858]*/ +/*[clinic end generated code: output=b4cb52dcca1da3bd input=fe79e7c0a0854004]*/ { if (value == NULL) { PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted"); @@ -473,13 +476,14 @@ BaseException___cause___get_impl(PyBaseExceptionObject *self) /*[clinic input] @critical_section @setter +@deleter BaseException.__cause__ [clinic start generated code]*/ static int BaseException___cause___set_impl(PyBaseExceptionObject *self, PyObject *value) -/*[clinic end generated code: output=6161315398aaf541 input=e1b403c0bde3f62a]*/ +/*[clinic end generated code: output=6161315398aaf541 input=3fdd9a0d1674abc9]*/ { if (value == NULL) { PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted"); diff --git a/Objects/frameobject.c b/Objects/frameobject.c index c50cbeaada3c406..2d07f1c2a6c4421 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -1643,12 +1643,13 @@ static bool frame_is_suspended(PyFrameObject *frame) /*[clinic input] @critical_section @setter +@deleter frame.f_lineno as frame_lineno [clinic start generated code]*/ static int frame_lineno_set_impl(PyFrameObject *self, PyObject *value) -/*[clinic end generated code: output=e64c86ff6be64292 input=36ed3c896b27fb91]*/ +/*[clinic end generated code: output=e64c86ff6be64292 input=c814c375c6bd16ba]*/ { PyCodeObject *code = _PyFrame_GetCode(self->f_frame); if (value == NULL) { @@ -1868,12 +1869,13 @@ frame_trace_get_impl(PyFrameObject *self) @permit_long_summary @critical_section @setter +@deleter frame.f_trace as frame_trace [clinic start generated code]*/ static int frame_trace_set_impl(PyFrameObject *self, PyObject *value) -/*[clinic end generated code: output=d6fe08335cf76ae4 input=e57380734815dac5]*/ +/*[clinic end generated code: output=d6fe08335cf76ae4 input=9fb7a5805196eae2]*/ { if (value == Py_None) { value = NULL; diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 0c1fab7f6d33a8a..0481adadf668f8d 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -926,12 +926,13 @@ function___annotate___get_impl(PyFunctionObject *self) /*[clinic input] @critical_section @setter +@deleter function.__annotate__ [clinic start generated code]*/ static int function___annotate___set_impl(PyFunctionObject *self, PyObject *value) -/*[clinic end generated code: output=05b7dfc07ada66cd input=eb6225e358d97448]*/ +/*[clinic end generated code: output=05b7dfc07ada66cd input=4bcfad0bdcfec768]*/ { if (value == NULL) { PyErr_SetString(PyExc_TypeError, @@ -980,12 +981,13 @@ function___annotations___get_impl(PyFunctionObject *self) /*[clinic input] @critical_section @setter +@deleter function.__annotations__ [clinic start generated code]*/ static int function___annotations___set_impl(PyFunctionObject *self, PyObject *value) -/*[clinic end generated code: output=a61795d4a95eede4 input=5302641f686f0463]*/ +/*[clinic end generated code: output=a61795d4a95eede4 input=71f6a58c00ac6745]*/ { if (value == Py_None) value = NULL; @@ -1025,12 +1027,13 @@ function___type_params___get_impl(PyFunctionObject *self) /*[clinic input] @critical_section @setter +@deleter function.__type_params__ [clinic start generated code]*/ static int function___type_params___set_impl(PyFunctionObject *self, PyObject *value) -/*[clinic end generated code: output=038b4cda220e56fb input=3862fbd4db2b70e8]*/ +/*[clinic end generated code: output=038b4cda220e56fb input=c0e33abc5901a2f5]*/ { /* Not legal to del f.__type_params__ or to set it to anything * other than a tuple object. */ diff --git a/Python/traceback.c b/Python/traceback.c index 5bfa28f9c7dc8b6..fe6a465bc64cc94 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -176,12 +176,13 @@ tb_lineno_get(PyObject *op, void *Py_UNUSED(_)) /*[clinic input] @critical_section @setter +@deleter traceback.tb_next [clinic start generated code]*/ static int traceback_tb_next_set_impl(PyTracebackObject *self, PyObject *value) -/*[clinic end generated code: output=d4868cbc48f2adac input=ce66367f85e3c443]*/ +/*[clinic end generated code: output=d4868cbc48f2adac input=936201ff689c5700]*/ { if (!value) { PyErr_Format(PyExc_TypeError, "can't delete tb_next attribute"); diff --git a/Tools/clinic/libclinic/clanguage.py b/Tools/clinic/libclinic/clanguage.py index a8473dba0512460..ab77e7ad6603cdc 100644 --- a/Tools/clinic/libclinic/clanguage.py +++ b/Tools/clinic/libclinic/clanguage.py @@ -13,7 +13,8 @@ from libclinic.function import ( Module, Class, Function, Parameter, ParamTuple, permute_optional_groups, - GETTER, SETTER, METHOD_INIT) + GETTER, METHOD_INIT, + ACCESSORS, SETTERS) from libclinic.converters import self_converter from libclinic.parse_args import ParseArgsCodeGen if TYPE_CHECKING: @@ -478,12 +479,12 @@ def render_function( full_name = f.full_name template_dict = {'full_name': full_name} template_dict['name'] = f.displayname - if f.kind in {GETTER, SETTER}: + if f.kind in ACCESSORS: template_dict['getset_name'] = f.c_basename.upper() template_dict['getset_basename'] = f.c_basename if f.kind is GETTER: template_dict['c_basename'] = f.c_basename + "_get" - elif f.kind is SETTER: + else: template_dict['c_basename'] = f.c_basename + "_set" # Implicitly add the setter value parameter. data.impl_parameters.append("PyObject *value") @@ -498,7 +499,7 @@ def render_function( for converter in converters: converter.set_template_dict(template_dict) - if f.kind not in {SETTER, METHOD_INIT}: + if f.kind not in SETTERS | {METHOD_INIT}: f.return_converter.render(f, data) template_dict['impl_return_type'] = f.return_converter.type diff --git a/Tools/clinic/libclinic/converters.py b/Tools/clinic/libclinic/converters.py index 76091a9eedc1bff..5539bd2e12e35f5 100644 --- a/Tools/clinic/libclinic/converters.py +++ b/Tools/clinic/libclinic/converters.py @@ -8,7 +8,7 @@ from libclinic.function import ( Function, Parameter, CALLABLE, STATIC_METHOD, CLASS_METHOD, METHOD_INIT, METHOD_NEW, - GETTER, SETTER) + ACCESSORS) from libclinic.codegen import CRenderData, TemplateDict from libclinic.converter import ( CConverter, legacy_converters, add_legacy_c_converter) @@ -1124,7 +1124,7 @@ def correct_name_for_self( f: Function, parser: bool = False ) -> tuple[str, str]: - if f.kind in {CALLABLE, METHOD_INIT, GETTER, SETTER}: + if f.kind in {CALLABLE, METHOD_INIT} | ACCESSORS: if f.cls: return "PyObject *", "self" return "PyObject *", "module" diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index 4dcbc815cc6f25b..a6b1d2bed5e5dee 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -18,7 +18,7 @@ Module, Class, Function, Parameter, FunctionKind, CALLABLE, STATIC_METHOD, CLASS_METHOD, METHOD_INIT, METHOD_NEW, - GETTER, SETTER) + ACCESSORS, SETTERS) from libclinic.converter import ( converters, legacy_converters) from libclinic.converters import ( @@ -447,21 +447,31 @@ def at_disable(self, *args: str) -> None: def at_getter(self) -> None: match self.kind: + case FunctionKind.CALLABLE: + self.kind = FunctionKind.GETTER case FunctionKind.GETTER: fail("Cannot apply @getter twice to the same function!") - case FunctionKind.SETTER: - fail("Cannot apply both @getter and @setter to the same function!") case _: - self.kind = FunctionKind.GETTER + fail("Can't set @getter, function is not a normal callable") def at_setter(self) -> None: match self.kind: - case FunctionKind.SETTER: + case FunctionKind.CALLABLE: + self.kind = FunctionKind.SETTER + case FunctionKind.SETTER | FunctionKind.SETTER_AND_DELETER: fail("Cannot apply @setter twice to the same function!") - case FunctionKind.GETTER: - fail("Cannot apply both @getter and @setter to the same function!") case _: - self.kind = FunctionKind.SETTER + fail("Can't set @setter, function is not a normal callable") + + def at_deleter(self) -> None: + match self.kind: + case FunctionKind.SETTER: + # The setter is called with NULL to delete the attribute. + self.kind = FunctionKind.SETTER_AND_DELETER + case FunctionKind.SETTER_AND_DELETER: + fail("Cannot apply @deleter twice to the same function!") + case _: + fail("Can't set @deleter, @setter is not applied") def at_staticmethod(self) -> None: if self.kind is not CALLABLE: @@ -592,7 +602,7 @@ def normalize_function_kind(self, fullname: str) -> None: fail(f"{name!r} must be a normal method; got '{self.kind}'!") if name == '__new__' and (self.kind is not CLASS_METHOD or not cls): fail("'__new__' must be a class method!") - if self.kind in {GETTER, SETTER} and not cls: + if self.kind in ACCESSORS and not cls: fail("@getter and @setter must be methods") # Normalise self.kind. @@ -605,8 +615,8 @@ def resolve_return_converter( self, full_name: str, forced_converter: str ) -> CReturnConverter: if forced_converter: - if self.kind in {GETTER, SETTER}: - fail(f"@{self.kind.name.lower()} method cannot define a return type") + if self.kind in ACCESSORS: + fail("@getter and @setter methods cannot define a return type") if self.kind is METHOD_INIT: fail("__init__ methods cannot define a return type") ast_input = f"def x() -> {forced_converter}: pass" @@ -626,7 +636,7 @@ def resolve_return_converter( except ValueError: fail(f"Badly formed annotation for {full_name!r}: {forced_converter!r}") - if self.kind in {METHOD_INIT, SETTER}: + if self.kind in {METHOD_INIT} | SETTERS: return int_return_converter() return CReturnConverter() @@ -732,6 +742,22 @@ def state_modulename_name(self, line: str) -> None: self.next(self.state_parameters_start) def add_function(self, func: Function) -> None: + if func.kind in ACCESSORS: + # The accessors of the same attribute are rendered into a single + # PyGetSetDef entry, which is identified by the C basename, so + # they must share it. + for other in (func.cls or func.module).functions: + if (other.kind in ACCESSORS + and other.full_name == func.full_name): + if (other.kind is func.kind + or {other.kind, func.kind} <= SETTERS): + kind = 'setter' if func.kind in SETTERS else 'getter' + fail(f"Cannot apply @{kind} to " + f"{func.full_name!r} twice") + if other.c_basename != func.c_basename: + fail(f"The accessors of {func.full_name!r} " + f"must have the same C basename") + # Insert a self converter automatically. tp, name = correct_name_for_self(func) if func.cls and tp == "PyObject *": @@ -814,9 +840,8 @@ def state_parameters_start(self, line: str) -> None: return self.next(self.state_function_docstring, line) assert self.function is not None - if self.function.kind in {GETTER, SETTER}: - getset = self.function.kind.name.lower() - fail(f"@{getset} methods cannot define parameters") + if self.function.kind in ACCESSORS: + fail("@getter and @setter methods cannot define parameters") self.parameter_continuation = '' return self.next(self.state_parameter, line) @@ -1358,7 +1383,7 @@ def format_docstring_signature( lines.append(f.displayname) if f.forced_text_signature: lines.append(f.forced_text_signature) - elif f.kind in {GETTER, SETTER}: + elif f.kind in ACCESSORS: # @getter and @setter do not need signatures like a method or a function. return '' else: @@ -1541,7 +1566,7 @@ def format_docstring(self) -> str: assert self.function is not None f = self.function # For the following special cases, it does not make sense to render a docstring. - if f.kind in {METHOD_INIT, METHOD_NEW, GETTER, SETTER} and not f.docstring: + if f.kind in {METHOD_INIT, METHOD_NEW} | ACCESSORS and not f.docstring: return f.docstring # Enforce the summary line! diff --git a/Tools/clinic/libclinic/function.py b/Tools/clinic/libclinic/function.py index 325633eb010608f..cad673045d1c26d 100644 --- a/Tools/clinic/libclinic/function.py +++ b/Tools/clinic/libclinic/function.py @@ -60,6 +60,7 @@ class FunctionKind(enum.Enum): METHOD_NEW = enum.auto() GETTER = enum.auto() SETTER = enum.auto() + SETTER_AND_DELETER = enum.auto() @functools.cached_property def new_or_init(self) -> bool: @@ -76,6 +77,12 @@ def __repr__(self) -> str: METHOD_NEW: Final = FunctionKind.METHOD_NEW GETTER: Final = FunctionKind.GETTER SETTER: Final = FunctionKind.SETTER +SETTER_AND_DELETER: Final = FunctionKind.SETTER_AND_DELETER + +# The kinds which implement the setter of an entry of PyGetSetDef. +SETTERS: Final = frozenset({SETTER, SETTER_AND_DELETER}) +# The kinds which implement an entry of PyGetSetDef. +ACCESSORS: Final = SETTERS | {GETTER} @dc.dataclass(repr=False) @@ -161,7 +168,7 @@ def methoddef_flags(self) -> str | None: case FunctionKind.STATIC_METHOD: flags.append('METH_STATIC') case _ as kind: - acceptable_kinds = {FunctionKind.CALLABLE, FunctionKind.GETTER, FunctionKind.SETTER} + acceptable_kinds = {FunctionKind.CALLABLE} | ACCESSORS assert kind in acceptable_kinds, f"unknown kind: {kind!r}" if self.coexist: flags.append('METH_COEXIST') diff --git a/Tools/clinic/libclinic/parse_args.py b/Tools/clinic/libclinic/parse_args.py index 0e99a89d74d7241..b08b949028205d2 100644 --- a/Tools/clinic/libclinic/parse_args.py +++ b/Tools/clinic/libclinic/parse_args.py @@ -5,7 +5,8 @@ from libclinic import fail, warn from libclinic.function import ( Function, Parameter, - GETTER, SETTER, METHOD_NEW) + GETTER, SETTER, METHOD_NEW, + ACCESSORS, SETTERS) from libclinic.converter import CConverter from libclinic.converters import ( defining_class_converter, object_converter, self_converter) @@ -188,6 +189,21 @@ def declare_parser( #define {methoddef_name} #endif /* !defined({methoddef_name}) */ """) +GETSETDEF_PROTOTYPE_IFNDEF: Final[str] = libclinic.normalize_snippet(""" + #ifndef {getset_name}_GETSETDEF + #define {getset_name}_GETSETDEF + #endif /* !defined({getset_name}_GETSETDEF) */ +""") +# The setter is called with NULL to delete the attribute. Unless @deleter is +# applied to it, deletion is rejected before the implementation is called. +SETTER_PREAMBLE: Final[str] = libclinic.normalize_snippet(""" + if (value == NULL) {{ + PyErr_Format(PyExc_AttributeError, + "attribute '{name}' of '%.100s' objects cannot be deleted", + Py_TYPE({self_name})->tp_name); + return -1; + }} +""", indent=4) class ParseArgsCodeGen: @@ -328,7 +344,7 @@ def select_prototypes(self) -> None: self.methoddef_define = GETTERDEF_PROTOTYPE_DEFINE if self.func.docstring: self.docstring_definition = GETSET_DOCSTRING_PROTOTYPE_STRVAR - elif self.func.kind is SETTER: + elif self.func.kind in SETTERS: if self.func.docstring: fail("docstrings are only supported for @getter, not @setter") self.return_value_declaration = "int {parser_retval};" @@ -387,9 +403,12 @@ def parse_no_args(self) -> None: if self.func.kind is GETTER: self.parser_prototype = PARSER_PROTOTYPE_GETTER parser_code = [] - elif self.func.kind is SETTER: + elif self.func.kind in SETTERS: self.parser_prototype = PARSER_PROTOTYPE_SETTER - parser_code = [] + if self.func.kind is SETTER: + parser_code = [SETTER_PREAMBLE] + else: + parser_code = [] elif not self.requires_defining_class: # no self.parameters, METH_NOARGS self.flags = "METH_NOARGS" @@ -921,7 +940,10 @@ def process_methoddef(self, clang: CLanguage) -> None: self.cpp_endif = "#endif /* " + conditional + " */" if self.methoddef_define and self.codegen.add_ifndef_symbol(self.func.full_name): - self.methoddef_ifndef = METHODDEF_PROTOTYPE_IFNDEF + if self.func.kind in ACCESSORS: + self.methoddef_ifndef = GETSETDEF_PROTOTYPE_IFNDEF + else: + self.methoddef_ifndef = METHODDEF_PROTOTYPE_IFNDEF def finalize(self, clang: CLanguage) -> None: # add ';' to the end of self.parser_prototype and self.impl_prototype