| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -411,6 +411,26 @@ def __iter__(self): | |||
| 411 | 411 | # can be killed in the middle of the call | |
| 412 | 412 | "blech" in p | |
| 413 | 413 | ||
| 414 | + def test_proxy_reversed(self): | ||
| 415 | + class MyObj: | ||
| 416 | + def __len__(self): | ||
| 417 | + return 3 | ||
| 418 | + def __reversed__(self): | ||
| 419 | + return iter('cba') | ||
| 420 | + | ||
| 421 | + obj = MyObj() | ||
| 422 | + self.assertEqual("".join(reversed(weakref.proxy(obj))), "cba") | ||
| 423 | + | ||
| 424 | + def test_proxy_hash(self): | ||
| 425 | + cool_hash = 299_792_458 | ||
| 426 | + | ||
| 427 | + class MyObj: | ||
| 428 | + def __hash__(self): | ||
| 429 | + return cool_hash | ||
| 430 | + | ||
| 431 | + obj = MyObj() | ||
| 432 | + self.assertEqual(hash(weakref.proxy(obj)), cool_hash) | ||
| 433 | + | ||
| 414 | 434 | def test_getweakrefcount(self): | |
| 415 | 435 | o = C() | |
| 416 | 436 | ref1 = weakref.ref(o) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Add pass-throughs for :func:`hash` and :func:`reversed` to | ||
| 2 | + :class:`weakref.proxy` objects. Patch by Pablo Galindo. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -665,10 +665,12 @@ proxy_iternext(PyWeakReference *proxy) | |||
| 665 | 665 | ||
| 666 | 666 | ||
| 667 | 667 | WRAP_METHOD(proxy_bytes, __bytes__) | |
| 668 | + WRAP_METHOD(proxy_reversed, __reversed__) | ||
| 668 | 669 | ||
| 669 | 670 | ||
| 670 | 671 | static PyMethodDef proxy_methods[] = { | |
| 671 | 672 | {"__bytes__", proxy_bytes, METH_NOARGS}, | |
| 673 | + {"__reversed__", proxy_reversed, METH_NOARGS}, | ||
| 672 | 674 | {NULL, NULL} | |
| 673 | 675 | }; | |
| 674 | 676 | ||
@@ -730,6 +732,21 @@ static PyMappingMethods proxy_as_mapping = { | |||
| 730 | 732 | }; | |
| 731 | 733 | ||
| 732 | 734 | ||
| 735 | + static Py_hash_t | ||
| 736 | + proxy_hash(PyObject *self) | ||
| 737 | + { | ||
| 738 | + PyWeakReference *proxy = (PyWeakReference *)self; | ||
| 739 | + if (!proxy_checkref(proxy)) { | ||
| 740 | + return -1; | ||
| 741 | + } | ||
| 742 | + PyObject *obj = PyWeakref_GET_OBJECT(proxy); | ||
| 743 | + Py_INCREF(obj); | ||
| 744 | + Py_hash_t res = PyObject_Hash(obj); | ||
| 745 | + Py_DECREF(obj); | ||
| 746 | + return res; | ||
| 747 | + } | ||
| 748 | + | ||
| 749 | + | ||
| 733 | 750 | PyTypeObject | |
| 734 | 751 | _PyWeakref_ProxyType = { | |
| 735 | 752 | PyVarObject_HEAD_INIT(&PyType_Type, 0) | |
@@ -746,7 +763,7 @@ _PyWeakref_ProxyType = { | |||
| 746 | 763 | &proxy_as_number, /* tp_as_number */ | |
| 747 | 764 | &proxy_as_sequence, /* tp_as_sequence */ | |
| 748 | 765 | &proxy_as_mapping, /* tp_as_mapping */ | |
| 749 | - 0, /* tp_hash */ | ||
| 766 | + proxy_hash, /* tp_hash */ | ||
| 750 | 767 | 0, /* tp_call */ | |
| 751 | 768 | proxy_str, /* tp_str */ | |
| 752 | 769 | proxy_getattr, /* tp_getattro */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments