From 6bd4449ecf2eccfa1e6bfd17b60a5c081ce9aa40 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Thu, 12 Nov 2026 12:49:32 -0800 Subject: [PATCH 1/3] gh-71199: Always build posixmodule as _os The module was compiled as posix or nt depending on the platform. Build it as `_os` everywhere with a single `PyInit__os`. Keep the historical names available for compatibility backed by the same init function so sys.builtin_module_names, find_spec() and loading it by hand (as LazyLoader does). Freeze skips posix and nt when writing its config.c, since the inittab templates already map them to PyInit__os. Co-Authored-By: Claude Opus 5.5 --- Doc/library/posix.rst | 4 ++ .../pycore_global_objects_fini_generated.h | 2 + Include/internal/pycore_global_strings.h | 2 + .../internal/pycore_runtime_init_generated.h | 2 + .../internal/pycore_unicodeobject_generated.h | 2 + Lib/importlib/_bootstrap_external.py | 4 +- .../test_importlib/builtin/test_loader.py | 25 +++++++++++ Lib/test/test_os/test_os.py | 25 +++++++++++ ...6-11-02-02-03-02.gh-issue-71199.38iE9A.rst | 3 ++ Modules/Setup.bootstrap.in | 2 +- Modules/config.c.in | 4 ++ Modules/posixmodule.c | 45 +++++++++++++++---- PC/config.c | 5 ++- PC/config_minimal.c | 5 ++- Python/stdlib_module_names.h | 1 + Tools/build/check_extension_modules.py | 1 + Tools/freeze/makeconfig.py | 2 +- 17 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-02-02-03-02.gh-issue-71199.38iE9A.rst diff --git a/Doc/library/posix.rst b/Doc/library/posix.rst index 7d43b5d4cf7657e..440cba8296d9dc6 100644 --- a/Doc/library/posix.rst +++ b/Doc/library/posix.rst @@ -23,6 +23,10 @@ available through the :mod:`os` interface. Once :mod:`os` is imported, there is :mod:`os` provides some additional functionality, such as automatically calling :func:`~os.putenv` when an entry in ``os.environ`` is changed. +.. versionchanged:: next + Functions in this module report :mod:`os` as their ``__module__`` and + are :ref:`pickled ` as ``os.``. + Errors are reported as exceptions; the usual exceptions are given for type errors, while errors reported by the system calls raise :exc:`OSError`. diff --git a/Include/internal/pycore_global_objects_fini_generated.h b/Include/internal/pycore_global_objects_fini_generated.h index 483a1dc58791cd5..6f092a8a683f514 100644 --- a/Include/internal/pycore_global_objects_fini_generated.h +++ b/Include/internal/pycore_global_objects_fini_generated.h @@ -275,6 +275,7 @@ _PyStaticObjects_CheckAll(PyInterpreterState *interp) { _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(_loop), "_loop", 5); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(_needs_com_addref_), "_needs_com_addref_", 18); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(_only_immortal), "_only_immortal", 14); + _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(_os), "_os", 3); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(_restype_), "_restype_", 9); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(_showwarnmsg), "_showwarnmsg", 12); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(_shutdown), "_shutdown", 9); @@ -705,6 +706,7 @@ _PyStaticObjects_CheckAll(PyInterpreterState *interp) { _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(options), "options", 7); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(order), "order", 5); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(origin), "origin", 6); + _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(os), "os", 2); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(other), "other", 5); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(out_fd), "out_fd", 6); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(outgoing), "outgoing", 8); diff --git a/Include/internal/pycore_global_strings.h b/Include/internal/pycore_global_strings.h index 949e19196dc5279..0f19b07bc322d70 100644 --- a/Include/internal/pycore_global_strings.h +++ b/Include/internal/pycore_global_strings.h @@ -287,6 +287,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(_loop) STRUCT_FOR_ID(_needs_com_addref_) STRUCT_FOR_ID(_only_immortal) + STRUCT_FOR_ID(_os) STRUCT_FOR_ID(_restype_) STRUCT_FOR_ID(_showwarnmsg) STRUCT_FOR_ID(_shutdown) @@ -717,6 +718,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(options) STRUCT_FOR_ID(order) STRUCT_FOR_ID(origin) + STRUCT_FOR_ID(os) STRUCT_FOR_ID(other) STRUCT_FOR_ID(out_fd) STRUCT_FOR_ID(outgoing) diff --git a/Include/internal/pycore_runtime_init_generated.h b/Include/internal/pycore_runtime_init_generated.h index 4ee954942521ae1..8c4b5745f621562 100644 --- a/Include/internal/pycore_runtime_init_generated.h +++ b/Include/internal/pycore_runtime_init_generated.h @@ -1562,6 +1562,7 @@ extern "C" { INIT_ID(_loop), \ INIT_ID(_needs_com_addref_), \ INIT_ID(_only_immortal), \ + INIT_ID(_os), \ INIT_ID(_restype_), \ INIT_ID(_showwarnmsg), \ INIT_ID(_shutdown), \ @@ -1992,6 +1993,7 @@ extern "C" { INIT_ID(options), \ INIT_ID(order), \ INIT_ID(origin), \ + INIT_ID(os), \ INIT_ID(other), \ INIT_ID(out_fd), \ INIT_ID(outgoing), \ diff --git a/Include/internal/pycore_unicodeobject_generated.h b/Include/internal/pycore_unicodeobject_generated.h index 3aad3345abc4d07..729ffdd63dc94d1 100644 --- a/Include/internal/pycore_unicodeobject_generated.h +++ b/Include/internal/pycore_unicodeobject_generated.h @@ -239,6 +239,7 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) { offsetof(struct _Py_global_strings, identifiers._py__loop._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py__needs_com_addref_._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py__only_immortal._ascii.ob_base), + offsetof(struct _Py_global_strings, identifiers._py__os._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py__restype_._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py__showwarnmsg._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py__shutdown._ascii.ob_base), @@ -669,6 +670,7 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) { offsetof(struct _Py_global_strings, identifiers._py_options._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_order._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_origin._ascii.ob_base), + offsetof(struct _Py_global_strings, identifiers._py_os._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_other._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_out_fd._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_outgoing._ascii.ob_base), diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 2c8d482e4f64366..16ae23cd868c083 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -25,6 +25,7 @@ # Import builtin modules import _imp import _io +import _os import sys import _warnings import marshal @@ -32,13 +33,10 @@ _MS_WINDOWS = (sys.platform == 'win32') if _MS_WINDOWS: - import nt as _os try: import winreg except ImportError: winreg = None -else: - import posix as _os if _MS_WINDOWS: diff --git a/Lib/test/test_importlib/builtin/test_loader.py b/Lib/test/test_importlib/builtin/test_loader.py index 2a5c2a8f39ee131..a912d7627fb5441 100644 --- a/Lib/test/test_importlib/builtin/test_loader.py +++ b/Lib/test/test_importlib/builtin/test_loader.py @@ -2,6 +2,7 @@ machinery = util.import_importlib('importlib.machinery') +import os import sys import types import unittest @@ -42,5 +43,29 @@ def test_not_builtin(self): ) = util.test_both(InspectLoaderTests, machinery=machinery) +class OsModuleTests: + + """Test loading _os and its historical name by hand.""" + + def test_load_by_hand(self): + # As LazyLoader does: a working module, sys.modules left alone. + loader = self.machinery.BuiltinImporter + for name in ('_os', os.name): + with self.subTest(name): + spec = loader.find_spec(name) + mod = loader.create_module(spec) + loader.exec_module(mod) + self.assertEqual(mod.__name__, 'os') + self.assertEqual(mod.getpid(), os.getpid()) + self.assertIsNot(mod, sys.modules[name]) + self.assertIs(sys.modules[os.name], sys.modules['_os']) + self.assertIs(sys.modules[os.name].stat, os.stat) + + +(Frozen_OsModuleTests, + Source_OsModuleTests + ) = util.test_both(OsModuleTests, machinery=machinery) + + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 8b6b7deb65b88bd..212cb8f145fe2d5 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -194,6 +194,7 @@ def fqdn(obj): self.assertEqual(fqdn(os.stat_result), ("os", "stat_result")) self.assertEqual(fqdn(os.times_result), ("os", "times_result")) self.assertEqual(fqdn(os.DirEntry), ("os", "DirEntry")) + self.assertEqual(fqdn(os.stat), ("os", "stat")) if hasattr(os, "statvfs_result"): self.assertEqual(fqdn(os.statvfs_result), ("os", "statvfs_result")) self.assertEqual(fqdn(os.uname_result), ("os", "uname_result")) @@ -202,6 +203,30 @@ def fqdn(obj): if hasattr(os, "waitid_result"): self.assertEqual(fqdn(os.waitid_result), ("os", "waitid_result")) + def test_platform_module(self): + # posix and nt stay builtins, importable only on their own platform, + # and are the _os module under its historical name. + import _os + other = 'posix' if os.name == 'nt' else 'nt' + mod = __import__(os.name) + self.assertIs(mod, _os) + self.assertIs(mod.stat, os.stat) + self.assertEqual(mod.__name__, 'os') + self.assertIn('_os', sys.builtin_module_names) + self.assertIn(os.name, sys.builtin_module_names) + self.assertNotIn(other, sys.builtin_module_names) + with self.assertRaises(ImportError): + __import__(other) + + def test_function_pickle(self): + # Functions pickle as os. on every platform. + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.subTest(f'protocol {proto}'): + p = pickle.dumps(os.stat, proto) + if proto < 4: + self.assertIn(b'cos\nstat\n', p) + self.assertIs(pickle.loads(p), os.stat) + def test_pickle_global_names(self): # Existing pickles name these via os or the platform module (posix # or nt); both must keep loading, as must new pickles. diff --git a/Misc/NEWS.d/next/Library/2026-11-02-02-03-02.gh-issue-71199.38iE9A.rst b/Misc/NEWS.d/next/Library/2026-11-02-02-03-02.gh-issue-71199.38iE9A.rst new file mode 100644 index 000000000000000..8d1cb54aabc51f2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-02-02-03-02.gh-issue-71199.38iE9A.rst @@ -0,0 +1,3 @@ +Change :mod:`posix` (:mod:`!nt` on Windows) to use the module name :mod:`os`. +Platform-specific import names keep working and functions now pickle +as ``os.``. diff --git a/Modules/Setup.bootstrap.in b/Modules/Setup.bootstrap.in index 1d1de43fd8f05b4..f7d2613e84fc305 100644 --- a/Modules/Setup.bootstrap.in +++ b/Modules/Setup.bootstrap.in @@ -12,7 +12,7 @@ # module C APIs are used in core atexit atexitmodule.c faulthandler faulthandler.c -posix posixmodule.c +_os posixmodule.c _signal signalmodule.c _tracemalloc _tracemalloc.c _suggestions _suggestions.c diff --git a/Modules/config.c.in b/Modules/config.c.in index 704f58506048a3e..610a75b07f4b314 100644 --- a/Modules/config.c.in +++ b/Modules/config.c.in @@ -55,6 +55,10 @@ struct _inittab _PyImport_Inittab[] = { /* This lives in Objects/unicodeobject.c */ {"_string", PyInit__string}, + /* The historical name of _os (Modules/posixmodule.c); PC/config.c lists + nt the same way */ + {"posix", PyInit__os}, + /* Sentinel */ {0, 0} }; diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index c6f89fc2454e88a..5674102b5006b99 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -644,13 +644,11 @@ static const unsigned int _Py_STATX_KNOWN = (STATX_BASIC_STATS | STATX_BTIME // --- os module ------------------------------------------------------------ +// The module is built as _os and is also registered in sys.modules under +// its historical platform name. #ifdef MS_WINDOWS -# define INITFUNC PyInit_nt -# define MODNAME "nt" # define MODNAME_OBJ &_Py_ID(nt) #else -# define INITFUNC PyInit_posix -# define MODNAME "posix" # define MODNAME_OBJ &_Py_ID(posix) #endif @@ -2576,7 +2574,7 @@ statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) // ht_module doesn't get set in PyStructSequence_NewType(), // so we can't use PyType_GetModule(). - PyObject *mod = PyImport_GetModule(MODNAME_OBJ); + PyObject *mod = PyImport_GetModule(&_Py_ID(_os)); if (mod == NULL) { return NULL; } @@ -19107,6 +19105,24 @@ posixmodule_exec(PyObject *m) { _posixstate *state = get_posix_state(m); + // Only the _os instance stands in for posix (nt); one loaded under that + // name, say by hand from its spec, leaves sys.modules alone. + PyObject *spec_name = PyModule_GetNameObject(m); + if (spec_name == NULL) { + return -1; + } + int is_os = PyUnicode_EqualToUTF8(spec_name, "_os"); + Py_DECREF(spec_name); + + // Name the module os before adding functions, so they report os as + // their module and pickle as os. on every platform. + if (PyObject_SetAttr(m, &_Py_ID(__name__), &_Py_ID(os)) < 0) { + return -1; + } + if (PyModule_AddFunctions(m, posix_methods) < 0) { + return -1; + } + #if defined(HAVE_PWRITEV) if (HAVE_PWRITEV_RUNTIME) {} else { PyObject* dct = PyModule_GetDict(m); @@ -19286,7 +19302,19 @@ posixmodule_exec(PyObject *m) assert(state->ticks_per_second >= 1); #endif - return PyModule_Add(m, "_have_functions", list); + if (PyModule_Add(m, "_have_functions", list) < 0) { + return -1; + } + if (!is_os) { + return 0; + } + + // Let import posix (nt) find this module unless that name is taken. + if (PyDict_SetDefaultRef(PyImport_GetModuleDict(), MODNAME_OBJ, m, + NULL) < 0) { + return -1; + } + return 0; } @@ -19300,10 +19328,9 @@ static PyModuleDef_Slot posixmodile_slots[] = { static struct PyModuleDef posixmodule = { PyModuleDef_HEAD_INIT, - .m_name = MODNAME, + .m_name = "os", .m_doc = posix__doc__, .m_size = sizeof(_posixstate), - .m_methods = posix_methods, .m_slots = posixmodile_slots, .m_traverse = _posix_traverse, .m_clear = _posix_clear, @@ -19311,7 +19338,7 @@ static struct PyModuleDef posixmodule = { }; PyMODINIT_FUNC -INITFUNC(void) +PyInit__os(void) { return PyModuleDef_Init(&posixmodule); } diff --git a/PC/config.c b/PC/config.c index dcccc6a3a6d0167..7ccd844fd374cde 100644 --- a/PC/config.c +++ b/PC/config.c @@ -15,7 +15,7 @@ extern PyObject* PyInit__tracemalloc(void); extern PyObject* PyInit_gc(void); extern PyObject* PyInit__math_integer(void); extern PyObject* PyInit_math(void); -extern PyObject* PyInit_nt(void); +extern PyObject* PyInit__os(void); extern PyObject* PyInit__operator(void); extern PyObject* PyInit__signal(void); extern PyObject* PyInit__statistics(void); @@ -103,7 +103,8 @@ struct _inittab _PyImport_Inittab[] = { {"gc", PyInit_gc}, {"_math_integer", PyInit__math_integer}, {"math", PyInit_math}, - {"nt", PyInit_nt}, /* Use the NT os functions, not posix */ + {"_os", PyInit__os}, + {"nt", PyInit__os}, {"_operator", PyInit__operator}, {"_signal", PyInit__signal}, {"_sysconfig", PyInit__sysconfig}, diff --git a/PC/config_minimal.c b/PC/config_minimal.c index 9a66ea1d1cd3482..20a844ca87e68d6 100644 --- a/PC/config_minimal.c +++ b/PC/config_minimal.c @@ -14,7 +14,7 @@ void *PyWin_DLLhModule = NULL; extern PyObject* PyInit_faulthandler(void); extern PyObject* PyInit__tracemalloc(void); extern PyObject* PyInit_gc(void); -extern PyObject* PyInit_nt(void); +extern PyObject* PyInit__os(void); extern PyObject* PyInit__signal(void); #if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM) || defined(MS_WINDOWS_GAMES) extern PyObject* PyInit_winreg(void); @@ -34,7 +34,8 @@ struct _inittab _PyImport_Inittab[] = { {"_ast", PyInit__ast}, {"faulthandler", PyInit_faulthandler}, {"gc", PyInit_gc}, - {"nt", PyInit_nt}, /* Use the NT os functions, not posix */ + {"_os", PyInit__os}, + {"nt", PyInit__os}, {"_signal", PyInit__signal}, {"_tokenize", PyInit__tokenize}, {"_tracemalloc", PyInit__tracemalloc}, diff --git a/Python/stdlib_module_names.h b/Python/stdlib_module_names.h index 565be27b7cebda5..2f55c8bae9d7ce9 100644 --- a/Python/stdlib_module_names.h +++ b/Python/stdlib_module_names.h @@ -58,6 +58,7 @@ static const char* _Py_stdlib_module_names[] = { "_opcode", "_opcode_metadata", "_operator", +"_os", "_osx_support", "_overlapped", "_pickle", diff --git a/Tools/build/check_extension_modules.py b/Tools/build/check_extension_modules.py index e907442b61ea22f..25594b1d12cf78b 100644 --- a/Tools/build/check_extension_modules.py +++ b/Tools/build/check_extension_modules.py @@ -56,6 +56,7 @@ "builtins", "gc", "marshal", + "posix", "sys", } diff --git a/Tools/freeze/makeconfig.py b/Tools/freeze/makeconfig.py index fabaace07a83e01..05db5d7b89bebdc 100644 --- a/Tools/freeze/makeconfig.py +++ b/Tools/freeze/makeconfig.py @@ -4,7 +4,7 @@ # Write the config.c file never = ['marshal', '_imp', '_ast', '__main__', 'builtins', - 'sys', 'gc', '_warnings'] + 'sys', 'gc', '_warnings', 'posix', 'nt'] def makeconfig(infp, outfp, modules, with_ifdef=0): m1 = re.compile('-- ADDMODULE MARKER 1 --') From a0266b86073f4a3813cdde028d169cb2df0ae6b3 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Thu, 12 Nov 2026 13:56:54 -0800 Subject: [PATCH 2/3] Move module name versionchanged chunk to end of intro --- Doc/library/posix.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Doc/library/posix.rst b/Doc/library/posix.rst index 440cba8296d9dc6..251730b861334ba 100644 --- a/Doc/library/posix.rst +++ b/Doc/library/posix.rst @@ -23,13 +23,12 @@ available through the :mod:`os` interface. Once :mod:`os` is imported, there is :mod:`os` provides some additional functionality, such as automatically calling :func:`~os.putenv` when an entry in ``os.environ`` is changed. -.. versionchanged:: next - Functions in this module report :mod:`os` as their ``__module__`` and - are :ref:`pickled ` as ``os.``. - Errors are reported as exceptions; the usual exceptions are given for type errors, while errors reported by the system calls raise :exc:`OSError`. +.. versionchanged:: next + This module and its contents report :mod:`os` as ``__module__`` and are + :ref:`pickled ` as ``os.``. .. _posix-large-files: From c0ce25dc3e76b7827eed2030b131f044ba786166 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Tue, 17 Nov 2026 12:53:43 -0800 Subject: [PATCH 3/3] Apply batched suggestions from code review Co-authored-by: Stan Ulbrych Co-authored-by: Cody Maloney --- Doc/library/posix.rst | 5 +++-- Lib/test/test_importlib/builtin/test_loader.py | 2 -- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Doc/library/posix.rst b/Doc/library/posix.rst index 251730b861334ba..a10e4802773205a 100644 --- a/Doc/library/posix.rst +++ b/Doc/library/posix.rst @@ -27,8 +27,9 @@ Errors are reported as exceptions; the usual exceptions are given for type errors, while errors reported by the system calls raise :exc:`OSError`. .. versionchanged:: next - This module and its contents report :mod:`os` as ``__module__`` and are - :ref:`pickled ` as ``os.``. + This module's :attr:`~module.__name__` and the + :attr:`~function.__module__` of its functions are now ``'os'``, and its + functions are :ref:`pickled ` as ``os.``. .. _posix-large-files: diff --git a/Lib/test/test_importlib/builtin/test_loader.py b/Lib/test/test_importlib/builtin/test_loader.py index a912d7627fb5441..79e64a11464750e 100644 --- a/Lib/test/test_importlib/builtin/test_loader.py +++ b/Lib/test/test_importlib/builtin/test_loader.py @@ -11,7 +11,6 @@ @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') class InspectLoaderTests: - """Tests for InspectLoader methods for BuiltinImporter.""" def test_get_code(self): @@ -44,7 +43,6 @@ def test_not_builtin(self): class OsModuleTests: - """Test loading _os and its historical name by hand.""" def test_load_by_hand(self):