Navigation Menu

Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Preserve cause of exception when getting/setting attribute in DeviceProxy #365

Merged
merged 5 commits into from Aug 26, 2020

Conversation

woutdenolf
Copy link
Contributor

Closes #364

@woutdenolf
Copy link
Contributor Author

I guess we still need to support python 2?

from six import raise_from

@woutdenolf woutdenolf marked this pull request as ready for review June 14, 2020 17:36
@ajoubertza
Copy link
Member

I guess we still need to support python 2?

Yes, please!

Copy link
Member

@ajoubertza ajoubertza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach looks good, just need the Python 2 support.

Comment on lines +298 to +299
if cause is None:
cause = e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would be multiple exceptions in this method if the device is off/unreachable, so keeping the first exception is a good approach 👍

Comment on lines 368 to 370
return super(DeviceProxy, self).__setattr__(name, value)
except Exception as e:
raise e from cause
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is interesting. If we get here then name isn't a known Tango device command/attribute/pipe. We end up writing to either an existing Python class instance attribute, or adding a new one, if it doesn't exist yet.

I'm not sure that __setattr__ can even raise an exception? If it did, the cause may or may not be relevant.

A couple of cases could occur for the example of an unreachable device:

  1. We haven't yet cached the lists of commands, attributes and pipes.
  2. We have cached the lists of commands, attributes and pipes prior to this call.

Then we get these four scenarios, depending on if the Tango command/attribute/pipe either:

  • Exists on the device:
    • For case 1: we'll write to a local class instance attribute and the user won't even get an error. This behaviour is unexpected, but not a new problem.
    • For case 2: we would have failed because it is a command, or we tried to call __set_attribute_value or write_pipe earlier in this method, so the user will get whatever error those methods throw. That's fine.
  • Doesn't exist on the device:
    • For case 1: we'll write to a local class instance attribute and the user won't get an error. This could be the desired behaviour, if the user was storing some other info on an instance of a DeviceProxy. Or, it could be undesired, if the user thought they were setting a Tango attribute/pipe but had the wrong name. Not much we can do about that.
    • For case 2: same as case 1. There won't be an error, and we're not sure if the user expected one of not.

In conclusion, the change in the PR is fine. It has just made us aware of some nuances in how setting attributes on a DeviceProxy behaves.

tango/device_proxy.py Outdated Show resolved Hide resolved
woutdenolf and others added 3 commits August 25, 2020 16:48
For some reason the CI tests for Python 2.7 are attempting to install a version of pytest-xdist that no longer supports Python 2.7.  Explicitly limit the version to avoid this.
Copy link
Member

@ajoubertza ajoubertza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. I have one query.

(And I updated the test requirements due to the last CI failure)

pass
except Exception as e:
if cause is None:
cause = e

if name_l in self.__get_cmd_cache():
raise TypeError('Cannot set the value of a command')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this line include cause, similar to line 332?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Thanks!

Copy link
Member

@ajoubertza ajoubertza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks for the contribution!

@ajoubertza ajoubertza changed the title [deviceproxy] preserve cause of exception when getting/setting attribute Preserve cause of exception when getting/setting attribute in DeviceProxy Aug 26, 2020
@ajoubertza ajoubertza merged commit 2ad9629 into tango-controls:develop Aug 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DeviceProxy AttributeError: cause is lost
2 participants