Skip to content

Commit fa7524d

Browse files
committed
Change the name of the _os module to posix/nt
1 parent f21e35c commit fa7524d

5 files changed

Lines changed: 67 additions & 41 deletions

File tree

‎Lib/importlib/_bootstrap_external.py‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,21 +1575,22 @@ def _setup(_bootstrap_module):
15751575
setattr(self_module, builtin_name, builtin_module)
15761576

15771577
# Directly load the os module (needed during bootstrap).
1578-
# XXX Changed to fit RustPython!!!
1579-
builtin_os = "_os"
1580-
if builtin_os in sys.modules:
1581-
os_module = sys.modules[builtin_os]
1578+
os_details = ('posix', ['/']), ('nt', ['\\', '/'])
1579+
for builtin_os, path_separators in os_details:
1580+
# Assumption made in _path_join()
1581+
assert all(len(sep) == 1 for sep in path_separators)
1582+
path_sep = path_separators[0]
1583+
if builtin_os in sys.modules:
1584+
os_module = sys.modules[builtin_os]
1585+
break
1586+
else:
1587+
try:
1588+
os_module = _bootstrap._builtin_from_name(builtin_os)
1589+
break
1590+
except ImportError:
1591+
continue
15821592
else:
1583-
try:
1584-
os_module = _bootstrap._builtin_from_name(builtin_os)
1585-
except ImportError:
1586-
raise ImportError('importlib requires _os')
1587-
path_separators = ['\\', '/'] if os_module.name == 'nt' else ['/']
1588-
1589-
# Assumption made in _path_join()
1590-
assert all(len(sep) == 1 for sep in path_separators)
1591-
path_sep = path_separators[0]
1592-
1593+
raise ImportError('importlib requires posix or nt')
15931594
setattr(self_module, '_os', os_module)
15941595
setattr(self_module, 'path_sep', path_sep)
15951596
setattr(self_module, 'path_separators', ''.join(path_separators))

‎Lib/os.py‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,50 @@ def _get_exports_list(module):
4343
except AttributeError:
4444
return [n for n in dir(module) if n[0] != '_']
4545

46-
import _os
47-
from _os import *
48-
from _os import _exit
49-
__all__.extend(_get_exports_list(_os))
50-
del _os
51-
5246
# Any new dependencies of the os module and/or changes in path separator
5347
# requires updating importlib as well.
54-
if name == 'nt':
55-
linesep = '\r\n'
56-
import ntpath as path
57-
else:
48+
if 'posix' in _names:
49+
name = 'posix'
5850
linesep = '\n'
51+
from posix import *
52+
try:
53+
from posix import _exit
54+
__all__.append('_exit')
55+
except ImportError:
56+
pass
5957
import posixpath as path
6058

59+
try:
60+
from posix import _have_functions
61+
except ImportError:
62+
pass
63+
64+
import posix
65+
__all__.extend(_get_exports_list(posix))
66+
del posix
67+
68+
elif 'nt' in _names:
69+
name = 'nt'
70+
linesep = '\r\n'
71+
from nt import *
72+
try:
73+
from nt import _exit
74+
__all__.append('_exit')
75+
except ImportError:
76+
pass
77+
import ntpath as path
78+
79+
import nt
80+
__all__.extend(_get_exports_list(nt))
81+
del nt
82+
83+
try:
84+
from nt import _have_functions
85+
except ImportError:
86+
pass
87+
88+
else:
89+
raise ImportError('no os specific module found')
6190

6291
sys.modules['os.path'] = path
6392
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,

‎Lib/shutil.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,9 +1015,7 @@ def disk_usage(path):
10151015

10161016
elif os.name == 'nt':
10171017

1018-
# XXX RustPython TODO: figure out what to do with posix vs nt vs os
1019-
# import nt
1020-
import os as nt
1018+
import nt
10211019
__all__.append('disk_usage')
10221020
_ntuple_diskusage = collections.namedtuple('usage', 'total used free')
10231021

‎vm/src/stdlib/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
109109
// disable some modules on WASM
110110
#[cfg(not(target_arch = "wasm32"))]
111111
{
112-
modules.insert("_os".to_owned(), Box::new(os::make_module));
112+
modules.insert(os::MODULE_NAME.to_owned(), Box::new(os::make_module));
113113
modules.insert("_socket".to_owned(), Box::new(socket::make_module));
114114
modules.insert(
115115
"_multiprocessing".to_owned(),

‎vm/src/stdlib/os.rs‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ use crate::pyobject::{
4242
};
4343
use crate::vm::VirtualMachine;
4444

45+
#[cfg(windows)]
46+
pub const MODULE_NAME: &str = "nt";
47+
#[cfg(not(windows))]
48+
pub const MODULE_NAME: &str = "posix";
49+
4550
#[cfg(unix)]
4651
pub fn raw_file_number(handle: File) -> i64 {
4752
use std::os::unix::io::IntoRawFd;
@@ -504,7 +509,7 @@ type DirEntryRef = PyRef<DirEntry>;
504509

505510
impl PyValue for DirEntry {
506511
fn class(vm: &VirtualMachine) -> PyClassRef {
507-
vm.class("_os", "DirEntry")
512+
vm.class(MODULE_NAME, "DirEntry")
508513
}
509514
}
510515

@@ -587,7 +592,7 @@ struct ScandirIterator {
587592

588593
impl PyValue for ScandirIterator {
589594
fn class(vm: &VirtualMachine) -> PyClassRef {
590-
vm.class("_os", "ScandirIter")
595+
vm.class(MODULE_NAME, "ScandirIter")
591596
}
592597
}
593598

@@ -666,7 +671,7 @@ struct StatResult {
666671

667672
impl StatResult {
668673
fn into_obj(self, vm: &VirtualMachine) -> PyObjectRef {
669-
self.into_struct_sequence(vm, vm.class("_os", "stat_result"))
674+
self.into_struct_sequence(vm, vm.class(MODULE_NAME, "stat_result"))
670675
.unwrap()
671676
.into_object()
672677
}
@@ -1170,7 +1175,7 @@ struct UnameResult {
11701175
#[cfg(unix)]
11711176
impl UnameResult {
11721177
fn into_obj(self, vm: &VirtualMachine) -> PyObjectRef {
1173-
self.into_struct_sequence(vm, vm.class("_os", "uname_result"))
1178+
self.into_struct_sequence(vm, vm.class(MODULE_NAME, "uname_result"))
11741179
.unwrap()
11751180
.into_object()
11761181
}
@@ -1257,12 +1262,6 @@ fn os_lseek(fd: i32, position: Offset, how: i32, vm: &VirtualMachine) -> PyResul
12571262
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
12581263
let ctx = &vm.ctx;
12591264

1260-
let os_name = if cfg!(windows) {
1261-
"nt".to_owned()
1262-
} else {
1263-
"posix".to_owned()
1264-
};
1265-
12661265
let environ = _os_environ(vm);
12671266

12681267
let scandir_iter = ctx.new_class("ScandirIter", ctx.object());
@@ -1342,7 +1341,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
13421341
let supports_dir_fd = PySet::default().into_ref(vm);
13431342
let supports_follow_symlinks = PySet::default().into_ref(vm);
13441343

1345-
let module = py_module!(vm, "_os", {
1344+
let module = py_module!(vm, MODULE_NAME, {
13461345
"close" => ctx.new_function(os_close),
13471346
"error" => ctx.new_function(os_error),
13481347
"fsync" => ctx.new_function(os_fsync),
@@ -1352,15 +1351,14 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
13521351
"putenv" => ctx.new_function(os_putenv),
13531352
"unsetenv" => ctx.new_function(os_unsetenv),
13541353
"environ" => environ,
1355-
"name" => ctx.new_str(os_name),
13561354
"ScandirIter" => scandir_iter,
13571355
"DirEntry" => dir_entry,
13581356
"stat_result" => stat_result,
13591357
"lstat" => ctx.new_function(os_lstat),
13601358
"getcwd" => ctx.new_function(os_getcwd),
13611359
"chdir" => ctx.new_function(os_chdir),
13621360
"fspath" => ctx.new_function(os_fspath),
1363-
"getpid" => ctx.new_function(os_getpid),
1361+
"getpid" => ctx.new_function(os_getpid),
13641362
"cpu_count" => ctx.new_function(os_cpu_count),
13651363
"_exit" => ctx.new_function(os_exit),
13661364
"urandom" => ctx.new_function(os_urandom),

0 commit comments

Comments
 (0)