Skip to content

Commit e1df6c1

Browse files
committed
agents/sysfsgpio: fix misleading Python error when permission denied
If the executor doesn't have the proper permissions to control the GPIO or export it, a Permission Denied will be raised. However, __del__ is called regardless and gpio_sysfs_value_fd attribute isn't present if the __init__ doesn't finish. Thus, Python prints the following: """ Exception ignored in: <function GpioDigitalOutput.__del__ at 0x7f5b017f80e0> Traceback (most recent call last): File "<loaded sysfsgpio>", line 44, in __del__ AttributeError: 'GpioDigitalOutput' object has no attribute 'gpio_sysfs_value_fd' """ before the raised Exception, possibly misleading the user on their debugging quest. Therefore, let's make sure the attribute exists before anything can throw an Exception in __init__. os.close() can only be called on a valid file descriptor, which None isn't, therefore only call os.close() if self.gpio_sysfs_value_fd is not None. Signed-off-by: Quentin Schulz <[email protected]>
1 parent 4398da7 commit e1df6c1

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

labgrid/util/agents/sysfsgpio.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def _assert_gpio_line_is_exported(index):
2424
raise ValueError("Device not found")
2525

2626
def __init__(self, index):
27+
self.gpio_sysfs_value_fd = None
2728
self._logger = logging.getLogger("Device: ")
2829
GpioDigitalOutput._assert_gpio_line_is_exported(index)
2930
gpio_sysfs_path = os.path.join(GpioDigitalOutput._gpio_sysfs_path_prefix,
@@ -41,7 +42,8 @@ def __init__(self, index):
4142
self.gpio_sysfs_value_fd = os.open(gpio_sysfs_value_path, flags=(os.O_RDWR | os.O_SYNC))
4243

4344
def __del__(self):
44-
os.close(self.gpio_sysfs_value_fd)
45+
if self.gpio_sysfs_value_fd:
46+
os.close(self.gpio_sysfs_value_fd)
4547
self.gpio_sysfs_value_fd = None
4648

4749
def get(self):

0 commit comments

Comments
 (0)