##// END OF EJS Templates
rust: extract function to convert Path to platform CString...
Yuya Nishihara -
r35649:edbe11cf default
parent child Browse files
Show More
@@ -14,7 +14,7 b' use libc::{c_char, c_int};'
14
14
15 use std::env;
15 use std::env;
16 use std::path::PathBuf;
16 use std::path::PathBuf;
17 use std::ffi::CString;
17 use std::ffi::{CString, OsStr};
18 #[cfg(target_family = "unix")]
18 #[cfg(target_family = "unix")]
19 use std::os::unix::ffi::OsStringExt;
19 use std::os::unix::ffi::OsStringExt;
20
20
@@ -62,6 +62,10 b' fn get_environment() -> Environment {'
62 }
62 }
63 }
63 }
64
64
65 fn cstring_from_os<T: AsRef<OsStr>>(s: T) -> CString {
66 CString::new(s.as_ref().to_str().unwrap()).unwrap()
67 }
68
65 // On UNIX, argv starts as an array of char*. So it is easy to convert
69 // On UNIX, argv starts as an array of char*. So it is easy to convert
66 // to C strings.
70 // to C strings.
67 #[cfg(target_family = "unix")]
71 #[cfg(target_family = "unix")]
@@ -86,9 +90,7 b' fn args_to_cstrings() -> Vec<CString> {'
86 }
90 }
87
91
88 fn set_python_home(env: &Environment) {
92 fn set_python_home(env: &Environment) {
89 let raw = CString::new(env.python_home.to_str().unwrap())
93 let raw = cstring_from_os(&env.python_home).into_raw();
90 .unwrap()
91 .into_raw();
92 unsafe {
94 unsafe {
93 python27_sys::Py_SetPythonHome(raw);
95 python27_sys::Py_SetPythonHome(raw);
94 }
96 }
@@ -133,9 +135,7 b' fn run() -> Result<(), i32> {'
133 // Python files. Apparently we could define our own ``Py_GetPath()``
135 // Python files. Apparently we could define our own ``Py_GetPath()``
134 // implementation. But this may require statically linking Python, which is
136 // implementation. But this may require statically linking Python, which is
135 // not desirable.
137 // not desirable.
136 let program_name = CString::new(env.python_exe.to_str().unwrap())
138 let program_name = cstring_from_os(&env.python_exe).as_ptr();
137 .unwrap()
138 .as_ptr();
139 unsafe {
139 unsafe {
140 python27_sys::Py_SetProgramName(program_name as *mut i8);
140 python27_sys::Py_SetProgramName(program_name as *mut i8);
141 }
141 }
General Comments 0
You need to be logged in to leave comments. Login now