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 }}
This repository has been archived by the owner on May 31, 2020. It is now read-only.
This should throw an error like AttributeError: 'int' object has no attribute 'abs', but it doesn't, and perhaps the reason is because x.abs() is the same as abs(x) once the function has been invoked on the java side. Not surprisingly, this is also problematic for x.range() and x.type().
The text was updated successfully, but these errors were encountered:
Someone asked about this bug on Gitter; here's my response for anyone who wants to go digging:
The builtins are all defined in python/common/org/Python.java - abs() is defined around line 427.
However, invoking integer.abs() is an attribute lookup - VOC is going to use __getattr__() on the object instance (the integer) to find the abs attribute (which will be a method - an instance of org.python.types.Method), and then call invoke() on that method.
The underlying cause here is a little complex, and relates to function binding. If you have a class with an instance method with a method called foo():
class MyClass:
def foo(self):
...
you can create an instance of MyClass, and call foo on in:
x = MyClass()
x.foo()
However, from Python’s perspective, that is exactly equivalent to:
x = MyClass()
MyClass.foo(x)
“binding” a method to an instance just fills in the first argument for you (i.e., self) - but you can still invoke the original method by providing all the arguments, including self.
Now, in the case of foo, that’s fine - because we look for a method called foo, and find it. However, in the case of abs - we look for a method called abs, and don’t find it - but because abs is a valid builtin method, VOC finds a method that matches, and invokes it, passing in the first argument (the instance).
So, even though there isn’t an abs() instance method on Integers, x.abs() succeeds, because there is an abs() builtin that has an equivalent prototype.
We need to work out why, when doing lookup of attributes on an instance, the lookup is falling back on builtins.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
This should throw an error like
AttributeError: 'int' object has no attribute 'abs'
, but it doesn't, and perhaps the reason is becausex.abs()
is the same asabs(x)
once the function has been invoked on the java side. Not surprisingly, this is also problematic forx.range()
andx.type()
.The text was updated successfully, but these errors were encountered: