Skip to content

Commit 616afa3

Browse files
cmaloneyclaude
andcommitted
gh-71199: Import _os directly in os
os chose between posix and nt by looking at sys.builtin_module_names. Import _os on every platform and take os.name from sys.platform, as importlib already does. Besides the public names, re-export _os's private helpers so the rest of the stdlib can use os._name. _os always provides _have_functions and _create_environ, so drop the guards around them; review with git diff -w. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8282ff2 commit 616afa3

1 file changed

Lines changed: 126 additions & 169 deletions

File tree

‎Lib/os.py‎

Lines changed: 126 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
r"""OS routines for NT or Posix depending on what system we're on.
22
33
This exports:
4-
- all functions from posix or nt, e.g. unlink, stat, etc.
4+
- all functions from _os, e.g. unlink, stat, etc.
55
- os.path is either posixpath or ntpath
66
- os.name is either 'posix' or 'nt'
77
- os.curdir is a string representing the current directory (always '.')
@@ -30,8 +30,6 @@
3030

3131
GenericAlias = type(list[int])
3232

33-
_names = sys.builtin_module_names
34-
3533
# Note: more names are added to __all__ later.
3634
__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
3735
"defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR",
@@ -49,167 +47,128 @@ def _get_exports_list(module):
4947

5048
# Any new dependencies of the os module and/or changes in path separator
5149
# requires updating importlib as well.
52-
if 'posix' in _names:
53-
name = 'posix'
54-
linesep = '\n'
55-
from posix import *
56-
try:
57-
from posix import _exit
58-
__all__.append('_exit')
59-
except ImportError:
60-
pass
61-
try:
62-
from posix import _clearenv
63-
__all__.append('_clearenv')
64-
except ImportError:
65-
pass
66-
import posixpath as path
67-
68-
try:
69-
from posix import _have_functions
70-
except ImportError:
71-
pass
72-
try:
73-
from posix import _create_environ
74-
except ImportError:
75-
pass
76-
77-
import posix
78-
__all__.extend(_get_exports_list(posix))
79-
del posix
80-
81-
elif 'nt' in _names:
50+
import _os
51+
from _os import *
52+
# The private helpers too, for the rest of the stdlib.
53+
globals().update({k: v for k, v in vars(_os).items()
54+
if k.startswith('_') and not k.startswith('__')})
55+
__all__.append('_exit')
56+
if _exists('_clearenv'):
57+
__all__.append('_clearenv')
58+
__all__.extend(_get_exports_list(_os))
59+
del _os
60+
61+
if sys.platform == 'win32':
8262
name = 'nt'
8363
linesep = '\r\n'
84-
from nt import *
85-
try:
86-
from nt import _exit
87-
__all__.append('_exit')
88-
except ImportError:
89-
pass
9064
import ntpath as path
91-
92-
import nt
93-
__all__.extend(_get_exports_list(nt))
94-
del nt
95-
96-
try:
97-
from nt import _have_functions
98-
except ImportError:
99-
pass
100-
try:
101-
from nt import _create_environ
102-
except ImportError:
103-
pass
104-
10565
else:
106-
raise ImportError('no os specific module found')
66+
name = 'posix'
67+
linesep = '\n'
68+
import posixpath as path
10769

10870
sys.modules['os.path'] = path
10971
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
11072
devnull)
11173

112-
del _names
113-
114-
115-
if _exists("_have_functions"):
116-
_globals = globals()
117-
def _add(str, fn):
118-
if (fn in _globals) and (str in _have_functions):
119-
_set.add(_globals[fn])
120-
121-
_set = set()
122-
_add("HAVE_FACCESSAT", "access")
123-
_add("HAVE_FCHMODAT", "chmod")
124-
_add("HAVE_FCHOWNAT", "chown")
125-
_add("HAVE_FSTATAT", "stat")
126-
_add("HAVE_LSTAT", "lstat")
127-
_add("HAVE_FUTIMESAT", "utime")
128-
_add("HAVE_LINKAT", "link")
129-
_add("HAVE_MKDIRAT", "mkdir")
130-
_add("HAVE_MKFIFOAT", "mkfifo")
131-
_add("HAVE_MKNODAT", "mknod")
132-
_add("HAVE_OPENAT", "open")
74+
75+
_globals = globals()
76+
def _add(str, fn):
77+
if (fn in _globals) and (str in _have_functions):
78+
_set.add(_globals[fn])
79+
80+
_set = set()
81+
_add("HAVE_FACCESSAT", "access")
82+
_add("HAVE_FCHMODAT", "chmod")
83+
_add("HAVE_FCHOWNAT", "chown")
84+
_add("HAVE_FSTATAT", "stat")
85+
_add("HAVE_LSTAT", "lstat")
86+
_add("HAVE_FUTIMESAT", "utime")
87+
_add("HAVE_LINKAT", "link")
88+
_add("HAVE_MKDIRAT", "mkdir")
89+
_add("HAVE_MKFIFOAT", "mkfifo")
90+
_add("HAVE_MKNODAT", "mknod")
91+
_add("HAVE_OPENAT", "open")
92+
_add("HAVE_READLINKAT", "readlink")
93+
_add("HAVE_RENAMEAT", "rename")
94+
_add("HAVE_SYMLINKAT", "symlink")
95+
_add("HAVE_UNLINKAT", "unlink")
96+
_add("HAVE_UNLINKAT", "rmdir")
97+
_add("HAVE_UTIMENSAT", "utime")
98+
if _exists("statx"):
99+
_set.add(statx)
100+
supports_dir_fd = _set
101+
102+
_set = set()
103+
_add("HAVE_FACCESSAT", "access")
104+
supports_effective_ids = _set
105+
106+
_set = set()
107+
_add("HAVE_FCHDIR", "chdir")
108+
_add("HAVE_FCHMOD", "chmod")
109+
_add("MS_WINDOWS", "chmod")
110+
_add("HAVE_FCHOWN", "chown")
111+
_add("HAVE_FDOPENDIR", "listdir")
112+
_add("HAVE_FDOPENDIR", "scandir")
113+
_add("HAVE_FEXECVE", "execve")
114+
_set.add(stat) # fstat always works
115+
_add("HAVE_FTRUNCATE", "truncate")
116+
_add("HAVE_FUTIMENS", "utime")
117+
_add("HAVE_FUTIMES", "utime")
118+
_add("HAVE_FPATHCONF", "pathconf")
119+
if _exists("statvfs") and _exists("fstatvfs"): # mac os x10.3
120+
_add("HAVE_FSTATVFS", "statvfs")
121+
if _exists("statx"):
122+
_set.add(statx)
123+
_add("HAVE_FREADLINK", "readlink")
124+
if sys.platform in ["linux", "android"] and "O_PATH" in _globals:
133125
_add("HAVE_READLINKAT", "readlink")
134-
_add("HAVE_RENAMEAT", "rename")
135-
_add("HAVE_SYMLINKAT", "symlink")
136-
_add("HAVE_UNLINKAT", "unlink")
137-
_add("HAVE_UNLINKAT", "rmdir")
138-
_add("HAVE_UTIMENSAT", "utime")
139-
if _exists("statx"):
140-
_set.add(statx)
141-
supports_dir_fd = _set
142-
143-
_set = set()
144-
_add("HAVE_FACCESSAT", "access")
145-
supports_effective_ids = _set
146-
147-
_set = set()
148-
_add("HAVE_FCHDIR", "chdir")
149-
_add("HAVE_FCHMOD", "chmod")
150-
_add("MS_WINDOWS", "chmod")
151-
_add("HAVE_FCHOWN", "chown")
152-
_add("HAVE_FDOPENDIR", "listdir")
153-
_add("HAVE_FDOPENDIR", "scandir")
154-
_add("HAVE_FEXECVE", "execve")
155-
_set.add(stat) # fstat always works
156-
_add("HAVE_FTRUNCATE", "truncate")
157-
_add("HAVE_FUTIMENS", "utime")
158-
_add("HAVE_FUTIMES", "utime")
159-
_add("HAVE_FPATHCONF", "pathconf")
160-
if _exists("statvfs") and _exists("fstatvfs"): # mac os x10.3
161-
_add("HAVE_FSTATVFS", "statvfs")
162-
if _exists("statx"):
163-
_set.add(statx)
164-
_add("HAVE_FREADLINK", "readlink")
165-
if sys.platform in ["linux", "android"] and "O_PATH" in _globals:
166-
_add("HAVE_READLINKAT", "readlink")
167-
supports_fd = _set
168-
169-
_set = set()
170-
_add("HAVE_FACCESSAT", "access")
171-
# Some platforms don't support lchmod(). Often the function exists
172-
# anyway, as a stub that always returns ENOSUP or perhaps EOPNOTSUPP.
173-
# (No, I don't know why that's a good design.) ./configure will detect
174-
# this and reject it--so HAVE_LCHMOD still won't be defined on such
175-
# platforms. This is Very Helpful.
176-
#
177-
# However, sometimes platforms without a working lchmod() *do* have
178-
# fchmodat(). (Examples: Linux kernel 3.2 with glibc 2.15,
179-
# OpenIndiana 3.x.) And fchmodat() has a flag that theoretically makes
180-
# it behave like lchmod(). So in theory it would be a suitable
181-
# replacement for lchmod(). But when lchmod() doesn't work, fchmodat()'s
182-
# flag doesn't work *either*. Sadly ./configure isn't sophisticated
183-
# enough to detect this condition--it only determines whether or not
184-
# fchmodat() minimally works.
185-
#
186-
# Therefore we simply ignore fchmodat() when deciding whether or not
187-
# os.chmod supports follow_symlinks. Just checking lchmod() is
188-
# sufficient. After all--if you have a working fchmodat(), your
189-
# lchmod() almost certainly works too.
190-
#
191-
# _add("HAVE_FCHMODAT", "chmod")
192-
_add("HAVE_FCHOWNAT", "chown")
193-
_add("HAVE_FSTATAT", "stat")
194-
_add("HAVE_LCHFLAGS", "chflags")
195-
_add("HAVE_LCHMOD", "chmod")
196-
_add("MS_WINDOWS", "chmod")
197-
if _exists("lchown"): # mac os x10.3
198-
_add("HAVE_LCHOWN", "chown")
199-
_add("HAVE_LINKAT", "link")
200-
_add("HAVE_LUTIMES", "utime")
201-
_add("HAVE_LSTAT", "stat")
202-
_add("HAVE_FSTATAT", "stat")
203-
_add("HAVE_UTIMENSAT", "utime")
204-
_add("MS_WINDOWS", "stat")
205-
if _exists("statx"):
206-
_set.add(statx)
207-
supports_follow_symlinks = _set
208-
209-
del _set
210-
del _have_functions
211-
del _globals
212-
del _add
126+
supports_fd = _set
127+
128+
_set = set()
129+
_add("HAVE_FACCESSAT", "access")
130+
# Some platforms don't support lchmod(). Often the function exists
131+
# anyway, as a stub that always returns ENOSUP or perhaps EOPNOTSUPP.
132+
# (No, I don't know why that's a good design.) ./configure will detect
133+
# this and reject it--so HAVE_LCHMOD still won't be defined on such
134+
# platforms. This is Very Helpful.
135+
#
136+
# However, sometimes platforms without a working lchmod() *do* have
137+
# fchmodat(). (Examples: Linux kernel 3.2 with glibc 2.15,
138+
# OpenIndiana 3.x.) And fchmodat() has a flag that theoretically makes
139+
# it behave like lchmod(). So in theory it would be a suitable
140+
# replacement for lchmod(). But when lchmod() doesn't work, fchmodat()'s
141+
# flag doesn't work *either*. Sadly ./configure isn't sophisticated
142+
# enough to detect this condition--it only determines whether or not
143+
# fchmodat() minimally works.
144+
#
145+
# Therefore we simply ignore fchmodat() when deciding whether or not
146+
# os.chmod supports follow_symlinks. Just checking lchmod() is
147+
# sufficient. After all--if you have a working fchmodat(), your
148+
# lchmod() almost certainly works too.
149+
#
150+
# _add("HAVE_FCHMODAT", "chmod")
151+
_add("HAVE_FCHOWNAT", "chown")
152+
_add("HAVE_FSTATAT", "stat")
153+
_add("HAVE_LCHFLAGS", "chflags")
154+
_add("HAVE_LCHMOD", "chmod")
155+
_add("MS_WINDOWS", "chmod")
156+
if _exists("lchown"): # mac os x10.3
157+
_add("HAVE_LCHOWN", "chown")
158+
_add("HAVE_LINKAT", "link")
159+
_add("HAVE_LUTIMES", "utime")
160+
_add("HAVE_LSTAT", "stat")
161+
_add("HAVE_FSTATAT", "stat")
162+
_add("HAVE_UTIMENSAT", "utime")
163+
_add("MS_WINDOWS", "stat")
164+
if _exists("statx"):
165+
_set.add(statx)
166+
supports_follow_symlinks = _set
167+
168+
del _set
169+
del _have_functions
170+
del _globals
171+
del _add
213172

214173

215174
# Python uses fixed values for the SEEK_ constants; they are mapped
@@ -848,20 +807,19 @@ def decode(value):
848807
del _create_environ_mapping
849808

850809

851-
if _exists("_create_environ"):
852-
def reload_environ():
853-
data = _create_environ()
854-
if name == 'nt':
855-
encodekey = environ.encodekey
856-
data = {encodekey(key): value
857-
for key, value in data.items()}
810+
def reload_environ():
811+
data = _create_environ()
812+
if name == 'nt':
813+
encodekey = environ.encodekey
814+
data = {encodekey(key): value
815+
for key, value in data.items()}
858816

859-
# modify in-place to keep os.environb in sync
860-
env_data = environ._data
861-
env_data.clear()
862-
env_data.update(data)
817+
# modify in-place to keep os.environb in sync
818+
env_data = environ._data
819+
env_data.clear()
820+
env_data.update(data)
863821

864-
__all__.append("reload_environ")
822+
__all__.append("reload_environ")
865823

866824
def getenv(key, default=None):
867825
"""Get an environment variable, return None if it doesn't exist.
@@ -1216,12 +1174,11 @@ def add_dll_directory(path):
12161174
Remove the directory by calling close() on the returned object or
12171175
using it in a with statement.
12181176
"""
1219-
import nt
1220-
cookie = nt._add_dll_directory(path)
1177+
cookie = _add_dll_directory(path)
12211178
return _AddedDllDirectory(
12221179
path,
12231180
cookie,
1224-
nt._remove_dll_directory
1181+
_remove_dll_directory
12251182
)
12261183

12271184

0 commit comments

Comments
 (0)