diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index e0a48dd9021fb8..dfbf237c275769 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -4296,6 +4296,7 @@ def test_cli_converters(self): int() long() object() + Py_hash_t() Py_ssize_t() size_t() unsigned_int() diff --git a/Objects/clinic/weakrefobject.c.h b/Objects/clinic/weakrefobject.c.h new file mode 100644 index 00000000000000..9b1917f5597d68 --- /dev/null +++ b/Objects/clinic/weakrefobject.c.h @@ -0,0 +1,21 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() + +static Py_hash_t +weakref_hash_impl(PyWeakReference *self); + +static Py_hash_t +weakref_hash(PyObject *self) +{ + Py_hash_t return_value = -1; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = weakref_hash_impl((PyWeakReference *)self); + Py_END_CRITICAL_SECTION(); + + return return_value; +} +/*[clinic end generated code: output=40453fff9335a3f7 input=a9049054013a1b77]*/ diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index ca55fe32875086..56007d4da12734 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -7,6 +7,13 @@ #include "pycore_pystate.h" #include "pycore_weakref.h" // _PyWeakref_GET_REF() +/*[clinic input] +class weakref "PyWeakReference *" "&_PyWeakref_RefType" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad66c8b0acee6ad9]*/ + +#include "clinic/weakrefobject.c.h" + #ifdef Py_GIL_DISABLED /* * Thread-safety for free-threaded builds @@ -186,8 +193,14 @@ weakref_vectorcall(PyObject *self, PyObject *const *args, return obj; } +/*[clinic input] +@critical_section +weakref.__hash__ +[clinic start generated code]*/ + static Py_hash_t -weakref_hash_lock_held(PyWeakReference *self) +weakref_hash_impl(PyWeakReference *self) +/*[clinic end generated code: output=4904eacaabf902c9 input=703596f4b120e47c]*/ { if (self->hash != -1) return self->hash; @@ -201,17 +214,6 @@ weakref_hash_lock_held(PyWeakReference *self) return self->hash; } -static Py_hash_t -weakref_hash(PyObject *op) -{ - PyWeakReference *self = _PyWeakref_CAST(op); - Py_hash_t hash; - Py_BEGIN_CRITICAL_SECTION(self); - hash = weakref_hash_lock_held(self); - Py_END_CRITICAL_SECTION(); - return hash; -} - static PyObject * weakref_repr(PyObject *self) { diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index af12c28b9c5045..a55b0a6e427478 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -51,7 +51,6 @@ __getattr__ __getattribute__ __gt__ -__hash__ __iand__ __ifloordiv__ __ilshift__ diff --git a/Tools/clinic/libclinic/return_converters.py b/Tools/clinic/libclinic/return_converters.py index 4134d8e065ec43..6747f85e63f858 100644 --- a/Tools/clinic/libclinic/return_converters.py +++ b/Tools/clinic/libclinic/return_converters.py @@ -152,6 +152,12 @@ class Py_ssize_t_return_converter(long_return_converter): conversion_fn = 'PyLong_FromSsize_t' +class Py_hash_t_return_converter(long_return_converter): + type = 'Py_hash_t' + conversion_fn = 'PyLong_FromSsize_t' + cast = '(Py_ssize_t)' + + class size_t_return_converter(long_return_converter): type = 'size_t' conversion_fn = 'PyLong_FromSize_t' diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index 173f9eebc15bb2..153272e7c24156 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -42,6 +42,7 @@ def make_return_converter(self) -> CReturnConverter: '__bool__': SlotSpec(return_converter='int'), '__contains__': SlotSpec(return_converter='int'), '__getitem__': SlotSpec(return_converter='object'), + '__hash__': SlotSpec(return_converter='Py_hash_t'), '__iadd__': SlotSpec(return_converter='object'), '__imul__': SlotSpec(return_converter='object'), '__len__': SlotSpec(return_converter='Py_ssize_t'),