voc
voc copied to clipboard
AttributeError when using self.__class__
It looks like the common idiom self.__class__
doesn't work with VOC classes.
Steps to reproduce:
Create a file testclass.py with the following code:
class Hello:
def hello(self):
print('hello, from class %s' % self.__class__.__name__)
Hello().hello()
Expected behavior:
$ python testclass.py
hello, from class Hello
However, this is what you get using VOC:
$ voc -v testclass.py
Compiling testclass.py ...
Adding default main method...
Writing ./python/testclass/__init__.class ...
Writing ./python/testclass/Hello.class ...
$ java -cp ../voc/dist/python-java-support.jar:. python.testclass.__init__
Exception in thread "main" AttributeError: 'Hello' object has no attribute '__class__'
at org.python.types.Object.__getattribute__(Object.java:288)
at python.testclass.Hello.hello(testclass.py:3)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.python.types.Function.invoke(Function.java:391)
at org.python.types.Method.invoke(Method.java:48)
at python.testclass.__init__.module$import(testclass.py:5)
at python.testclass.__init__.main(testclass.py)
Environment details:
$ javac -version
javac 1.8.0_121
$ python --version
Python 3.4.4
I was looking into fixing this adding @org.python.Attribute
to the __class__
attribute of Object.
After doing that, I tried executing the following code:
class MyClass:
pass
print(MyClass().__class__)
print(MyClass().__class__.__name__)
And this time, the output is still not the expect, but it's a bit more interesting:
+ java -cp ../voc/dist/python-java-support.jar:. python.testclass.__init__
<class 'MyClass'>
__main__
The expected would be:
$ python testclass.py
<class '__main__.MyClass'>
MyClass
So, for some reason, getting the name attribute for a class ends up being __main__
, but not sure if it's a bug on setting the attribute or getting it. :)
I am starting work on this issue.
Why It is not adding default main method as clearly seen in your output. ?
The above code exposes class and sets name correctly for classes
It fails on this however:
E - <class 'test.MyClass'>
E ? ^^^^
E + <class '__main__.MyClass'>
E ? ^^^^^^^^
E MyClass
E ===end of test===
E : Global context
The type is not setting module to main when it is being run as such. Have yet been able to figure out exactly how to do that though.