Issue1182143
Created on 2005-04-13 11:02 by sdementen, last changed 2009-11-05 18:46 by rhettinger.
|
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) |
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) |
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) |
Date: 2009-05-15 08:55 |
|
See also issue 5353.
|
|
msg87892 - (view) |
Author: Antoine Pitrou (pitrou) |
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)
|
|
| Date |
User |
Action |
Args |
| 2009-11-05 18:46:22 | rhettinger | set | assignee: rhettinger -> |
| 2009-05-28 14:28:19 | ezio.melotti | set | nosy:
+ ezio.melotti
|
| 2009-05-16 13:43:46 | pitrou | set | nosy:
+ pitrou messages:
+ msg87892
|
| 2009-05-15 14:46:36 | rhettinger | set | assignee: rhettinger |
| 2009-05-15 08:55:25 | r.david.murray | set | nosy:
+ r.david.murray messages:
+ msg87799
|
| 2009-05-15 02:51:35 | ajaksu2 | set | nosy:
+ ajaksu2
versions:
+ Python 3.2, - Python 3.1 |
| 2009-02-15 22:16:17 | ajaksu2 | set | stage: test needed versions:
+ Python 3.1, Python 2.7 |
| 2005-04-13 11:02:55 | sdementen | create | |
|