Optimize Python.run_code

This commit is contained in:
messense 2017-05-25 21:10:18 +08:00
parent 9abc69f5c4
commit 8f62197f07
No known key found for this signature in database
GPG key ID: BB41A8A2C716CCA9

View file

@ -276,17 +276,9 @@ impl<'p> Python<'p> {
return Err(PyErr::fetch(self));
}
let mdict = ffi::PyModule_GetDict(mptr);
let globals = match globals {
Some(g) => g.as_ptr(),
None => mdict,
};
let locals = match locals {
Some(l) => l.as_ptr(),
None => globals
};
let globals = globals.map(|g| g.as_ptr())
.unwrap_or_else(|| ffi::PyModule_GetDict(mptr));
let locals = locals.map(|l| l.as_ptr()).unwrap_or(globals);
let res_ptr = ffi::PyRun_StringFlags(code.as_ptr(),
start, globals, locals, 0 as *mut _);
@ -356,6 +348,10 @@ mod test {
let d = PyDict::new(py);
d.set_item(py, "foo", 13).unwrap();
// Inject our own global namespace
let v: i32 = py.eval("foo + 29", Some(&d), None).unwrap().extract(py).unwrap();
assert_eq!(v, 42);
// Inject our own local namespace
let v: i32 = py.eval("foo + 29", None, Some(&d)).unwrap().extract(py).unwrap();
assert_eq!(v, 42);