py/objexcept: Support errno attribute on OSError exceptions.#7136
Merged
Conversation
Member
|
I've explained the |
Contributor
|
+1 because the cons are almost a no-brainer. Perhaps add more explicit documentation for the errno/value always being there, like in the 'cpython diffs'? |
Member
Author
Done. |
Member
Author
|
I pushed a commit which changes the tests to use |
This commit adds the errno attribute to exceptions, so code can retrieve errno codes from an OSError using exc.errno. The implementation here simply lets `errno` (and the existing `value`) attributes work on any exception instance (they both alias args[0]). This is for efficiency and to keep code size down. The pros and cons of this are: Pros: - more compatible with CPython, less difference to document and learn - OSError().errno will correctly return None, whereas the current way of doing it via OSError().args[0] will raise an IndexError - it reduces code size on most bare-metal ports (because they already have the errno qstr) - for Python code that uses exc.errno the generated bytecode is 2 bytes smaller and more efficient to execute (compared with exc.args[0]); so bytecode loaded to RAM saves 2 bytes RAM for each use of this attribute, and bytecode that is frozen saves 2 bytes flash/ROM for each use - it's easier/shorter to type, and saves 2 bytes of space in .py files that use it (for each use) Cons: - increases code size by 4-8 bytes on minimal ports that don't already have the `errno` qstr - all exceptions now have .errno and .value attributes (a cpydiff test is added to address this) See also micropython#2407. Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
8421a8a to
3123f69
Compare
tannewt
pushed a commit
to tannewt/circuitpython
that referenced
this pull request
Nov 16, 2022
.. and explain why, because it wasn't clear to past-me. Actually tested on a pico w :) Closes: micropython#7136
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds the errno attribute to exceptions, so code can retrieve errno codes from an OSError using
exc.errno.This is an alternative implementation of #2407, which generated a lot of controversy at the time (4.5 years ago).
The reason to post this PR is to hopefully finish the discussion about adding/not adding this feature, and finally close #2407.
The implementation here is a simpler version of #2407 which simply lets
errno(and the existingvalue) attributes to work on any exception instance (they both aliasargs[0]). This is for efficiency and to keep code size down. As I see it, the pros and cons of addingerrnoare:Pros:
OSError().errnowill correctly returnNone, whereas the current way of doing it viaOSError().args[0]will raise anIndexErrorerrnoqstr)exc.errnothe generated bytecode is 2 bytes smaller and more efficient to execute (compared withexc.args[0]); so bytecode loaded to RAM saves 2 bytes RAM for each use of this attribute, and bytecode that is frozen saves 2 bytes flash/ROM for each useCons:
uerrnomodule (due to the additional qstr).errnoand.valueattributes (this is specific to the implementation here)Feedback/comments welcome!
Code size change of this PR: