Skip to content

Python 3.{8,9,10}: functions are not saved when globals are not provided to py.run() #3371

@GoldsteinE

Description

@GoldsteinE

Bug Description

Passing None as a globals argument to py.run() leads to newly defined functions seemingly not being saved anywhere at least under Python 3.8, 3.9 and 3.10.

Steps to Reproduce

Run the following code:

fn main() -> eyre::Result<()> {
    let script_text = r#"
def trivial(s):
    return s
class Foo:
    def __init__(self):
        print(trivial('Hello, world!'))
    "#;

    pyo3::Python::with_gil(|py| {
        let module = pyo3::types::PyDict::new(py);
        py.run(script_text, None, Some(module))?;
        let foo_class = module.get_item("Foo").unwrap();
        let _foo = foo_class.call0()?;
        Ok(())
    })
}

Observe the following error:

NameError: name 'trivial' is not defined

Your operating system and version

NixOS

Your Python version (python --version)

Python 3.8.17, Python 3.9.17, Python 3.10.12

Your Rust version (rustc --version)

rustc 1.73.0-nightly (8131b9774 2023-08-02)

Your PyO3 version

0.19.2

How did you install python? Did you use a virtualenv?

Nix, no virtualenv

Additional Info

Possibly related to #3370, #2891

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions