Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-132952: Import _io instead of io at startup#132957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 6 commits intopython:mainfromJelleZijlstra:sunder-io
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Set the __module__ of UnsupportedOperation even if io is not imported
  • Loading branch information
@JelleZijlstra
JelleZijlstra committedApr 26, 2025
commitc3cfa671f1ff319e01e6bf07d18ac419ff846aa4
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

1 change: 1 addition & 0 deletionsInclude/internal/pycore_global_strings.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -514,6 +514,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(intern)
STRUCT_FOR_ID(intersection)
STRUCT_FOR_ID(interval)
STRUCT_FOR_ID(io)
STRUCT_FOR_ID(is_running)
STRUCT_FOR_ID(is_struct)
STRUCT_FOR_ID(isatty)
Expand Down
1 change: 1 addition & 0 deletionsInclude/internal/pycore_runtime_init_generated.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

4 changes: 4 additions & 0 deletionsInclude/internal/pycore_unicodeobject_generated.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

3 changes: 0 additions & 3 deletionsLib/io.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -60,9 +60,6 @@
IncrementalNewlineDecoder, text_encoding, TextIOWrapper)


# Pretend this exception was created here.
UnsupportedOperation.__module__ = "io"

# for seek()
SEEK_SET = 0
SEEK_CUR = 1
Expand Down
19 changes: 19 additions & 0 deletionsLib/test/test_io.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -445,6 +445,25 @@ def test_invalid_operations(self):
self.assertRaises(exc, fp.seek, 1, self.SEEK_CUR)
self.assertRaises(exc, fp.seek, -1, self.SEEK_END)

@support.cpython_only
def test_startup_optimization(self):
# gh-132952: Test that `io` is not imported at startup and that the
# __module__ of UnsupportedOperation is set to "io".
assert_python_ok("-S", "-c", textwrap.dedent(
"""
import sys
assert "io" not in sys.modules
try:
sys.stdin.truncate()
except Exception as e:
typ = type(e)
assert typ.__module__ == "io", (typ, typ.__module__)
assert typ.__name__ == "UnsupportedOperation", (typ, typ.__name__)
else:
raise AssertionError("Expected UnsupportedOperation")
"""
))

@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
def test_optional_abilities(self):
# Test for OSError when optional APIs are not supported
Expand Down
5 changes: 5 additions & 0 deletionsModules/_io/_iomodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -661,6 +661,11 @@ iomodule_exec(PyObject *m)
"UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
if (state->unsupported_operation == NULL)
return -1;
if (PyObject_SetAttrString(state->unsupported_operation,
"__module__", &_Py_ID(io)) < 0)
{
return -1;
}
if (PyModule_AddObjectRef(m, "UnsupportedOperation",
state->unsupported_operation) < 0)
{
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp