oracle / graalpython

A Python 3 implementation built on GraalVM
Other
1.2k stars 104 forks source link

[interop] __abstractmethods__ readable, but fails to be read #220

Closed fniephaus closed 1 year ago

fniephaus commented 3 years ago

The Python list type (and possibly others) list __abstractmethods__ as a member and claim that it is readable. Read member operations, however, fail with an exception.

Repro

$ polyglot --shell --jvm
GraalVM MultiLanguage Shell 21.1.0
Copyright (c) 2013-2020, Oracle and/or its affiliates
  Python version 3.8.5
  Ruby version 2.7.2
  Squeak/Smalltalk version 21.2.0-dev
ruby> Truffle::Interop.members(Polyglot.eval('python', 'list'), true).include?('__abstractmethods__')
true
ruby> Truffle::Interop.member_readable?(Polyglot.eval('python', 'list'), '__abstractmethods__')
true
ruby> Truffle::Interop.read_member(Polyglot.eval('python', 'list'), '__abstractmethods__')
Unknown identifier: __abstractmethods__ (NameError)
        at <ruby> <top (required)>(<internal:core> core/truffle/boot.rb:1:0-83)
        at <ruby> Kernel#eval(resource:/truffleruby/core/kernel.rb:325:10433-10495)
        at <ruby> Binding#eval(resource:/truffleruby/core/binding.rb:16:528-566)
        at <ruby> Context#eval(interactiveRubySource)(Unknown)
msimacek commented 3 years ago

The __abstractmethods__ descriptor is indeed there and it has a getter. When called in readMember, the getter raises an AttributeError that gets translated into the error you see. So the behavior is kinda conunterintuitive, but technically correct. The member is there, it has a getter and in general, we cannot know that the getter would throw the exception without invoking it and we can only do that in readMember because isMemeberReadable shouldn't have side-effects. You can even see a similar glitch in CPython, where it will autocomplete list.__abst to list.__abstractmethods__, which will then raise AttributeError.

Since we know what this particular getter does, we could hardcode some specific logic for it in our isMemberRedable. But I would prefer not to do that. Is it causing you real problems or is it something that you just noticed? Through which message did you see it listed?

fniephaus commented 3 years ago

Thanks for following up, @msimacek. This is not blocking for us, but it's something I came across when inspecting Python list with our interop object explorer. Maybe your findings should be added to the broader discussion of what members are (or should be) in Truffle languages. /cc @chumer

msimacek commented 1 year ago

It seems we didn't reach any action items here, so let's close this issue as "this is just how python works".