mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
bpo-40523: Add pass-throughs for hash() and reversed() to weakref.proxy objects (GH-19946)
This commit is contained in:
parent
1253c3ef70
commit
96074de573
3 changed files with 40 additions and 1 deletions
|
|
@ -665,10 +665,12 @@ proxy_iternext(PyWeakReference *proxy)
|
|||
|
||||
|
||||
WRAP_METHOD(proxy_bytes, __bytes__)
|
||||
WRAP_METHOD(proxy_reversed, __reversed__)
|
||||
|
||||
|
||||
static PyMethodDef proxy_methods[] = {
|
||||
{"__bytes__", proxy_bytes, METH_NOARGS},
|
||||
{"__reversed__", proxy_reversed, METH_NOARGS},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
@ -730,6 +732,21 @@ static PyMappingMethods proxy_as_mapping = {
|
|||
};
|
||||
|
||||
|
||||
static Py_hash_t
|
||||
proxy_hash(PyObject *self)
|
||||
{
|
||||
PyWeakReference *proxy = (PyWeakReference *)self;
|
||||
if (!proxy_checkref(proxy)) {
|
||||
return -1;
|
||||
}
|
||||
PyObject *obj = PyWeakref_GET_OBJECT(proxy);
|
||||
Py_INCREF(obj);
|
||||
Py_hash_t res = PyObject_Hash(obj);
|
||||
Py_DECREF(obj);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
PyTypeObject
|
||||
_PyWeakref_ProxyType = {
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
|
|
@ -746,7 +763,7 @@ _PyWeakref_ProxyType = {
|
|||
&proxy_as_number, /* tp_as_number */
|
||||
&proxy_as_sequence, /* tp_as_sequence */
|
||||
&proxy_as_mapping, /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
proxy_hash, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
proxy_str, /* tp_str */
|
||||
proxy_getattr, /* tp_getattro */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue