This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 2to3 doesn't detect or fix Exception indexing
Type: enhancement Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder: Close 2to3 issues and list them here
View: 45544
Assigned To: Nosy List: benjamin.peterson, dlenski, iritkatriel, xtreak
Priority: normal Keywords:

Created on 2017-05-02 19:31 by dlenski, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg292799 - (view) Author: Daniel Lenski (dlenski) * Date: 2017-05-02 19:31
Python 2.7 allows indexing an Exception object to access its args list.

>>> e=Exception('foo','bar','baz')
>>> assert e[0] is e.args[0]

This doesn't work in 3.5:

>>> e=Exception('foo','bar','baz')
>>> e.args[0]
'foo'
>>> e[0]
TypeError: 'Exception' object is not subscriptable

This conversion (e[x] -> e.args[x]) seems like it ought to be fairly straightforward to support in 2to3.
msg396172 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-20 14:35
There is no way to know through static analysis that the subscript is on an object of type exception. I think this should be closed as won't fix.
msg396289 - (view) Author: Daniel Lenski (dlenski) * Date: 2021-06-21 21:14
> There is no way to know through static analysis that the subscript is on an object of type exception. I think this should be closed as won't fix.

In almost all cases, the variable in question will receive its value via `except ... as e:`.

   try:
       ...
   except (Exception1, Exception2, ...) as e:
       e[0]

Seems to me that it should be possible for 2to3 to handle this large subset of applicable cases via static analysis.
msg404561 - (view) Author: Daniel Lenski (dlenski) * Date: 2021-10-21 01:25
Why was this closed?

As I wrote in a previous comment, this situation can be automatically identified and addressed via static analysis in the vastly-predominant case, where the Exception object is assigned via an `exception ... as exc:` clause.
msg404566 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2021-10-21 04:48
See the superseding issue; 2to3 is deprecated and headed towards deletion.
History
Date User Action Args
2022-04-11 14:58:45adminsetgithub: 74424
2021-10-21 04:48:00benjamin.petersonsetmessages: + msg404566
2021-10-21 01:25:46dlenskisetmessages: + msg404561
2021-10-20 22:53:12iritkatrielsetstatus: open -> closed
superseder: Close 2to3 issues and list them here
stage: test needed -> resolved
2021-06-21 21:14:18dlenskisetstatus: pending -> open

messages: + msg396289
2021-06-20 14:35:05iritkatrielsetstatus: open -> pending

nosy: + iritkatriel
messages: + msg396172

resolution: wont fix
2018-09-11 10:02:13xtreaksetnosy: + xtreak
2017-05-05 22:11:14terry.reedysetnosy: + benjamin.peterson

stage: test needed
2017-05-02 19:31:56dlenskicreate