##// 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 use libc::{c_char, c_int};
14 14
15 15 use std::env;
16 16 use std::path::PathBuf;
17 use std::ffi::CString;
17 use std::ffi::{CString, OsStr};
18 18 #[cfg(target_family = "unix")]
19 19 use std::os::unix::ffi::OsStringExt;
20 20
@@ -62,6 +62,10 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 69 // On UNIX, argv starts as an array of char*. So it is easy to convert
66 70 // to C strings.
67 71 #[cfg(target_family = "unix")]
@@ -86,9 +90,7 fn args_to_cstrings() -> Vec<CString> {
86 90 }
87 91
88 92 fn set_python_home(env: &Environment) {
89 let raw = CString::new(env.python_home.to_str().unwrap())
90 .unwrap()
91 .into_raw();
93 let raw = cstring_from_os(&env.python_home).into_raw();
92 94 unsafe {
93 95 python27_sys::Py_SetPythonHome(raw);
94 96 }
@@ -133,9 +135,7 fn run() -> Result<(), i32> {
133 135 // Python files. Apparently we could define our own ``Py_GetPath()``
134 136 // implementation. But this may require statically linking Python, which is
135 137 // not desirable.
136 let program_name = CString::new(env.python_exe.to_str().unwrap())
137 .unwrap()
138 .as_ptr();
138 let program_name = cstring_from_os(&env.python_exe).as_ptr();
139 139 unsafe {
140 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