Created
May 1, 2011 18:08
-
-
Save EnigmaCurry/950696 to your computer and use it in GitHub Desktop.
PyPy 1.5 bug where call to __getitem__ differs from CPython 2.7.1
This file contains 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
# This is a test case to describe a bug I'm seeing in PyPy 1.5. I have | |
# a Cache object that is a dictionary that supports lookup via regular | |
# attribute access. For instance: | |
# | |
# >>> c = Cache() | |
# >>> c["asdf"] = "asdf" | |
# >>> c.asdf == c["asdf"] | |
# True | |
# | |
# When looking up keys via attribute, PyPy 1.5 calls __getitem__ | |
# whereas CPython 2.7.1 does not. | |
class Cache(dict): | |
"A dictionary that supports attribute style key lookup" | |
def __init__(self, **kw): | |
dict.__init__(self, kw) | |
self.__dict__ = self | |
class Test(Cache): | |
def __getitem__(self, item): | |
# I want to process items differently than attributes: | |
raise Exception("Trying to getitem: %s" % item) | |
if __name__ == "__main__": | |
t = Test() | |
t["asdf"] = "asdf" | |
#CPython does not call __getitem__ .. PyPy does: | |
print t.asdf | |
#Doesn't matter if it's a member of __dict__ or not: | |
print t.__getattribute__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment