Skip to content

Commit 272eb23

Browse files
cmaloneyclaude
andcommitted
gh-71199: Report os as the module of posixmodule types and functions
Types such as times_result, uname_result and sched_param, and every function, reported the platform module (posix or nt) as __module__, so pickles referenced posix.times_result or posix.stat and could not be loaded on Windows. Name the types os.<type> like stat_result (gh-63408), and set the module's __name__ to os before adding its functions. os re-exports all of them, and older pickles keep loading through the platform module. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d05a3bf commit 272eb23

7 files changed

Lines changed: 39 additions & 14 deletions

File tree

‎Include/internal/pycore_global_objects_fini_generated.h‎

Lines changed: 1 addition & 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ struct _Py_global_strings {
717717
STRUCT_FOR_ID(options)
718718
STRUCT_FOR_ID(order)
719719
STRUCT_FOR_ID(origin)
720+
STRUCT_FOR_ID(os)
720721
STRUCT_FOR_ID(other)
721722
STRUCT_FOR_ID(out_fd)
722723
STRUCT_FOR_ID(outgoing)

‎Include/internal/pycore_runtime_init_generated.h‎

Lines changed: 1 addition & 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: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Lib/test/test_os/test_os.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,16 @@ def test_type_fqdn(self):
179179
def fqdn(obj):
180180
return (obj.__module__, obj.__qualname__)
181181

182-
native = os.name
183182
self.assertEqual(fqdn(os.stat_result), ("os", "stat_result"))
184-
self.assertEqual(fqdn(os.times_result), (native, "times_result"))
183+
self.assertEqual(fqdn(os.times_result), ("os", "times_result"))
184+
self.assertEqual(fqdn(os.DirEntry), ("os", "DirEntry"))
185+
self.assertEqual(fqdn(os.stat), ("os", "stat"))
185186
if hasattr(os, "statvfs_result"):
186187
self.assertEqual(fqdn(os.statvfs_result), ("os", "statvfs_result"))
187188
if hasattr(os, "sched_param"):
188-
self.assertEqual(fqdn(os.sched_param), (native, "sched_param"))
189+
self.assertEqual(fqdn(os.sched_param), ("os", "sched_param"))
189190
if hasattr(os, "waitid_result"):
190-
self.assertEqual(fqdn(os.waitid_result), (native, "waitid_result"))
191+
self.assertEqual(fqdn(os.waitid_result), ("os", "waitid_result"))
191192

192193
def test_platform_module(self):
193194
# posix and nt stay builtins, importable only on their own platform.
@@ -206,6 +207,14 @@ def test_platform_module_from_spec(self):
206207
self.assertEqual(mod.getpid(), os.getpid())
207208
self.assertIs(sys.modules[os.name].stat, os.stat)
208209

210+
def test_function_pickle(self):
211+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
212+
with self.subTest(f'protocol {proto}'):
213+
p = pickle.dumps(os.stat, proto)
214+
if proto < 4:
215+
self.assertIn(b'cos\nstat\n', p)
216+
self.assertIs(pickle.loads(p), os.stat)
217+
209218
def test_pickle_global_names(self):
210219
# Pickles reference these via the platform module (posix or nt) or
211220
# via os; both must keep loading.
@@ -5614,6 +5623,8 @@ def test_times_result_pickle(self):
56145623
with self.subTest(f'protocol {proto}'):
56155624
p = pickle.dumps(result, proto)
56165625
self.assertIn(b'times_result', p)
5626+
if proto < 4:
5627+
self.assertIn(b'cos\ntimes_result\n', p)
56175628
unpickled = pickle.loads(p)
56185629
self.assertEqual(result, unpickled)
56195630

‎Lib/test/test_os/test_posix.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,10 @@ def test_get_and_set_scheduler_and_param(self):
14471447
def test_sched_param(self):
14481448
param = posix.sched_param(1)
14491449
for proto in range(pickle.HIGHEST_PROTOCOL+1):
1450-
newparam = pickle.loads(pickle.dumps(param, proto))
1450+
p = pickle.dumps(param, proto)
1451+
if proto < 4:
1452+
self.assertIn(b'cos\nsched_param\n', p)
1453+
newparam = pickle.loads(p)
14511454
self.assertEqual(newparam, param)
14521455
newparam = copy.copy(param)
14531456
self.assertIsNot(newparam, param)

‎Modules/posixmodule.c‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,9 @@ static const unsigned int _Py_STATX_KNOWN = (STATX_BASIC_STATS | STATX_BTIME
655655
#endif
656656

657657
/*[clinic input]
658-
# one of the few times we lie about this name!
659658
module os
660659
[clinic start generated code]*/
661-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
660+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8cff096d1133288f]*/
662661

663662
PyDoc_STRVAR(posix__doc__,
664663
"This module provides access to operating system functionality that is\n\
@@ -2561,7 +2560,7 @@ static PyStructSequence_Field waitid_result_fields[] = {
25612560
};
25622561

25632562
static PyStructSequence_Desc waitid_result_desc = {
2564-
MODNAME ".waitid_result", /* name */
2563+
"os.waitid_result", /* name */
25652564
waitid_result__doc__, /* doc */
25662565
waitid_result_fields,
25672566
5
@@ -6768,7 +6767,7 @@ or via the attributes sysname, nodename, release, version, and machine.\n\
67686767
See os.uname for more information.");
67696768

67706769
static PyStructSequence_Desc uname_result_desc = {
6771-
MODNAME ".uname_result", /* name */
6770+
"os.uname_result", /* name */
67726771
uname_result__doc__, /* doc */
67736772
uname_result_fields,
67746773
5
@@ -8818,7 +8817,7 @@ static PyStructSequence_Field sched_param_fields[] = {
88188817
};
88198818

88208819
static PyStructSequence_Desc sched_param_desc = {
8821-
MODNAME ".sched_param", /* name */
8820+
"os.sched_param", /* name */
88228821
os_sched_param__doc__, /* doc */
88238822
sched_param_fields,
88248823
1
@@ -11376,7 +11375,7 @@ and elapsed.\n\
1137611375
See os.times for more information.");
1137711376

1137811377
static PyStructSequence_Desc times_result_desc = {
11379-
MODNAME ".times_result", /* name */
11378+
"os.times_result", /* name */
1138011379
times_result__doc__, /* doc */
1138111380
times_result_fields,
1138211381
5
@@ -16840,7 +16839,7 @@ static PyType_Slot DirEntryType_slots[] = {
1684016839
};
1684116840

1684216841
static PyType_Spec DirEntryType_spec = {
16843-
.name = MODNAME ".DirEntry",
16842+
.name = "os.DirEntry",
1684416843
.basicsize = sizeof(DirEntry),
1684516844
.flags = (
1684616845
Py_TPFLAGS_DEFAULT
@@ -17397,7 +17396,7 @@ static PyType_Slot ScandirIteratorType_slots[] = {
1739717396
};
1739817397

1739917398
static PyType_Spec ScandirIteratorType_spec = {
17400-
.name = MODNAME ".ScandirIterator",
17399+
.name = "os.ScandirIterator",
1740117400
.basicsize = sizeof(ScandirIterator),
1740217401
// bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
1740317402
// PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
@@ -19106,6 +19105,15 @@ posixmodule_exec(PyObject *m)
1910619105
{
1910719106
_posixstate *state = get_posix_state(m);
1910819107

19108+
// Name the module os before adding functions, so they report os as
19109+
// their module and pickle as os.<name> on every platform.
19110+
if (PyObject_SetAttr(m, &_Py_ID(__name__), &_Py_ID(os)) < 0) {
19111+
return -1;
19112+
}
19113+
if (PyModule_AddFunctions(m, posix_methods) < 0) {
19114+
return -1;
19115+
}
19116+
1910919117
#if defined(HAVE_PWRITEV)
1911019118
if (HAVE_PWRITEV_RUNTIME) {} else {
1911119119
PyObject* dct = PyModule_GetDict(m);
@@ -19302,7 +19310,6 @@ static struct PyModuleDef posixmodule = {
1930219310
.m_name = MODNAME,
1930319311
.m_doc = posix__doc__,
1930419312
.m_size = sizeof(_posixstate),
19305-
.m_methods = posix_methods,
1930619313
.m_slots = posixmodile_slots,
1930719314
.m_traverse = _posix_traverse,
1930819315
.m_clear = _posix_clear,

0 commit comments

Comments
 (0)