Skip to content

Commit 698e9ea

Browse files
gh-109714: Omit redundant arguments when raising OSError subclasses
errno now defaults to the error code which corresponds to the exception class, and strerror is derived from it, so passing them explicitly adds nothing in _pyio, _pyrepl, multiprocessing and shutil. BlockingIOError in _pyio now passes the number of characters written by keyword, and FileExistsError in multiprocessing passes only the Windows error code, from which both errno and strerror are derived. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent fe3a26f commit 698e9ea

4 files changed

Lines changed: 7 additions & 12 deletions

File tree

Lib/_pyio.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,8 +1325,8 @@ def _flush_unlocked(self):
13251325
"should not raise BlockingIOError")
13261326
if n is None:
13271327
raise BlockingIOError(
1328-
errno.EAGAIN,
1329-
"write could not complete without blocking", 0)
1328+
"write could not complete without blocking",
1329+
characters_written=0)
13301330
if n > len(self._write_buf) or n < 0:
13311331
raise OSError("write() returned incorrect number of bytes")
13321332
del self._write_buf[:n]
@@ -1629,8 +1629,7 @@ def __init__(self, file, mode='r', closefd=True, opener=None):
16291629
self._stat_atopen = os.fstat(fd)
16301630
try:
16311631
if stat.S_ISDIR(self._stat_atopen.st_mode):
1632-
raise IsADirectoryError(errno.EISDIR,
1633-
os.strerror(errno.EISDIR), file)
1632+
raise IsADirectoryError(filename=file)
16341633
except AttributeError:
16351634
# Ignore the AttributeError if stat.S_ISDIR or errno.EISDIR
16361635
# don't exist.

Lib/_pyrepl/terminfo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Pure Python curses-like terminal capability queries."""
22

33
from dataclasses import dataclass, field
4-
import errno
54
import os
65
from pathlib import Path
76
import re
@@ -151,7 +150,7 @@ def _read_terminfo_file(terminal_name: str) -> bytes:
151150
if path.is_file():
152151
return path.read_bytes()
153152

154-
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
153+
raise FileNotFoundError(filename=filename)
155154

156155

157156
# Hard-coded terminal capabilities for common terminals

Lib/multiprocessing/shared_memory.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from functools import partial
1212
import mmap
1313
import os
14-
import errno
1514
import struct
1615
import secrets
1716
import types
@@ -143,10 +142,8 @@ def __init__(self, name=None, create=False, size=0, *, track=True):
143142
if last_error_code == _winapi.ERROR_ALREADY_EXISTS:
144143
if name is not None:
145144
raise FileExistsError(
146-
errno.EEXIST,
147-
os.strerror(errno.EEXIST),
148-
name,
149-
_winapi.ERROR_ALREADY_EXISTS
145+
filename=name,
146+
winerror=_winapi.ERROR_ALREADY_EXISTS
150147
)
151148
else:
152149
continue

Lib/shutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
12451245
root_dir = os.fspath(root_dir)
12461246
stmd = os.stat(root_dir).st_mode
12471247
if not stat.S_ISDIR(stmd):
1248-
raise NotADirectoryError(errno.ENOTDIR, 'Not a directory', root_dir)
1248+
raise NotADirectoryError(filename=root_dir)
12491249

12501250
if supports_root_dir:
12511251
kwargs['root_dir'] = root_dir

0 commit comments

Comments
 (0)