Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add __builtins__ to globals in py.run() if they're missing #3378

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3378.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `__builtins__` to globals in `py.run()` and `py.eval()` if they're missing.
42 changes: 41 additions & 1 deletion src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ impl<'py> Python<'py> {
/// If `globals` is `None`, it defaults to Python module `__main__`.
/// If `locals` is `None`, it defaults to the value of `globals`.
///
/// If `globals` doesn't contain `__builtins__`, default `__builtins__`
/// will be added automatically.
///
/// # Examples
///
/// ```
Expand All @@ -568,6 +571,9 @@ impl<'py> Python<'py> {
/// If `globals` is `None`, it defaults to Python module `__main__`.
/// If `locals` is `None`, it defaults to the value of `globals`.
///
/// If `globals` doesn't contain `__builtins__`, default `__builtins__`
/// will be added automatically.
///
/// # Examples
/// ```
/// use pyo3::{
Expand Down Expand Up @@ -632,6 +638,29 @@ impl<'py> Python<'py> {
.unwrap_or_else(|| ffi::PyModule_GetDict(mptr));
let locals = locals.map(AsPyPointer::as_ptr).unwrap_or(globals);

// If `globals` don't provide `__builtins__`, most of the code will fail if Python
// version is <3.10. That's probably not what user intended, so insert `__builtins__`
// for them.
//
// See also:
// - https://github.com/python/cpython/pull/24564 (the same fix in CPython 3.10)
// - https://github.com/PyO3/pyo3/issues/3370
let builtins_s = crate::intern!(self, "__builtins__").as_ptr();
let has_builtins = ffi::PyDict_Contains(globals, builtins_s);
if has_builtins == -1 {
return Err(PyErr::fetch(self));
}
if has_builtins == 0 {
// Inherit current builtins.
let builtins = ffi::PyEval_GetBuiltins();

// `PyDict_SetItem` doesn't take ownership of `builtins`, but `PyEval_GetBuiltins`
// seems to return a borrowed reference, so no leak here.
Comment on lines +657 to +658
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if ffi::PyDict_SetItem(globals, builtins_s, builtins) == -1 {
return Err(PyErr::fetch(self));
}
}

let code_obj = ffi::Py_CompileString(code.as_ptr(), "<string>\0".as_ptr() as _, start);
if code_obj.is_null() {
return Err(PyErr::fetch(self));
Expand Down Expand Up @@ -1031,7 +1060,7 @@ impl<'unbound> Python<'unbound> {
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{IntoPyDict, PyList};
use crate::types::{IntoPyDict, PyDict, PyList};
use crate::Py;
use std::sync::Arc;

Expand Down Expand Up @@ -1166,4 +1195,15 @@ mod tests {
assert!(v.eq(py.Ellipsis()).unwrap());
});
}

#[test]
fn test_py_run_inserts_globals() {
Python::with_gil(|py| {
let namespace = PyDict::new(py);
py.run("class Foo: pass", Some(namespace), Some(namespace))
.unwrap();
assert!(namespace.get_item("Foo").is_some());
assert!(namespace.get_item("__builtins__").is_some());
})
}
}