pub fn run_code(&mut self, code: &str) -> PyResult<PyObject> {
        let py = self.acquire_gil();

        let code = CString::new(code).or_else(|_| {
            Err(PyErr::new::<ValueError, _>(
                py,
                "source code is not a valid C string",
            ))
        })?;

        unsafe {
            let main = pyffi::PyImport_AddModule("__main__\0".as_ptr() as *const _);

            if main.is_null() {
                return Err(PyErr::fetch(py));
            }

            let main_dict = pyffi::PyModule_GetDict(main);

            let res = pyffi::PyRun_StringFlags(
                code.as_ptr() as *const _,
                pyffi::Py_file_input,
                main_dict,
                main_dict,
                std::ptr::null_mut(),
            );

            if res.is_null() {
                Err(PyErr::fetch(py))
            } else {
                Ok(PyObject::from_owned_ptr(py, res))
            }
        }
    }
/// Runs Python code provided by a string.
///
/// This is similar to what ``python -c <code>`` would do.
///
/// The interpreter is automatically initialized if needed.
Runs Python code provided by a string.
This is similar to what ``python -c `` would do.

The interpreter is automatically initialized if needed.
Runs Python code provided by a string . This is similar to what ` ` python - c ` ` would do . The interpreter is automatically initialized if needed .
pub fn run_code(&mut self, code: &str) -> PyResult<PyObject> {
        let py = self.acquire_gil();
        let code = CString::new(code).or_else(|_| {
            Err(PyErr::new::<ValueError, _>(
                py,
                "source code is not a valid C string",
            ))
        })?;
        unsafe {
            let main = pyffi::PyImport_AddModule("__main__\0".as_ptr() as *const _);
            if main.is_null() {
                return Err(PyErr::fetch(py));
            }
            let main_dict = pyffi::PyModule_GetDict(main);
            let res = pyffi::PyRun_StringFlags(
                code.as_ptr() as *const _,
                pyffi::Py_file_input,
                main_dict,
                main_dict,
                std::ptr::null_mut(),
            );
            if res.is_null() {
                Err(PyErr::fetch(py))
            } else {
                Ok(PyObject::from_owned_ptr(py, res))
            }
        }
    }
pub fn run_code ( & mut self , code : & str ) -> PyResult < PyObject > { let py = self . acquire_gil ( ) ; let code = CString :: new ( code ) . or_else ( | _ | { Err ( PyErr :: new :: < ValueError , _ > ( py , " " , ) ) } ) ? ; unsafe { let main = pyffi :: PyImport_AddModule ( " \0 " . as_ptr ( ) as * const _ ) ; if main . is_null ( ) { return Err ( PyErr :: fetch ( py ) ) ; } let main_dict = pyffi :: PyModule_GetDict ( main ) ; let res = pyffi :: PyRun_StringFlags ( code . as_ptr ( ) as * const _ , pyffi :: Py_file_input , main_dict , main_dict , std :: ptr :: null_mut ( ) , ) ; if res . is_null ( ) { Err ( PyErr :: fetch ( py ) ) } else { Ok ( PyObject :: from_owned_ptr ( py , res ) ) } } }
Runs Python code provided by a string.
Runs Python code provided by a string .
&mut self
code
&str