Skip to content

Commit d8c2fba

Browse files
committed
Update to PyO3 0.23.
1 parent 40804b4 commit d8c2fba

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ autoexamples = false
1414

1515
[dependencies]
1616
inline-python-macros = { version = "=0.12.0", path = "./macros" }
17-
pyo3 = { version = "0.22", default-features = false, features = ["auto-initialize"] }
17+
pyo3 = { version = "0.23", default-features = false, features = ["auto-initialize"] }
1818

1919
[workspace]
2020
members = ["examples", "ct-python"]

examples/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ path = "rust-fn.rs"
1717

1818
[dependencies]
1919
inline-python = { path = ".." }
20-
pyo3 = "0.22"
20+
pyo3 = "0.23"

macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ proc_macro = true
1616
[dependencies]
1717
proc-macro2 = { version = "1.0", features = ["span-locations"] }
1818
quote = "1.0"
19-
pyo3 = { version = "0.22", default-features = false, features = ["auto-initialize"] }
19+
pyo3 = { version = "0.23", default-features = false, features = ["auto-initialize"] }
2020

2121
[target.'cfg(unix)'.dependencies]
2222
libc = "0.2.71"

macros/src/error.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use proc_macro::Span;
22
use proc_macro2::TokenStream;
3-
use pyo3::{prelude::*, types::PyTraceback, Bound, PyErr, PyResult, PyTypeInfo, Python, ToPyObject};
3+
use pyo3::{prelude::*, types::PyTraceback, Bound, PyErr, PyResult, PyTypeInfo, Python, IntoPyObject};
44
use quote::{quote, quote_spanned};
55

