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.

Unsupported provider

classification
Title: making builtin exceptions more informative
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: George Jenkins, ajaksu2, berker.peksag, doerwalter, ezio.melotti, iritkatriel, pitrou, r.david.murray, rhettinger, sdementen
Priority: normal Keywords: patch

Created on 2005-04-13 11:02 by sdementen, last changed 2022-04-11 14:56 by admin.

Files
File name Uploaded Description Edit
Issue1182143_1.patch George Jenkins, 2015-03-19 03:24 review
Issue1182143_1hg.patch George Jenkins, 2015-04-09 02:05 Patch generated via mercurial review
Messages (13)
msg61192 - (view) Author: Sebastien de Menten (sdementen) Date: 2005-04-13 11:02
Using builtin exception information is tricky as it 
consists of:
 a) the type of exception (easily accessible)
 b) the args attribute = a 1 element tuple with a string

1st example:

try:
    print foo
except NameError, e:
    print e.args
    symbol = e.args[0][17:-16]
    print symbols
==> 
("NameError: name 'foo' is not defined", )
foo

It would be nicer to have:
e.args = ("NameError: name 'foo' is not defined", "foo")
The first element being the current string for backward 
compatibilty.
=============================

2nd example:

try:
    (4).foo
except NameError, e:
    print e.args
==> ("'int' object has no attribute 'foo'",)

It would be nicer to have:
e.args = ("'int' object has no attribute 'foo'", 4, "foo")
Again, the first element being the current string for 
backward compatibilty.

=============================

Moreover, in the documentation about Exception, I read
"""Warning: Messages to exceptions are not part of the 
Python API. Their 
contents may change from one version of Python to the 
next without warning 
and should not be relied on by code which will run under 
multiple versions 
of the interpreter. """

So even args could not be relied upon !
But it also means that there is no need to be backward 
compatible (I am playing devil's advocate, backward 
compatibility is important !)

Seb

ps: There may be problems (that I am not aware) with
 a) an exception keeping references to other objects
 b) C API that can throw only exceptions with strings
 c) a specific advantage of having strings only in builtin 
exceptions
msg61193 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-06-03 01:33
Logged In: YES 
user_id=80475

This looks like a good idea to me.
msg61194 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2005-06-03 06:03
Logged In: YES 
user_id=89016

+1

This should probably be part of Brett's Py3000 exception PEP.

Candidates I can think of are:

KeyError(obj=..., key=...)
IndexError(obj=..., index=...)
IOError(file=..., code=...)
NameError(name=...)

Regenerating the message would be done with __str__().

I'm not sure how this would work on the level of the C API.
msg87799 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-05-15 08:55
See also issue 5353.
msg87892 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-05-16 13:43
Beware that making exceptions hold on arbitrary objects increases the
possibility of delayed collection and reference cycles, though.
(especially in trunk where the "current" exception can last after the
end of an except block)
msg238489 - (view) Author: George Jenkins (George Jenkins) * Date: 2015-03-19 03:24
Hi, I attempted a solution for this a while back

Attached is a (git diff based) patch for review

Changes can also been seen at: https://bitbucket.org/bloomberg/cpython/pull-request/2/http-bugspythonorg-issue1182143-add-name/diff
(but note the changes in Misc/NEWS and Tools/msi/msi.py are not related / somehow got pulled into my changes by mercurial)

Please review/let me know of any other process required. Thanks!
msg240304 - (view) Author: George Jenkins (George Jenkins) * Date: 2015-04-09 02:05
Add patch generated via mercurial
msg242447 - (view) Author: George Jenkins (George Jenkins) * Date: 2015-05-03 02:30
Heh, just noticed this issue passed its 10 year anniversary!

If someone has time to review my patch, that would be much appreciated. Thanks!
msg245599 - (view) Author: George Jenkins (George Jenkins) * Date: 2015-06-21 16:40
Reviewer please :) 

(or, advice on how I can get this to proceed, thx)
msg245603 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-21 17:00
What does your patch implement?  It's not clear from the issue discussion that an API was decided on.
msg245883 - (view) Author: George Jenkins (George Jenkins) * Date: 2015-06-27 15:55
Thanks for the response.

In terms of python API changes - I exposed the 'name' attribute on UnboundLocalError and NameError exceptions. 

(This can be seen (& is verified) by the changes to test_exceptions.py exceptions test cases in the patch)

In terms of the wider changes in the patch - I factored UnboundLocalError and NameError to have specific native implementations, rather than been based off SimpleExtendsException. And thus able to accept the name of the variable causing the exception.

And I agree with your comment on the exact API never been agreed on. Since there are now native implementations of UnboundLocalError and NameError, I can happily change their implementation to expose whatever can be decided to be better (comments?!)

Thanks, George
msg253415 - (view) Author: George Jenkins (George Jenkins) * Date: 2015-10-24 19:47
Bump on this one please
msg257268 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2016-01-01 03:46
I would suggest you to bring this up to python-dev (or perhaps python-ideas?).
Also note that Brett proposed something similar in #18162 and other related issues (#18156, #18163, #18165, #18166).
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41852
2021-12-10 19:57:33ajaksu2setnosy: + iritkatriel
2016-01-01 03:46:50ezio.melottisetmessages: + msg257268
versions: + Python 3.6, - Python 3.5
2015-10-24 19:47:46George Jenkinssetmessages: + msg253415
2015-06-27 15:55:41George Jenkinssetmessages: + msg245883
2015-06-21 17:00:23r.david.murraysetmessages: + msg245603
2015-06-21 16:40:25George Jenkinssetmessages: + msg245599
2015-05-03 15:38:48berker.peksagsetnosy: + berker.peksag

stage: test needed -> patch review
2015-05-03 02:30:50George Jenkinssetmessages: + msg242447
2015-04-09 02:05:25George Jenkinssetfiles: + Issue1182143_1hg.patch

messages: + msg240304
2015-03-19 03:24:11George Jenkinssetfiles: + Issue1182143_1.patch
versions: + Python 3.5, - Python 3.2
nosy: + George Jenkins

messages: + msg238489

keywords: + patch
2010-08-21 13:46:07BreamoreBoysetnosy: doerwalter, rhettinger, sdementen, pitrou, ajaksu2, ezio.melotti, r.david.murray
components: + Interpreter Core, - Library (Lib)
versions: - Python 2.7
2009-11-05 18:46:22rhettingersetassignee: rhettinger ->
2009-05-28 14:28:19ezio.melottisetnosy: + ezio.melotti
2009-05-16 13:43:46pitrousetnosy: + pitrou
messages: + msg87892
2009-05-15 14:46:36rhettingersetassignee: rhettinger
2009-05-15 08:55:25r.david.murraysetnosy: + r.david.murray
messages: + msg87799
2009-05-15 02:51:35ajaksu2setnosy: + ajaksu2

versions: + Python 3.2, - Python 3.1
2009-02-15 22:16:17ajaksu2setstage: test needed
versions: + Python 3.1, Python 2.7
2005-04-13 11:02:55sdementencreate