-
-
Notifications
You must be signed in to change notification settings - Fork 425
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic looks good; there's a couple of stray comments that can be cleaned up, but it seems sound.
The tests, however, need to be beefed up.
batavia/VirtualMachine.js
Outdated
); | ||
} else if (obj.__getattr__ instanceof Function) { | ||
val = obj.__getattr__(attr); | ||
} else if (obj.__getattr__ instanceof types.Function) { // Shouldn't these already be methods? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - the Function is only bound to the instance as part of the getattr process. The class has a single, common types.Function
instance; each object instance gets a separate types.Method instance. In theory, this could be cached after first access, or at time of object construction; but Method is such a lightweight class it doesn't really matter.
tests/structures/test_class.py
Outdated
@@ -46,6 +46,23 @@ def __str__(self): | |||
print('Done.') | |||
""", run_in_function=False) | |||
|
|||
# @unittest.expectedFailure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment can be removed, too.
tests/structures/test_class.py
Outdated
print(obj.fail) | ||
print(obj.y) | ||
""") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test will pass; but it's not a good test. If you actually run this, you'll get exceptions because the y
on line 58 is an unknown variable. The error will be consistent between Python and Batavia, so the test passes; but it's not fully testing like you think it is.
It also doesn't test __getattribute__
, which was the major contribution of this patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that, in view of the current implementation and that this is Javascript, you should also add tests in the spirit of my negative example above (setting __getattr__
and __getattribute__
on the instance to verify that it doesn't work).
Ah, of course, I should've checked in python before actually adding the tests :D |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are better; but they're still a bit opaque, and missing a bunch of paths through the code. They also don't do any validation that attr
is being passed around correctly.
Another good test of __getattr__
et al would to give a demonstration of the descriptor protocol - that's an example of the code in active use.
batavia/VirtualMachine.js
Outdated
@@ -1300,15 +1300,26 @@ VirtualMachine.prototype.byte_COMPARE_OP = function(opnum) { | |||
VirtualMachine.prototype.byte_LOAD_ATTR = function(attr) { | |||
var obj = this.pop(); | |||
var val; | |||
if (obj.__getattr__ === undefined) { | |||
|
|||
if (obj.__getattribute__ === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be wrong -- we should be checking for __getattr__
and __getattribute__
on the class, not on the instance:
>>> class A:
... pass
...
>>> a = A()
>>> a.__getattr__ = lambda x:x
>>>
>>> a.asdasdsad
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'asdasdsad'
>>> A.__getattr__ = lambda instance,x:x
>>> a.asdasdsad
'asdasdsad'
Correct me if I'm wrong, but to do anything useful (including a decorator protocol) with user-defined |
Hi, @lucantrop, are you still working on this? mind a little collaboration? |
Yeah, sure, I was just going to ask for some help. In the testbed, the following code:
prints:
which indicates that the object didn't inherit everything it should've from the class.
prints
I suspect the problem lies somewhere in the |
It mostly works like cpython now, but there are a few fixes left to do. |
@freakboy3742 this is (finally 😄) finished, so you can review it again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👊
Made it so
byte_LOAD_ATTR
properly handles__getattribute__
and__getattr__
.