← 返回首页
bpo-40523: Add pass-throughs for hash() and reversed() to weakref.proxy objects by pablogsal · Pull Request #19946 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
20 changes: 20 additions & 0 deletions Lib/test/test_weakref.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,26 @@ def __iter__(self):
# can be killed in the middle of the call
"blech" in p

def test_proxy_reversed(self):
class MyObj:
def __len__(self):
return 3
def __reversed__(self):
return iter('cba')

obj = MyObj()
self.assertEqual("".join(reversed(weakref.proxy(obj))), "cba")

def test_proxy_hash(self):
cool_hash = 299_792_458

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

The fastest hash you will ever see 😉


class MyObj:
def __hash__(self):
return cool_hash

obj = MyObj()
self.assertEqual(hash(weakref.proxy(obj)), cool_hash)

def test_getweakrefcount(self):
o = C()
ref1 = weakref.ref(o)
Expand Down
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add pass-throughs for :func:`hash` and :func:`reversed` to
:class:`weakref.proxy` objects. Patch by Pablo Galindo.
19 changes: 18 additions & 1 deletion Objects/weakrefobject.c
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};

Expand Down Expand Up @@ -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)
Expand All @@ -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 */
Expand Down
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.