Skip to content
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

Internal AttributeError when accessing invalid attribute in Python #2387

Closed
PeterJCLaw opened this issue Oct 25, 2020 · 2 comments
Closed

Internal AttributeError when accessing invalid attribute in Python #2387

PeterJCLaw opened this issue Oct 25, 2020 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@PeterJCLaw
Copy link

Describe the Bug

If you happen to typo the name of a method or attribute in your Python controller you get an error which appears to come from the Webots controller rather than your code.

Steps to Reproduce

from controller import Robot

Robot().oops

results in:

Traceback (most recent call last):
  File "demo.py", line 3, in <module>
    Robot().oops
  File "/usr/local/webots/lib/controller/python36/controller.py", line 1701, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, Node, name)
  File "/usr/local/webots/lib/controller/python36/controller.py", line 88, in _swig_getattr
    return _swig_getattr_nondynamic(self, class_type, name, 0)
  File "/usr/local/webots/lib/controller/python36/controller.py", line 83, in _swig_getattr_nondynamic
    return object.__getattr__(self, name)
AttributeError: type object 'object' has no attribute '__getattr__'

Expected behavior

While AttributeError is correct here, I would expect that it would have the proper name of the attribute which was being attempted to access (i.e: oops in the above).

System

  • Operating System: Ubuntu 18.04

Additional context

I'm not sure what the code in Webots' controller.py is trying to do, but as far as I can determine object.__getattr__ is always going to be an error -- there isn't a class method of that name in any version of Python.

If this is meant to always error, then a simple fix would be to change _swig_getattr_nondynamic like so:

-    if (not static):
-        return object.__getattr__(self, name)
-    else:
-        raise AttributeError(name)
+    raise AttributeError(name)

Alternatively, perhaps the function getattr is intended to be being used?

     if (not static):
-        return object.__getattr__(self, name)
+        return getattr(self, name)
     else:
         raise AttributeError(name)
@omichel
Copy link
Member

omichel commented Oct 26, 2020

That's probably an issue caused by SWIG, yet another good reason to create a Python API which doesn't rely on SWIG, see also #2385 about it.

@lukicdarkoo lukicdarkoo added this to the R2021b milestone Nov 30, 2020
@stefaniapedrazzi stefaniapedrazzi added the bug Something isn't working label Mar 3, 2021
@stefaniapedrazzi stefaniapedrazzi modified the milestones: R2021b, R2022a May 17, 2021
@omichel omichel modified the milestones: R2022a, R2022b Dec 3, 2021
@omichel omichel modified the milestones: R2022b, R2022c Jun 13, 2022
@ygoumaz
Copy link
Contributor

ygoumaz commented Nov 8, 2022

Closing this issue as it was fixed with the new Python API.

@ygoumaz ygoumaz closed this as completed Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

5 participants