Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong error reported when accessing an attribute #696

Closed
aplavin opened this issue Jun 12, 2019 · 1 comment · Fixed by #997
Closed

Wrong error reported when accessing an attribute #696

aplavin opened this issue Jun 12, 2019 · 1 comment · Fixed by #997

Comments

@aplavin
Copy link
Contributor

aplavin commented Jun 12, 2019

Hi! I have a class that does some checking before setting an attribute value, like this (pure Python):

class A:
    def __setattr__(self, name, value):
        if value == 0:
            raise ValueError(value)
        # set value...

a = A()
a.a = 5  # OK
a.a = 0  # correct error message:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-7600552ca637> in <module>()
----> 1 a.a = 0

<ipython-input-6-585623315894> in __setattr__(self, name, value)
      2     def __setattr__(self, name, value):
      3         if value == 0:
----> 4             raise ValueError(value)
      5         # set value...
      6

ValueError: 0

However, running the same code with PyCall.jl gives a very confusing error message:

py"""
class A:
    def __setattr__(self, name, value):
        if value == 0:
            raise ValueError(value)
        # set value...
"""
a = py"A"()
a.a = 5  # OK
a.a = 0  # completely wrong exception:
ERROR: KeyError: key :a not found
Stacktrace:
 [1] setproperty!(::PyObject, ::Symbol, ::Int64) at /users/aplavin/.julia/packages/PyCall/ttONZ/src/PyCall.jl:329
 [2] top-level scope at none:0
@aplavin aplavin changed the title Wrong error reported when setting an attribute Wrong error reported when accessing an attribute Feb 19, 2021
@aplavin
Copy link
Contributor Author

aplavin commented Feb 19, 2021

Bump! In the meantime I noticed that the same KeyError is wrongly raised when getting an attribute as well, not only when setting.
Traceback points to this line:

p == C_NULL && throw(KeyError(s))
. I don't know Python internals, but looks like this code doesn't check for raised exceptions at all and raises KeyError no matter what.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant