Skip to content

Commit 2e10fb9

Browse files
committed
Handle libraries with attrs having invalid __getattr__.
This fixes a regression in RF 7.0a1 reported with SSHLibrary. It has some kind of a config object with `__getattr__` that can raise other exceptions than AttributeError. These exceptions go through when we use `hasattr(candidate, 'robot_name')` and need to be handled in our code. I consider SSHLibrary being buggy, but because it worked earlier this can be consired a regression.
1 parent 3c65591 commit 2e10fb9

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

atest/robot/test_libraries/auto_keywords_off.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Private Method Is Not Recognized As Keyword
1414

1515
Private Decorated Method Is Recognized As Keyword
1616
Check Test Case ${TESTNAME}
17+
18+
Invalid __getattr__ is handled
19+
Check Test Case ${TESTNAME}

atest/testdata/test_libraries/ClassWithAutoKeywordsOff.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
from robot.api.deco import keyword
22

33

4+
class InvalidGetattr:
5+
6+
def __getattr__(self, item):
7+
if item == 'robot_name':
8+
raise ValueError('This goes through getattr() and hasattr().')
9+
raise AttributeError
10+
11+
412
class ClassWithAutoKeywordsOff:
513
ROBOT_AUTO_KEYWORDS = False
614

@@ -17,3 +25,5 @@ def _private_method_is_not_keyword(self):
1725
@keyword
1826
def _private_decorated_method_is_keyword(self):
1927
print('Decorated private methods are keywords.')
28+
29+
invalid_getattr = InvalidGetattr()

atest/testdata/test_libraries/auto_keywords_off.robot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ Private Method Is Not Recognized As Keyword
1818
Private Decorated Method Is Recognized As Keyword
1919
Private Decorated Method Is Keyword
2020
Private Decorated Method In Module Is Keyword
21+
22+
Invalid __getattr__ is handled
23+
[Documentation] FAIL No keyword with name 'Invalid Getattr' found.
24+
Invalid Getattr

src/robot/running/testlibraries.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ def has_robot_name(name):
257257
candidate = inspect.getattr_static(libcode, name)
258258
if isinstance(candidate, (classmethod, staticmethod)):
259259
candidate = candidate.__func__
260-
return hasattr(candidate, 'robot_name')
260+
try:
261+
return hasattr(candidate, 'robot_name')
262+
except Exception:
263+
return False
261264

262265
auto_keywords = getattr(libcode, 'ROBOT_AUTO_KEYWORDS', True)
263266
if auto_keywords:

0 commit comments

Comments
 (0)