Merge pull request #1774 from RustPython/coolreader18/_os-module-posix-nt

Change the name of the _os module to posix/nt
This commit is contained in:
Noah
2020-02-28 07:06:05 -06:00
committed by GitHub
5 changed files with 69 additions and 42 deletions

View File

@@ -1575,21 +1575,22 @@ def _setup(_bootstrap_module):
setattr(self_module, builtin_name, builtin_module)
# Directly load the os module (needed during bootstrap).
# XXX Changed to fit RustPython!!!
builtin_os = "_os"
if builtin_os in sys.modules:
os_module = sys.modules[builtin_os]
os_details = ('posix', ['/']), ('nt', ['\\', '/'])
for builtin_os, path_separators in os_details:
# Assumption made in _path_join()
assert all(len(sep) == 1 for sep in path_separators)
path_sep = path_separators[0]
if builtin_os in sys.modules:
os_module = sys.modules[builtin_os]
break
else:
try:
os_module = _bootstrap._builtin_from_name(builtin_os)
break
except ImportError:
continue
else:
try:
os_module = _bootstrap._builtin_from_name(builtin_os)
except ImportError:
raise ImportError('importlib requires _os')
path_separators = ['\\', '/'] if os_module.name == 'nt' else ['/']
# Assumption made in _path_join()
assert all(len(sep) == 1 for sep in path_separators)
path_sep = path_separators[0]
raise ImportError('importlib requires posix or nt')
setattr(self_module, '_os', os_module)
setattr(self_module, 'path_sep', path_sep)
setattr(self_module, 'path_separators', ''.join(path_separators))
@@ -1604,7 +1605,8 @@ def _setup(_bootstrap_module):
setattr(self_module, '_weakref', weakref_module)
# Directly load the winreg module (needed during bootstrap).
if builtin_os == 'nt':
# XXX RustPython TODO: winreg module
if builtin_os == 'nt' and False:
winreg_module = _bootstrap._builtin_from_name('winreg')
setattr(self_module, '_winreg', winreg_module)

49
Lib/os.py vendored
View File

@@ -43,21 +43,50 @@ def _get_exports_list(module):
except AttributeError:
return [n for n in dir(module) if n[0] != '_']
import _os
from _os import *
from _os import _exit
__all__.extend(_get_exports_list(_os))
del _os
# Any new dependencies of the os module and/or changes in path separator
# requires updating importlib as well.
if name == 'nt':
linesep = '\r\n'
import ntpath as path
else:
if 'posix' in _names:
name = 'posix'
linesep = '\n'
from posix import *
try:
from posix import _exit
__all__.append('_exit')
except ImportError:
pass
import posixpath as path
try:
from posix import _have_functions
except ImportError:
pass
import posix
__all__.extend(_get_exports_list(posix))
del posix
elif 'nt' in _names:
name = 'nt'
linesep = '\r\n'
from nt import *
try:
from nt import _exit
__all__.append('_exit')
except ImportError:
pass
import ntpath as path
import nt
__all__.extend(_get_exports_list(nt))
del nt
try:
from nt import _have_functions
except ImportError:
pass
else:
raise ImportError('no os specific module found')
sys.modules['os.path'] = path
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,

4
Lib/shutil.py vendored
View File

@@ -1015,9 +1015,7 @@ if hasattr(os, 'statvfs'):
elif os.name == 'nt':
# XXX RustPython TODO: figure out what to do with posix vs nt vs os
# import nt
import os as nt
import nt
__all__.append('disk_usage')
_ntuple_diskusage = collections.namedtuple('usage', 'total used free')

View File

@@ -109,7 +109,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
// disable some modules on WASM
#[cfg(not(target_arch = "wasm32"))]
{
modules.insert("_os".to_owned(), Box::new(os::make_module));
modules.insert(os::MODULE_NAME.to_owned(), Box::new(os::make_module));
modules.insert("_socket".to_owned(), Box::new(socket::make_module));
modules.insert(
"_multiprocessing".to_owned(),

View File

@@ -42,6 +42,11 @@ use crate::pyobject::{
};
use crate::vm::VirtualMachine;
#[cfg(windows)]
pub const MODULE_NAME: &str = "nt";
#[cfg(not(windows))]
pub const MODULE_NAME: &str = "posix";
#[cfg(unix)]
pub fn raw_file_number(handle: File) -> i64 {
use std::os::unix::io::IntoRawFd;
@@ -504,7 +509,7 @@ type DirEntryRef = PyRef<DirEntry>;
impl PyValue for DirEntry {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.class("_os", "DirEntry")
vm.class(MODULE_NAME, "DirEntry")
}
}
@@ -587,7 +592,7 @@ struct ScandirIterator {
impl PyValue for ScandirIterator {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.class("_os", "ScandirIter")
vm.class(MODULE_NAME, "ScandirIter")
}
}
@@ -666,7 +671,7 @@ struct StatResult {
impl StatResult {
fn into_obj(self, vm: &VirtualMachine) -> PyObjectRef {
self.into_struct_sequence(vm, vm.class("_os", "stat_result"))
self.into_struct_sequence(vm, vm.class(MODULE_NAME, "stat_result"))
.unwrap()
.into_object()
}
@@ -1170,7 +1175,7 @@ struct UnameResult {
#[cfg(unix)]
impl UnameResult {
fn into_obj(self, vm: &VirtualMachine) -> PyObjectRef {
self.into_struct_sequence(vm, vm.class("_os", "uname_result"))
self.into_struct_sequence(vm, vm.class(MODULE_NAME, "uname_result"))
.unwrap()
.into_object()
}
@@ -1257,12 +1262,6 @@ fn os_lseek(fd: i32, position: Offset, how: i32, vm: &VirtualMachine) -> PyResul
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
let os_name = if cfg!(windows) {
"nt".to_owned()
} else {
"posix".to_owned()
};
let environ = _os_environ(vm);
let scandir_iter = ctx.new_class("ScandirIter", ctx.object());
@@ -1342,7 +1341,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let supports_dir_fd = PySet::default().into_ref(vm);
let supports_follow_symlinks = PySet::default().into_ref(vm);
let module = py_module!(vm, "_os", {
let module = py_module!(vm, MODULE_NAME, {
"close" => ctx.new_function(os_close),
"error" => ctx.new_function(os_error),
"fsync" => ctx.new_function(os_fsync),
@@ -1352,7 +1351,6 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"putenv" => ctx.new_function(os_putenv),
"unsetenv" => ctx.new_function(os_unsetenv),
"environ" => environ,
"name" => ctx.new_str(os_name),
"ScandirIter" => scandir_iter,
"DirEntry" => dir_entry,
"stat_result" => stat_result,
@@ -1360,7 +1358,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"getcwd" => ctx.new_function(os_getcwd),
"chdir" => ctx.new_function(os_chdir),
"fspath" => ctx.new_function(os_fspath),
"getpid" => ctx.new_function(os_getpid),
"getpid" => ctx.new_function(os_getpid),
"cpu_count" => ctx.new_function(os_cpu_count),
"_exit" => ctx.new_function(os_exit),
"urandom" => ctx.new_function(os_urandom),