File tree 3 files changed +15
-20
lines changed
Misc/NEWS.d/next/Core and Builtins
3 files changed +15
-20
lines changed Original file line number Diff line number Diff line change @@ -422,14 +422,20 @@ def __reversed__(self):
422
422
self .assertEqual ("" .join (reversed (weakref .proxy (obj ))), "cba" )
423
423
424
424
def test_proxy_hash (self ):
425
- cool_hash = 299_792_458
426
-
427
425
class MyObj :
428
426
def __hash__ (self ):
429
- return cool_hash
427
+ return 42
428
+
429
+ obj = MyObj ()
430
+ with self .assertRaises (TypeError ):
431
+ hash (weakref .proxy (obj ))
432
+
433
+ class MyObj :
434
+ __hash__ = None
430
435
431
436
obj = MyObj ()
432
- self .assertEqual (hash (weakref .proxy (obj )), cool_hash )
437
+ with self .assertRaises (TypeError ):
438
+ hash (weakref .proxy (obj ))
433
439
434
440
def test_getweakrefcount (self ):
435
441
o = C ()
Original file line number Diff line number Diff line change
1
+ Remove the pass-through for :func: `hash ` of :class: `weakref.proxy ` objects
2
+ to prevent unintended consequences when the original referred object
3
+ dies while the proxy is part of a hashable object. Patch by Pablo Galindo.
Original file line number Diff line number Diff line change @@ -732,21 +732,6 @@ static PyMappingMethods proxy_as_mapping = {
732
732
};
733
733
734
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
-
750
735
PyTypeObject
751
736
_PyWeakref_ProxyType = {
752
737
PyVarObject_HEAD_INIT (& PyType_Type , 0 )
@@ -763,7 +748,8 @@ _PyWeakref_ProxyType = {
763
748
& proxy_as_number , /* tp_as_number */
764
749
& proxy_as_sequence , /* tp_as_sequence */
765
750
& proxy_as_mapping , /* tp_as_mapping */
766
- proxy_hash , /* tp_hash */
751
+ // Notice that tp_hash is intentionally omitted as proxies are "mutable" (when the reference dies).
752
+ 0 , /* tp_hash */
767
753
0 , /* tp_call */
768
754
proxy_str , /* tp_str */
769
755
proxy_getattr , /* tp_getattro */
You can’t perform that action at this time.
0 commit comments