`py_run!` creates unexpected scope issues
Open
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
Bug Description
Try the following code (originally discussed in #2889).
#[pyfunction]
fn report_unraisable(py: Python) {
use pyo3::exceptions::PyRuntimeError;
let err = PyRuntimeError::new_err("foo");
err.write_unraisable(py, None);
let err = PyRuntimeError::new_err("bar");
err.write_unraisable(py, Some(py.NotImplemented().as_ref(py)));
}
#[test]
fn test_write_unraisable() {
Python::with_gil(|py| {
let report_unraisable = wrap_pyfunction!(report_unraisable, py).unwrap();
let locals = PyDict::new(py);
locals
.set_item("report_unraisable", report_unraisable)
.unwrap();
py_run!(
py,
report_unraisable,
r#"
import sys
captured = []
def report(data):
captured.append(list(data))
original_hook = sys.unraisablehook
try:
sys.unraisablehook = report
report_unraisable()
assert len(captured) == 2
assert captured[0][0] is RuntimeError
assert str(captured[0][1]) == 'foo'
assert captured[0][4] is None
assert captured[1][0] is RuntimeError
assert str(captured[1][1]) == 'bar'
assert captured[1][4] is NotImplemented
finally:
sys.unraisablehook = original_hook
"#
);
});
}
The report function defined within the py_run! source will print errors like the following, and the test will fail:
Exception ignored in sys.unraisablehook: <function report at 0x102e94040>
Traceback (most recent call last):
File "<string>", line 5, in report
NameError: name 'captured' is not defined
Exception ignored in sys.unraisablehook: <function report at 0x102e94040>
Traceback (most recent call last):
File "<string>", line 5, in report
NameError: name 'captured' is not defined
Traceback (most recent call last):
File "<string>", line 12, in <module>
AssertionError
Steps to Reproduce
As above
Backtrace
No response
Your operating system and version
macOS Ventura 13.1
Your Python version (python --version)
Python 3.10.9
Your Rust version (rustc --version)
rustc 1.67.0-beta.2
Your PyO3 version
0.18.0
How did you install python? Did you use a virtualenv?
brew + vent
Additional Info
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the provided test around the py_run! macro, especially the locally defined report function and captured list. Trace how the macro supplies Python globals and locals, then make the test pass while preserving access to captured; the assertions should report both unraisable errors correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100