Skip to content

Instantly share code, notes, and snippets.

@EnigmaCurry
Created May 1, 2011 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EnigmaCurry/950696 to your computer and use it in GitHub Desktop.
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 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