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
dir, __dir__ and __getattr__ weirdness #3729
Comments
This can be simplified further. class Foo:
def __getattr__(self, name):
return 42
foo = Foo()
print(foo.bar) # Correctly returns 42
print(len(dir(foo))) # On my Unix MicroPython prints 687! It is the presence of |
A third partSince
then typing "f." followed by the tab key, causes this:
and micropython (unix) dies nastily: the terminal in Ubuntu it ran in looses its connection to the keyboard. I have to kill it by closing the window... |
This behaviour is a consequence of the "improved" dir and tab completion implemented in #3617. It would be worth reading the commit comments in 98647e8 and 165aab1 The
So there's an explicit caveat about undefined behaviour with Also:
MicroPython's implementation of dir attempts to be minimal and convenient, hence it probes all known names and returns those that the object responds to. Responding to other points above:
Yes, right, that's an omission. And probably if you define
Thanks for the report about this crash, it's definitely something that needs investigation... |
This patch fixes the possibility of a crash of the REPL when tab-completing an object which raises an exception when its attributes are accessed. See issue #3729.
This is in two parts
Here is a simple class
I would expect that using it like:
would produce:
At least that's what the various flavours of Anaconda give me. If I try the same in micropython, I get:
and quite a lot of lines ending with a list of seemingly all the names known to micropython.
Now if I modify the class defined before
by removing the redefinition of
__getattr__
:the same test code returns:
What I can deduce
Is that:
__dir__
hook is not implemented (not really a problem in itself);dir
is very partially implemented:__getattr__
seems to be iterated over the list of all the names in the current scope. This is definitely weird.The text was updated successfully, but these errors were encountered: