Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Doc/library/posix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ available through the :mod:`os` interface. Once :mod:`os` is imported, there is
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 <pickle-picklable>` as ``os.<name>``.

.. _posix-large-files:

Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

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

2 changes: 2 additions & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_runtime_init_generated.h

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

2 changes: 2 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

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

4 changes: 1 addition & 3 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@
# Import builtin modules
import _imp
import _io
import _os
import sys
import _warnings
import marshal


_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:
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_importlib/builtin/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import os
import sys
import types
import unittest
Expand Down Expand Up @@ -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()
25 changes: 25 additions & 0 deletions Lib/test/test_os/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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.<name> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.<name>``.
2 changes: 1 addition & 1 deletion Modules/Setup.bootstrap.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Modules/config.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};
Expand Down
45 changes: 36 additions & 9 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.<name> 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);
Expand Down Expand Up @@ -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;
}


Expand All @@ -19300,18 +19328,17 @@ 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,
.m_free = _posix_free,
};

PyMODINIT_FUNC
INITFUNC(void)
PyInit__os(void)
{
return PyModuleDef_Init(&posixmodule);
}
5 changes: 3 additions & 2 deletions PC/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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},
Expand Down
5 changes: 3 additions & 2 deletions PC/config_minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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},
Expand Down
1 change: 1 addition & 0 deletions Python/stdlib_module_names.h

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

1 change: 1 addition & 0 deletions Tools/build/check_extension_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"builtins",
"gc",
"marshal",
"posix",
"sys",
}

Expand Down
2 changes: 1 addition & 1 deletion Tools/freeze/makeconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 --')
Expand Down
Loading