DevFailed does not support indexing

Hello.
It seems that python3 version of PyTango 9.2.2 does not support indexing of DevFailed.
I tried the following code on python 2 and python3.

import tango
ex = None
try:
        dev = tango.DeviceProxy('a/b/c') # This device does not exist
except tango.DevFailed as e:
        ex = e
ex.args[0]# Works well
ex[0] # Fails on python3 with TypeError: 'DevFailed' object does not support indexing

Info:
Ubuntu 18.04
PyTango 9.2.2
python 3.6
libboost-python1.65
I am not a PyTango expert but it seems like the DevFailed type has changed and you now need to specify ex.args[0] e.g.

In [37]: print(ex.args[0])
DevError[
    desc = device a/b/c not defined in the database !
  origin = DataBase::ImportDevice()
  reason = DB_DeviceNotDefined
severity = ERR]

Maybe a Python expert can explain the new type.

Andy
I mean that the same code works well on python 2 but fails on python 3.

import tango
ex = None
try:
        dev = tango.DeviceProxy('a/b/c') # This device does not exist
except tango.DevFailed as e:
        ex = e
ex.args[0]# Works well on both python2 python3
ex[0] # Works on python2 but fails on python3 with TypeError: 'DevFailed' object does not support indexing

Packages:
python3-pytango: 9.2.2-1build1
python-pytango: 9.2.2-1build1
Yes, BaseException.__getitem__ has been removed in python 3.

Do you think it's a problem?
I missed that Exception API was changed.
It is not a huge problem. In my opinion, there is no need to provide full compatibility with old code.
Yes, Some problems could arise during porting python2 code to python3.
I think that the optimal solution is to create "Porting guideline" page in Docs.
Edited 5 years ago
Hi,
related to this issue, we did find a bug in pyutil.py in method __Util__create_device:
try:
db.import_device(device_name)
except DevFailed as df:
device_exists = not df[0].reason == "DB_DeviceNotDefined"

it should be:
try:
db.import_device(device_name)
except DevFailed as df:
device_exists = not df.args[0].reason == "DB_DeviceNotDefined"

Could we get a fix in the next release, please?

Thanks for you help.

Laurent
Laurent
 
Register or login to create to post a reply.