niner / inline-python-pm

Inline::Python - Write Perl subs and classes in Python.
https://metacpan.org/release/Inline-Python
20 stars 13 forks source link

Correcting KeyError to AttributeError in __getattr__ #4

Closed lmazuel closed 10 years ago

lmazuel commented 10 years ago

Hi,

To be conform to Python doc and standard, __getattr__ has to raise an AttributeError if attribute is not there and not a KeyError. Example of test in Python 2.7:

>>> class Foo: pass
>>> Foo().test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute 'test'

I have tested too in Python 2.5, the older Python distrib than Inline Python talks about in the documentation.

To give you a little context, I'm currently finishing to port Inline Python to Python 3. In P3, __getattr__ MUST raise AttributeError if the attribute does not exist, otherwise our "getattr" implementation is not correct (i.e. hasattr raises an exception, do not answer False) . Since it's even the standard in Python 2.x, I think it's interesting to correct it directly in Inline Python core, without a "if py_version == 3" condition.

In the next days, I will try to make more pull requests of things indirectly linked to the Python 3 port. My main goal is to make the more atomic commits that I can. I think it's a good practice and in addition easier for you to check.

Hope it helps!