mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Have a default sys.path value pointing to <checkout>/Lib
This commit is contained in:
1 file changed
+24
-32
+24
-32
@@ -186,9 +186,19 @@ fn create_settings(matches: &ArgMatches) -> PySettings {
|
|||||||
// add the current directory to sys.path
|
// add the current directory to sys.path
|
||||||
settings.path_list.push("".to_owned());
|
settings.path_list.push("".to_owned());
|
||||||
|
|
||||||
|
if let Some(paths) = option_env!("BUILDTIME_RUSTPYTHONPATH") {
|
||||||
|
settings.path_list.extend(
|
||||||
|
std::env::split_paths(paths).map(|path| path.into_os_string().into_string().unwrap()),
|
||||||
|
)
|
||||||
|
} else if option_env!("RUSTPYTHONPATH").is_none() {
|
||||||
|
settings
|
||||||
|
.path_list
|
||||||
|
.push(concat!(env!("CARGO_MANIFEST_DIR"), "/Lib").to_owned());
|
||||||
|
}
|
||||||
|
|
||||||
if !ignore_environment {
|
if !ignore_environment {
|
||||||
settings.path_list.append(&mut get_paths("RUSTPYTHONPATH"));
|
settings.path_list.extend(get_paths("RUSTPYTHONPATH"));
|
||||||
settings.path_list.append(&mut get_paths("PYTHONPATH"));
|
settings.path_list.extend(get_paths("PYTHONPATH"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now process command line flags:
|
// Now process command line flags:
|
||||||
@@ -268,18 +278,18 @@ fn get_env_var_value(name: &str) -> Result<u8, std::env::VarError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function to retrieve a sequence of paths from an environment variable.
|
/// Helper function to retrieve a sequence of paths from an environment variable.
|
||||||
fn get_paths(env_variable_name: &str) -> Vec<String> {
|
fn get_paths(env_variable_name: &str) -> impl Iterator<Item = String> + '_ {
|
||||||
let paths = env::var_os(env_variable_name);
|
env::var_os(env_variable_name)
|
||||||
match paths {
|
.into_iter()
|
||||||
Some(paths) => env::split_paths(&paths)
|
.flat_map(move |paths| {
|
||||||
.map(|path| {
|
env::split_paths(&paths)
|
||||||
path.into_os_string()
|
.map(|path| {
|
||||||
.into_string()
|
path.into_os_string()
|
||||||
.unwrap_or_else(|_| panic!("{} isn't valid unicode", env_variable_name))
|
.into_string()
|
||||||
})
|
.unwrap_or_else(|_| panic!("{} isn't valid unicode", env_variable_name))
|
||||||
.collect(),
|
})
|
||||||
None => vec![],
|
.collect::<Vec<_>>()
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "flame-it")]
|
#[cfg(feature = "flame-it")]
|
||||||
@@ -329,24 +339,6 @@ fn write_profile(matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
|
fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
|
||||||
if let Some(paths) = option_env!("BUILDTIME_RUSTPYTHONPATH") {
|
|
||||||
let sys_path = vm.get_attribute(vm.sys_module.clone(), "path")?;
|
|
||||||
for (i, path) in std::env::split_paths(paths).enumerate() {
|
|
||||||
vm.call_method(
|
|
||||||
&sys_path,
|
|
||||||
"insert",
|
|
||||||
vec![
|
|
||||||
vm.ctx.new_int(i),
|
|
||||||
vm.ctx.new_str(
|
|
||||||
path.into_os_string()
|
|
||||||
.into_string()
|
|
||||||
.expect("Invalid UTF8 in BUILDTIME_RUSTPYTHONPATH"),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let scope = vm.new_scope_with_builtins();
|
let scope = vm.new_scope_with_builtins();
|
||||||
let main_module = vm.new_module("__main__", scope.globals.clone());
|
let main_module = vm.new_module("__main__", scope.globals.clone());
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user