You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FrameLocalsProxy is stricter than dict about what constitutes a match #120906
The change in behaviour isn't a huge problem so if it's intended then I won't spend waste any time complaining about it, but I do think it's worth confirming that it is intended/desired.
Internally we check if the input key is an exact unicode, but we do utilize dict for certain features (repr for example). This causes an inconsistent behavior.
My preferred solution is to enforce any key to be an exact unicode string. The reason for that is, unlike a generic dict, FrameLocalsProxy should be a direct proxy for local variables and keys other than unicode string does not quite make sense. However, I know I'm the more aggressive type when it comes to changes so I'll list some concerns as well.
frame.f_locals for an unoptimized frame (module/class) is a real dict and can take any type of keys. There might also be a usage to use a subclass as keys? If the original usage is shown to be helpful, that might tip the scale for me.
I'm a bit worried about the can of worms when we allow subclasses of unicode strings - what if it overwrite the __hash__ and __eq__ methods? Will it cause more troubles than the benefits?
The Cython compiler wraps most of it's strings into an EncodedString class (for reasons that are possibly historic and which I don't quite understand). To clarify - that's a class used internally by the compiler, rather than a class used in Cython extension modules.
We have an inline method of compilation, and here missing variables are taken from the surrounding frame. The frame comes from Python so the variable names are just a normal str. However the names we're using to look them up are our EncodedString class. These used to work fine when used to look up keys (since they just matched the equivalent str) but now don't.
It's easy enough to work around so I'm relaxed about the solution whatever you decide to do. I think what we were doing was mostly accidental and there wasn't a hidden special use-case behind it.
But for this use-case I'm just reading existing values from the FrameLocalsProxy and not worried about storing subclassed strings. Obviously you need to consider both sides of it though.
To avoid breaking mapping invariants, if a given key compares equal to the name of a local variable, it needs to be handled as a lookup of that variable name. Having a PyUnicode_CheckExact guard on a string comparison fast path still makes sense, but the fallback needs to be a linear search using the abstract object comparison API rather than assuming the key is absent.
But that's not the mapping protocol right? What if a key has a different hash, but the equal value? What if the key is
classEvil:
def__eq__(self, other):
returnTrue
It's unhashable but only comparing equality will match it to an arbitrary local (the first one).
Are we going to implement the full mapping interface? Checking everything as dict/mapping does? The dark corner we left here might eat us in the future.
What if a key has a different hash, but the equal value
That's a violation of the hash protocol: objects that compare equal must have the same hash (or at least one must be unhashable).
The Evil example is incomplete, since __hash__ is not inherited if __eq__ is overridden.
But if Evil had __hash__ explicitly set to str.__hash__, it would be OK to rely on it and skip the call to __eq__.
IMO, it would also be OK to convert the argument to PyUnicode, and do the lookup with the resulting temporary object.
My worry about supporting a unicode subclass or an arbitrary object as a key is - it does not fit the mapping protocol. If an object overrides it's __eq__ but not __hash__, it can't be used as a key. If an object has a different hash to another one but also equal to that object, they will be different keys in the dict.
None of the proposals above have this feature. So basically, we are inventing a very new mapping protocol specifically for FrameLocalsproxy and it's not easy to explain - we don't have a simple enough rule.
That's why I liked "unicode only" solution - that's a simple rule that works and easy to understand. It will cause some trouble, but we are already causing troubles.
We need to consider - what if a key is a unicode subclass, but has different __eq__ and/or __hash__ method? It would be very difficult for us to behave exactly as a dict. What if it's not even a subclass of unicode?
If we can't do that, I kind of like what @encukou suggested - we convert the non-exact-unicode keys to unicode first, for both read and write. That's a golden rule that everyone can follow, and that will solve many benign cases like this very issue.
I finally looked at the code, rather than going just by the conversation here. But I couldn't just look...
Please see PR #122309, as a suggestion for how to handle this.
If an object overrides its __eq__ but not __hash__, it can't be used as a key. It's not hashable.
If an object has a different hash to another one but also equal to that object, that's a violation of the hash protocol, and dict can behave surprisingly (give weird results/exceptions, but not crash). We don't need to match that behavior.
The hash protocol:
limits the types (to hashable ones) and
allows dicts to do an optimization: they can look at a small subset of entries. We don't need to use that optimization.
Keys that aren't unicode subclasses shouldn't be a problem.
The data model docs in https://docs.python.org/3/reference/datamodel.html#object.__hash__ also state "The only required property is that objects which compare equal have the same hash value". However, they go into more detail about how to define __hash__ and __eq__ to be consistent with each other, as well as how to indicate when subclasses of hashable parent classes are not themselves hashable.
Classes that don't abide by those rules just straight up don't work properly, so the fact they won't misbehave as badly with locals proxy instances as they do with regular dicts isn't a problem we need to be concerned about.
Activity
gaogaotiantian commentedon Jun 23, 2024
Interesting issue. I don't have a definitive answer here but this is something we need to deal with, because we are getting some weird issues here:
Internally we check if the input key is an exact unicode, but we do utilize dict for certain features (
reprfor example). This causes an inconsistent behavior.My preferred solution is to enforce any key to be an exact unicode string. The reason for that is, unlike a generic
dict,FrameLocalsProxyshould be a direct proxy for local variables and keys other than unicode string does not quite make sense. However, I know I'm the more aggressive type when it comes to changes so I'll list some concerns as well.frame.f_localsfor an unoptimized frame (module/class) is a real dict and can take any type of keys. There might also be a usage to use a subclass as keys? If the original usage is shown to be helpful, that might tip the scale for me.I'm a bit worried about the can of worms when we allow subclasses of unicode strings - what if it overwrite the
__hash__and__eq__methods? Will it cause more troubles than the benefits?da-woods commentedon Jun 23, 2024
Just to explain the context I ran into it in:
The Cython compiler wraps most of it's strings into an
EncodedStringclass (for reasons that are possibly historic and which I don't quite understand). To clarify - that's a class used internally by the compiler, rather than a class used in Cython extension modules.We have an
inlinemethod of compilation, and here missing variables are taken from the surrounding frame. The frame comes from Python so the variable names are just a normalstr. However the names we're using to look them up are ourEncodedStringclass. These used to work fine when used to look up keys (since they just matched the equivalentstr) but now don't.It's easy enough to work around so I'm relaxed about the solution whatever you decide to do. I think what we were doing was mostly accidental and there wasn't a hidden special use-case behind it.
But for this use-case I'm just reading existing values from the
FrameLocalsProxyand not worried about storing subclassed strings. Obviously you need to consider both sides of it though.gaogaotiantian commentedon Jun 23, 2024
We need to discuss this further with @markshannon, but I think the desired behavior would be either
gaogaotiantian commentedon Jun 23, 2024
I just remembered that we already had that discussion when I was implementing PEP 667. The PEP suggests that we should allow arbitrary types.
Then the question would be what if the user gives a unicode-like key, do we try to access the fast variables with that key?
ncoghlan commentedon Jul 16, 2024
To avoid breaking mapping invariants, if a given key compares equal to the name of a local variable, it needs to be handled as a lookup of that variable name. Having a
PyUnicode_CheckExactguard on a string comparison fast path still makes sense, but the fallback needs to be a linear search using the abstract object comparison API rather than assuming the key is absent.gaogaotiantian commentedon Jul 16, 2024
But that's not the mapping protocol right? What if a key has a different hash, but the equal value? What if the key is
It's unhashable but only comparing equality will match it to an arbitrary local (the first one).
Are we going to implement the full mapping interface? Checking everything as dict/mapping does? The dark corner we left here might eat us in the future.
encukou commentedon Jul 17, 2024
That's a violation of the hash protocol: objects that compare equal must have the same hash (or at least one must be unhashable).
The
Evilexample is incomplete, since__hash__is not inherited if__eq__is overridden.But if
Evilhad__hash__explicitly set tostr.__hash__, it would be OK to rely on it and skip the call to__eq__.IMO, it would also be OK to convert the argument to
PyUnicode, and do the lookup with the resulting temporary object.1 remaining item
gaogaotiantian commentedon Jul 25, 2024
My worry about supporting a unicode subclass or an arbitrary object as a key is - it does not fit the mapping protocol. If an object overrides it's
__eq__but not__hash__, it can't be used as a key. If an object has a different hash to another one but also equal to that object, they will be different keys in the dict.None of the proposals above have this feature. So basically, we are inventing a very new mapping protocol specifically for
FrameLocalsproxyand it's not easy to explain - we don't have a simple enough rule.That's why I liked "unicode only" solution - that's a simple rule that works and easy to understand. It will cause some trouble, but we are already causing troubles.
We need to consider - what if a key is a unicode subclass, but has different
__eq__and/or__hash__method? It would be very difficult for us to behave exactly as a dict. What if it's not even a subclass of unicode?If we can't do that, I kind of like what @encukou suggested - we convert the non-exact-unicode keys to unicode first, for both read and write. That's a golden rule that everyone can follow, and that will solve many benign cases like this very issue.
pythongh-120906: Support arbitrary hashable keys in FrameLocalsProxy
encukou commentedon Jul 26, 2024
I finally looked at the code, rather than going just by the conversation here. But I couldn't just look...
Please see PR #122309, as a suggestion for how to handle this.
If an object overrides its
__eq__but not__hash__, it can't be used as a key. It's not hashable.If an object has a different hash to another one but also equal to that object, that's a violation of the hash protocol, and dict can behave surprisingly (give weird results/exceptions, but not crash). We don't need to match that behavior.
The hash protocol:
Keys that aren't unicode subclasses shouldn't be a problem.
gaogaotiantian commentedon Jul 26, 2024
Oh sorry I did not realize you posted in the issue. I don't think I'm getting notification from this issue.
Could you point to me the documentation about the hash protocol? I could not find it.
encukou commentedon Jul 27, 2024
See hashable in the docs:
ncoghlan commentedon Jul 27, 2024
The data model docs in https://docs.python.org/3/reference/datamodel.html#object.__hash__ also state "The only required property is that objects which compare equal have the same hash value". However, they go into more detail about how to define
__hash__and__eq__to be consistent with each other, as well as how to indicate when subclasses of hashable parent classes are not themselves hashable.Classes that don't abide by those rules just straight up don't work properly, so the fact they won't misbehave as badly with locals proxy instances as they do with regular dicts isn't a problem we need to be concerned about.
gh-120906: Support arbitrary hashable keys in FrameLocalsProxy (GH-1…
[3.13] pythongh-120906: Support arbitrary hashable keys in FrameLocal…
[3.13] pythongh-120906: Support arbitrary hashable keys in FrameLocal…
Yhg1s commentedon Jul 31, 2024
This issue is all fixed now, right?
[3.13] gh-120906: Support arbitrary hashable keys in FrameLocalsProxy (…
ncoghlan commentedon Aug 1, 2024
Indeed it is!
pythongh-120906: Support arbitrary hashable keys in FrameLocalsProxy (…