For best performance (reduce time to build an exception object), I would be interested to only format the error message on demand. For example, when str(exception) is called.
The problem is the Exception.args attribute. Example:
$ ./python Python 3.10.0a6+
exc=AttributeError("%s object has no attribute %s" % ("MyObject", "name")) str(exc)
'MyObject object has no attribute name'
('MyObject object has no attribute name',)
Currently, args is the raw positional arguments passed to the Exception constructor, and they are very likely many applications relying on Exception.args.
For backward compatibility, we could store "%s object has no attribute %s" and ("MyObject", "name"), and build the args tuple on demand and format the string at the first str() call.
At the C level, args is exposed directly as PyBaseExceptionObject.args and the PyBaseExceptionObject structure is part of the public C API. Changing it would be a C API incompatible change.
By the way, the PEP 473 doesn't say anything about it and has no "Backward compatibility" section :-(
On Sat, Mar 13, 2021 at 7:49 PM Sebastian Kreft firstname.lastname@example.org wrote:
Hi dev-team, I'm reopening the discussion of PEP 473, which was closed due to:
The steering council felt the PEP was too broad and not focused enough. Discussions about adding more attributes to built-in exceptions can continue on the issue tracker on a per-exception basis (and obviously here for any broader points, e.g. performance implications as I know that has come up before when the idea of storing relevant objects on exceptions).
Before the PEP was rejected by the SC, I had submitted https://github.com/python/cpython/pull/6271 and the last response said:
You may propose a newly revised PEP which overcomes the last SC feedback. But without the accepted PEP, this PR can not be merged.
Please clarify how can I proceed with adding some of the specified fields to the mentioned exceptions. Should I create a new PEP as suggested in the PR or should we create individual BPO issues for each exception modification.
-- Sebastian Kreft _______________________________________________ Python-Dev mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://email@example.com/message/4XVFFNBH... Code of Conduct: http://python.org/psf/codeofconduct/