gh-109714: Improve the OSError constructor - #156430
Open
serhiy-storchaka wants to merge 1 commit into
Open
Conversation
* The errno argument can now be omitted. It defaults to the error code which corresponds to the exception class. * The strerror argument can now be omitted. It is derived from the resulting errno, or on Windows from winerror. * filename, winerror and filename2 can now be passed by keyword, as can characters_written for BlockingIOError. * args is no longer truncated when a file name is given, so that an exception carrying one survives pickling. * Passing more than five positional arguments is now a TypeError. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
serhiy-storchaka
requested review from
AA-Turner and
iritkatriel
as code owners
August 26, 2026 20:23
Documentation build overview
|
This was referenced Aug 27, 2026
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.
The errno and strerror arguments of the
OSErrorconstructor can now be omitted.errno defaults to the error code which corresponds to the exception class, exposed as the new
default_errnoclass attribute, so thatFileNotFoundError()works both with code that tests theerrnoattribute and with code that usesisinstance().strerror is derived from the resulting
errno, or on Windows from winerror if that was given, whose message is more specific.filename, winerror and filename2 can now be passed by keyword, as can characters_written for
BlockingIOError.argsis now set to the canonical positional form, so thatOSError(*args)reproduces the exception, which is how it is pickled.It used to be truncated to two items whenever a file name was consumed, so an exception carrying one did not survive pickling.
Incompatible changes:
TypeError, instead of being silently ignored.argskeeps the file names, soOSError(2, 'm', 'f').argsis the whole 3-tuple rather than(2, 'm').Noneis not stored on the exception, so the args of a pickledOSError(2, 'm', None)lose the trailingNone.