66
/// Format a nice error message for a python compilation error.
77
pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> TokenStream {
8-
let value = error.to_object(py);
8+
let value = (&error).into_pyobject(py).unwrap();
99

10-
if value.is_none(py) {
11-
let error = format!("python: {}", error.get_type_bound(py).name().unwrap());
10+
if value.is_none() {
11+
let error = format!("python: {}", error.get_type(py).name().unwrap());
1212
return quote!(compile_error! {#error});
1313
}
1414

15-
if error.matches(py, pyo3::exceptions::PySyntaxError::type_object_bound(py)) {
16-
let line: Option<usize> = value.getattr(py, "lineno").ok().and_then(|x| x.extract(py).ok());
17-
let msg: Option<String> = value.getattr(py, "msg").ok().and_then(|x| x.extract(py).ok());
15+
if let Ok(true) = error.matches(py, pyo3::exceptions::PySyntaxError::type_object(py)) {
16+
let line: Option<usize> = value.getattr("lineno").ok().and_then(|x| x.extract().ok());
17+
let msg: Option<String> = value.getattr("msg").ok().and_then(|x| x.extract().ok());
1818
if let (Some(line), Some(msg)) = (line, msg) {
1919
if let Some(span) = span_for_line(tokens.clone(), line) {
2020
let error = format!("python: {}", msg);
@@ -23,10 +23,10 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
2323
}
2424
}
2525

26-
if let Some(tb) = &error.traceback_bound(py) {
26+
if let Some(tb) = &error.traceback(py) {
2727
if let Ok((file, line)) = get_traceback_info(tb) {
2828
if file == Span::call_site().source_file().path().to_string_lossy() {
29-
if let Ok(msg) = value.bind(py).str() {
29+
if let Ok(msg) = value.str() {
3030
if let Some(span) = span_for_line(tokens, line) {
3131
let error = format!("python: {}", msg);
3232
return quote_spanned!(span.into() => compile_error!{#error});
@@ -36,7 +36,7 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
3636
}
3737
}
3838

39-
let error = format!("python: {}", value.bind(py).str().unwrap());
39+
let error = format!("python: {}", value.str().unwrap());
4040
quote!(compile_error! {#error})
4141
}
4242

macros/src/run.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn ensure_libpython_symbols_loaded(py: Python) -> PyResult<()> {
1414
// This function tries to (re)load the right version of libpython, but this
1515
// time with RTLD_GLOBAL enabled.
1616

17-
let sysconfig = py.import_bound("sysconfig")?;
17+
let sysconfig = py.import("sysconfig")?;
1818
let libdir: String = sysconfig.getattr("get_config_var")?.call1(("LIBDIR",))?.extract()?;
1919
let so_name: String = sysconfig.getattr("get_config_var")?.call1(("INSTSONAME",))?.extract()?;
2020
let path = std::ffi::CString::new(format!("{}/{}", libdir, so_name)).unwrap();
@@ -28,10 +28,10 @@ fn run_and_capture(py: Python, code: PyObject) -> PyResult<String> {
2828
#[cfg(unix)]
2929
let _ = ensure_libpython_symbols_loaded(py);
3030

31-
let globals = py.import_bound("__main__")?.dict().copy()?;
31+
let globals = py.import("__main__")?.dict().copy()?;
3232

33-
let sys = py.import_bound("sys")?;
34-
let io = py.import_bound("io")?;
33+
let sys = py.import("sys")?;
34+
let io = py.import("io")?;
3535

3636
let stdout = io.getattr("StringIO")?.call0()?;
3737
let original_stdout = sys.dict().get_item("stdout")?;

src/context.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::PythonBlock;
33
use pyo3::{
44
prelude::*,
55
types::{PyCFunction, PyDict},
6-
FromPyObject, Py, PyResult, Python, ToPyObject,
6+
FromPyObject, Py, PyResult, Python, IntoPyObject,
77
};
88

99
/// An execution context for Python code.
@@ -71,12 +71,12 @@ impl Context {
7171

7272
fn try_new(py: Python) -> PyResult<Self> {
7373
Ok(Self {
74-
globals: py.import_bound("__main__")?.dict().copy()?.into(),
74+
globals: py.import("__main__")?.dict().copy()?.into(),
7575
})
7676
}
7777

7878
/// Get the globals as dictionary.
79-
pub fn globals<'p>(&'p self, py: Python<'p>) -> &'p Bound<'p, PyDict> {
79+
pub fn globals<'p>(&self, py: Python<'p>) -> &Bound<'p, PyDict> {
8080
self.globals.bind(py)
8181
}
8282

@@ -112,14 +112,14 @@ impl Context {
112112
/// If you already have the GIL, you can use [`Context::set_with_gil`] instead.
113113
///
114114
/// This function panics if the conversion fails.
115-
pub fn set<T: ToPyObject>(&self, name: &str, value: T) {
115+
pub fn set<T: for<'p> IntoPyObject<'p>>(&self, name: &str, value: T) {
116116
Python::with_gil(|py| self.set_with_gil(py, name, value));
117117
}
118118

119119
/// Set a global variable in the context.
120120
///
121121
/// This function panics if the conversion fails.
122-
pub fn set_with_gil<'p, T: ToPyObject>(&self, py: Python<'p>, name: &str, value: T) {
122+
pub fn set_with_gil<'p, T: IntoPyObject<'p>>(&self, py: Python<'p>, name: &str, value: T) {
123123
match self.globals(py).set_item(name, value) {
124124
Ok(()) => (),
125125
Err(e) => {
@@ -165,7 +165,7 @@ impl Context {
165165
pub fn add_wrapped_with_gil<'p>(&self, py: Python<'p>, wrapper: &impl Fn(Python) -> PyResult<Bound<'_, PyCFunction>>) {
166166
let obj = wrapper(py).unwrap();
167167
let name = obj.getattr("__name__").expect("Missing __name__");
168-
self.set_with_gil(py, name.extract().unwrap(), obj)
168+
self.set_with_gil(py, name.extract().unwrap(), obj);
169169
}
170170

171171
/// Run Python code using this context.

0 commit comments

Comments
 (0)