Skip to content

Commit 6bd4449

Browse files
cmaloneyclaude
andcommitted
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 <noreply@anthropic.com>
1 parent eb7cd6e commit 6bd4449

17 files changed

Lines changed: 116 additions & 18 deletions

‎Doc/library/posix.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ available through the :mod:`os` interface. Once :mod:`os` is imported, there is
2323
:mod:`os` provides some additional functionality, such as automatically calling
2424
:func:`~os.putenv` when an entry in ``os.environ`` is changed.
2525

26+
.. versionchanged:: next
27+
Functions in this module report :mod:`os` as their ``__module__`` and
28+
are :ref:`pickled <pickle-picklable>` as ``os.<name>``.
29+
2630
Errors are reported as exceptions; the usual exceptions are given for type
2731
errors, while errors reported by the system calls raise :exc:`OSError`.
2832

‎Include/internal/pycore_global_objects_fini_generated.h‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_global_strings.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ struct _Py_global_strings {
287287
STRUCT_FOR_ID(_loop)
288288
STRUCT_FOR_ID(_needs_com_addref_)
289289
STRUCT_FOR_ID(_only_immortal)
290+
STRUCT_FOR_ID(_os)
290291
STRUCT_FOR_ID(_restype_)
291292
STRUCT_FOR_ID(_showwarnmsg)
292293
STRUCT_FOR_ID(_shutdown)
@@ -717,6 +718,7 @@ struct _Py_global_strings {
717718
STRUCT_FOR_ID(options)
718719
STRUCT_FOR_ID(order)
719720
STRUCT_FOR_ID(origin)
721+
STRUCT_FOR_ID(os)
720722
STRUCT_FOR_ID(other)
721723
STRUCT_FOR_ID(out_fd)
722724
STRUCT_FOR_ID(outgoing)

‎Include/internal/pycore_runtime_init_generated.h‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Include/internal/pycore_unicodeobject_generated.h‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Lib/importlib/_bootstrap_external.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,18 @@
2525
# Import builtin modules
2626
import _imp
2727
import _io
28+
import _os
2829
import sys
2930
import _warnings
3031
import marshal
3132

3233

3334
_MS_WINDOWS = (sys.platform == 'win32')
3435
if _MS_WINDOWS:
35-
import nt as _os
3636
try:
3737
import winreg
3838
except ImportError:
3939
winreg = None
40-
else:
41-
import posix as _os
4240

4341

4442
if _MS_WINDOWS:

‎Lib/test/test_importlib/builtin/test_loader.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
machinery = util.import_importlib('importlib.machinery')
44

5+
import os
56
import sys
67
import types
78
import unittest
@@ -42,5 +43,29 @@ def test_not_builtin(self):
4243
) = util.test_both(InspectLoaderTests, machinery=machinery)
4344

4445

46+
class OsModuleTests:
47+
48+
"""Test loading _os and its historical name by hand."""
49+
50+
def test_load_by_hand(self):
51+
# As LazyLoader does: a working module, sys.modules left alone.
52+
loader = self.machinery.BuiltinImporter
53+
for name in ('_os', os.name):
54+
with self.subTest(name):
55+
spec = loader.find_spec(name)
56+
mod = loader.create_module(spec)
57+
loader.exec_module(mod)
58+
self.assertEqual(mod.__name__, 'os')
59+
self.assertEqual(mod.getpid(), os.getpid())
60+
self.assertIsNot(mod, sys.modules[name])
61+
self.assertIs(sys.modules[os.name], sys.modules['_os'])
62+
self.assertIs(sys.modules[os.name].stat, os.stat)
63+
64+
65+
(Frozen_OsModuleTests,
66+
Source_OsModuleTests
67+
) = util.test_both(OsModuleTests, machinery=machinery)
68+
69+
4570
if __name__ == '__main__':
4671
unittest.main()

‎Lib/test/test_os/test_os.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def fqdn(obj):
194194
self.assertEqual(fqdn(os.stat_result), ("os", "stat_result"))
195195
self.assertEqual(fqdn(os.times_result), ("os", "times_result"))
196196
self.assertEqual(fqdn(os.DirEntry), ("os", "DirEntry"))
197+
self.assertEqual(fqdn(os.stat), ("os", "stat"))
197198
if hasattr(os, "statvfs_result"):
198199
self.assertEqual(fqdn(os.statvfs_result), ("os", "statvfs_result"))
199200
self.assertEqual(fqdn(os.uname_result), ("os", "uname_result"))
@@ -202,6 +203,30 @@ def fqdn(obj):
202203
if hasattr(os, "waitid_result"):
203204
self.assertEqual(fqdn(os.waitid_result), ("os", "waitid_result"))
204205

206+
def test_platform_module(self):
207+
# posix and nt stay builtins, importable only on their own platform,
208+
# and are the _os module under its historical name.
209+
import _os
210+
other = 'posix' if os.name == 'nt' else 'nt'
211+
mod = __import__(os.name)
212+
self.assertIs(mod, _os)
213+
self.assertIs(mod.stat, os.stat)
214+
self.assertEqual(mod.__name__, 'os')
215+
self.assertIn('_os', sys.builtin_module_names)
216+
self.assertIn(os.name, sys.builtin_module_names)
217+
self.assertNotIn(other, sys.builtin_module_names)
218+
with self.assertRaises(ImportError):
219+
__import__(other)
220+
221+
def test_function_pickle(self):
222+
# Functions pickle as os.<name> on every platform.
223+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
224+
with self.subTest(f'protocol {proto}'):
225+
p = pickle.dumps(os.stat, proto)
226+
if proto < 4:
227+
self.assertIn(b'cos\nstat\n', p)
228+
self.assertIs(pickle.loads(p), os.stat)
229+
205230
def test_pickle_global_names(self):
206231
# Existing pickles name these via os or the platform module (posix
207232
# or nt); both must keep loading, as must new pickles.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Change :mod:`posix` (:mod:`!nt` on Windows) to use the module name :mod:`os`.
2+
Platform-specific import names keep working and functions now pickle
3+
as ``os.<name>``.

‎Modules/Setup.bootstrap.in‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# module C APIs are used in core
1313
atexit atexitmodule.c
1414
faulthandler faulthandler.c
15-
posix posixmodule.c
15+
_os posixmodule.c
1616
_signal signalmodule.c
1717
_tracemalloc _tracemalloc.c
1818
_suggestions _suggestions.c

0 commit comments

Comments
 (0)