[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Fred L. Drake, Jr.
Fred L. Drake, Jr. added the comment: > ref objects behave differently: they inherit their referent's hash > value when alive, and remember it. proxy objects could be made to > behave the same way. They could, yes, but that would break the proxy behavior, and the hash <--> equality behavior fo

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Antoine Pitrou
Antoine Pitrou added the comment: > So consider what happens if you forward the __hash__ method to the > proxied object: the hash will change when the object dies. ref objects behave differently: they inherit their referent's hash value when alive, and remember it. proxy objects could be made

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Fred L. Drake, Jr.
Fred L. Drake, Jr. added the comment: Clearly I've been away from this code for a long time. The hash support for ref objects is definitely a very special case, only intended to support WeakKeyDictionary. We that class implemented in C, we'd probably want the hash support for refs not to be e

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Gareth Rees
Gareth Rees added the comment: > I don't see any reason for proxy objects to be less hashable than ref objects. The difference is that unlike a ref object, a proxy object is supposed to forward its method calls to the proxied object. So consider what happens if you forward the __hash__ method

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Fred L. Drake, Jr.
Fred L. Drake, Jr. added the comment: I don't see any reason for proxy objects to be less hashable than ref objects. As for the p == p case, where the referent has expired, returning True if p is p seems acceptable (along with False inequalities, and True for other comparisons allowing equalit

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Antoine Pitrou
Antoine Pitrou added the comment: Ok, PEP 205 explains it a bit more: "the resulting proxy cannot be used as a dictionary key since it cannot be compared once the referent has expired, and comparability is necessary for dictionary keys. Operations on proxy objects after the referent dies cause

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Antoine Pitrou
Antoine Pitrou added the comment: Still, weakref.ref objects are hashable, so I don't understand why weakref.proxy would be different. Hashability of weakref.ref has been added in 29aa832b8787, to support weak dictionaries. See issue403985. It simply wasn't discussed whether to also add hasha

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Gareth Rees
Gareth Rees added the comment: The documentation says that weakref.Proxy objects are not hashable because "this avoids a number of problems related to their fundamentally mutable nature, and prevent their use as dictionary keys". Hashable objects must be immutable, otherwise the hash might cha

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Antoine Pitrou
Antoine Pitrou added the comment: > weakref.Proxy objects are not hashable regardless of the referent Interesting. @fdrake, do you remember why that is? -- ___ Python tracker __

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Gareth Rees
Gareth Rees added the comment: Hashable is particularly misleading, because "weakref.Proxy objects are not hashable regardless of the referent". -- ___ Python tracker ___ __

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Antoine Pitrou
Antoine Pitrou added the comment: Those methods are defined precisely so that they can be delegated. Since virtually anything can be proxied, the weakref.proxy *class* cannot tell upfront whether the proxied object will be an Iterator or not. I don't really see how to get around that. ---

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-28 Thread Gareth Rees
Gareth Rees added the comment: Not just Iterator, but Container, Hashable, Iterable, and Sized too! >>> import weakref >>> class C: pass >>> o = C() >>> w = weakref.proxy(o) >>> from collections.abc import * >>> isinstance(w, Container) True >>> isinstance(w, Hash

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-27 Thread Ned Deily
Changes by Ned Deily : -- nosy: +fdrake, pitrou ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.py

[issue24067] Weakproxy is an instance of collections.Iterator

2015-04-27 Thread Eyal Reuveni
New submission from Eyal Reuveni: Calling weakref.proxy() on an object returns something that is an instance of collections.Iterator, regardless of whether the object is an instance of collections.Iterator or even if the object itself is iterable. To reproduce, run the following code (verified