From fa7524d0b9022219fd95bbe991801548a5084867 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Mon, 24 Feb 2020 22:11:08 +0000 Subject: [PATCH 1/2] Change the name of the _os module to posix/nt --- Lib/importlib/_bootstrap_external.py | 29 ++++++++-------- Lib/os.py | 49 ++++++++++++++++++++++------ Lib/shutil.py | 4 +-- vm/src/stdlib/mod.rs | 2 +- vm/src/stdlib/os.rs | 24 +++++++------- 5 files changed, 67 insertions(+), 41 deletions(-) diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index daf3b58e1c..d60cb7691a 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -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)) diff --git a/Lib/os.py b/Lib/os.py index 449381eb06..a9bba366cb 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -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, diff --git a/Lib/shutil.py b/Lib/shutil.py index b938b56b34..3c02776a40 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -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') diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs index c709de3e0c..ea305c19b4 100644 --- a/vm/src/stdlib/mod.rs +++ b/vm/src/stdlib/mod.rs @@ -109,7 +109,7 @@ pub fn get_module_inits() -> HashMap { // 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(), diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index d6ec328bab..78e3097ea5 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -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; 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), From 959b0b9e629a11d657251353f005aba2c57d1284 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Mon, 24 Feb 2020 22:31:49 +0000 Subject: [PATCH 2/2] Don't try to import winreg in importlib._bootstrap_external --- Lib/importlib/_bootstrap_external.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index d60cb7691a..18eb43f2a8 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1605,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)