agents/sysfsgpio: fix misleading Python error when permission denied #1704
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
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 "", 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.
This has been tested by running labgrid with a non-root user with the GPIO not exported and the non-root user not having permission to write to
/sys/class/gpio/export
.Checklist