Skip to content

Commit 3b3c692

Browse files
authored
gh-114258: Use Argument Clinic for __hash__ (gh-160059)
1 parent 867e24f commit 3b3c692

6 files changed

Lines changed: 43 additions & 13 deletions

File tree

‎Lib/test/test_clinic.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,6 +4296,7 @@ def test_cli_converters(self):
42964296
int()
42974297
long()
42984298
object()
4299+
Py_hash_t()
42994300
Py_ssize_t()
43004301
size_t()
43014302
unsigned_int()

‎Objects/clinic/weakrefobject.c.h‎

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/weakrefobject.c‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
#include "pycore_pystate.h"
88
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
99

10+
/*[clinic input]
11+
class weakref "PyWeakReference *" "&_PyWeakref_RefType"
12+
[clinic start generated code]*/
13+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad66c8b0acee6ad9]*/
14+
15+
#include "clinic/weakrefobject.c.h"
16+
1017
#ifdef Py_GIL_DISABLED
1118
/*
1219
* Thread-safety for free-threaded builds
@@ -186,8 +193,14 @@ weakref_vectorcall(PyObject *self, PyObject *const *args,
186193
return obj;
187194
}
188195

196+
/*[clinic input]
197+
@critical_section
198+
weakref.__hash__
199+
[clinic start generated code]*/
200+
189201
static Py_hash_t
190-
weakref_hash_lock_held(PyWeakReference *self)
202+
weakref_hash_impl(PyWeakReference *self)
203+
/*[clinic end generated code: output=4904eacaabf902c9 input=703596f4b120e47c]*/
191204
{
192205
if (self->hash != -1)
193206
return self->hash;
@@ -201,17 +214,6 @@ weakref_hash_lock_held(PyWeakReference *self)
201214
return self->hash;
202215
}
203216

204-
static Py_hash_t
205-
weakref_hash(PyObject *op)
206-
{
207-
PyWeakReference *self = _PyWeakref_CAST(op);
208-
Py_hash_t hash;
209-
Py_BEGIN_CRITICAL_SECTION(self);
210-
hash = weakref_hash_lock_held(self);
211-
Py_END_CRITICAL_SECTION();
212-
return hash;
213-
}
214-
215217
static PyObject *
216218
weakref_repr(PyObject *self)
217219
{

‎Tools/clinic/libclinic/dsl_parser.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
__getattr__
5252
__getattribute__
5353
__gt__
54-
__hash__
5554
__iand__
5655
__ifloordiv__
5756
__ilshift__

‎Tools/clinic/libclinic/return_converters.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class Py_ssize_t_return_converter(long_return_converter):
152152
conversion_fn = 'PyLong_FromSsize_t'
153153

154154

155+
class Py_hash_t_return_converter(long_return_converter):
156+
type = 'Py_hash_t'
157+
conversion_fn = 'PyLong_FromSsize_t'
158+
cast = '(Py_ssize_t)'
159+
160+
155161
class size_t_return_converter(long_return_converter):
156162
type = 'size_t'
157163
conversion_fn = 'PyLong_FromSize_t'

‎Tools/clinic/libclinic/slots.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def make_return_converter(self) -> CReturnConverter:
4242
'__bool__': SlotSpec(return_converter='int'),
4343
'__contains__': SlotSpec(return_converter='int'),
4444
'__getitem__': SlotSpec(return_converter='object'),
45+
'__hash__': SlotSpec(return_converter='Py_hash_t'),
4546
'__iadd__': SlotSpec(return_converter='object'),
4647
'__imul__': SlotSpec(return_converter='object'),
4748
'__len__': SlotSpec(return_converter='Py_ssize_t'),

0 commit comments

Comments
 (0)