Skip to content

Commit 28460db

Browse files
authored
Merge pull request #50 from Jules-Bertholet/master
Upgrade to PyO3 0.17
2 parents 239311b + 36f5d94 commit 28460db

File tree

8 files changed

+39
-41
lines changed

8 files changed

+39
-41
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "inline-python"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
authors = [
55
"Mara Bos <[email protected]>",
66
"Maarten de Vries <[email protected]>",
@@ -13,8 +13,8 @@ keywords = ["python", "inline", "embed", "macro"]
1313
autoexamples = false
1414

1515
[dependencies]
16-
inline-python-macros = { version = "=0.8.0", path = "./macros" }
17-
pyo3 = { version = "0.15.0", default-features = false, features = ["auto-initialize"] }
16+
inline-python-macros = { version = "=0.9.0", path = "./macros" }
17+
pyo3 = { version = "0.17.0", default-features = false, features = ["auto-initialize"] }
1818

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

ct-python/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ repository = "https://github.com/fusion-engineering/inline-python/tree/master/ct
99
keywords = ["python", "macro", "generate", "compile-time"]
1010

1111
[dependencies]
12-
inline-python-macros = { version = "=0.8.0", path = "../macros" }
12+
inline-python-macros = { version = "=0.9.0", path = "../macros" }

examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
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.15.0"
20+
pyo3 = "0.17.0"

macros/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "inline-python-macros"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
authors = [
55
"Mara Bos <[email protected]>",
66
"Maarten de Vries <[email protected]>",
@@ -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.15.0", default-features = false, features = ["auto-initialize"] }
19+
pyo3 = { version = "0.17.0", default-features = false, features = ["auto-initialize"] }
2020

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

macros/src/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use proc_macro::Span;
22
use proc_macro2::TokenStream;
3-
use pyo3::type_object::PyTypeObject;
4-
use pyo3::{PyAny, PyErr, PyResult, Python, ToPyObject};
3+
use pyo3::{PyAny, PyErr, PyResult, PyTypeInfo, Python, ToPyObject};
54
use quote::{quote, quote_spanned};
65

76
/// Format a nice error message for a python compilation error.
87
pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> TokenStream {
98
let value = error.to_object(py);
109

1110
if value.is_none(py) {
12-
let error = format!("python: {}", error.ptype(py).name().unwrap());
11+
let error = format!("python: {}", error.get_type(py).name().unwrap());
1312
return quote!(compile_error! {#error});
1413
}
1514

@@ -24,7 +23,7 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
2423
}
2524
}
2625

27-
if let Some(tb) = &error.ptraceback(py) {
26+
if let Some(tb) = &error.traceback(py) {
2827
if let Ok((file, line)) = get_traceback_info(tb) {
2928
if file == Span::call_site().source_file().path().to_string_lossy() {
3029
if let Ok(msg) = value.as_ref(py).str() {

macros/src/lib.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ fn python_impl(input: TokenStream) -> Result<TokenStream, TokenStream> {
3232
let filename = CString::new(filename).unwrap();
3333

3434
let bytecode = unsafe {
35-
let gil = Python::acquire_gil();
36-
let py = gil.python();
37-
38-
let code = PyObject::from_owned_ptr_or_err(py, ffi::Py_CompileString(python.as_ptr(), filename.as_ptr(), ffi::Py_file_input))
39-
.map_err(|err| error::compile_error_msg(py, err, tokens))?;
40-
41-
Literal::byte_string(
42-
PyBytes::from_owned_ptr_or_err(py, ffi::PyMarshal_WriteObjectToString(code.as_ptr(), pyo3::marshal::VERSION))
43-
.map_err(|_e| quote!(compile_error! {"failed to generate python bytecode"}))?
44-
.as_bytes(),
45-
)
35+
let result: Result<Literal, TokenStream> = Python::with_gil(|py| {
36+
let code = PyObject::from_owned_ptr_or_err(py, ffi::Py_CompileString(python.as_ptr(), filename.as_ptr(), ffi::Py_file_input))
37+
.map_err(|err| error::compile_error_msg(py, err, tokens))?;
38+
39+
Ok(Literal::byte_string(
40+
PyBytes::from_owned_ptr_or_err(py, ffi::PyMarshal_WriteObjectToString(code.as_ptr(), pyo3::marshal::VERSION))
41+
.map_err(|_e| quote!(compile_error! {"failed to generate python bytecode"}))?
42+
.as_bytes(),
43+
))
44+
});
45+
result?
4646
};
4747

4848
let varname = variables.keys();
@@ -78,15 +78,14 @@ fn ct_python_impl(input: TokenStream) -> Result<TokenStream, TokenStream> {
7878
let python = CString::new(python).unwrap();
7979
let filename = CString::new(filename).unwrap();
8080

81-
let gil = Python::acquire_gil();
82-
let py = gil.python();
81+
Python::with_gil(|py| {
82+
let code = unsafe {
83+
PyObject::from_owned_ptr_or_err(py, ffi::Py_CompileString(python.as_ptr(), filename.as_ptr(), ffi::Py_file_input))
84+
.map_err(|err| error::compile_error_msg(py, err, tokens.clone()))?
85+
};
8386

84-
let code = unsafe {
85-
PyObject::from_owned_ptr_or_err(py, ffi::Py_CompileString(python.as_ptr(), filename.as_ptr(), ffi::Py_file_input))
86-
.map_err(|err| error::compile_error_msg(py, err, tokens.clone()))?
87-
};
88-
89-
run::run_ct_python(py, code, tokens)
87+
run::run_ct_python(py, code, tokens)
88+
})
9089
}
9190

9291
fn check_no_attribute(input: TokenStream) -> Result<(), TokenStream> {

src/context.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Context {
5050
/// This function panics if it fails to create the context.
5151
#[allow(clippy::new_without_default)]
5252
pub fn new() -> Self {
53-
Self::new_with_gil(Python::acquire_gil().python())
53+
Python::with_gil(Self::new_with_gil)
5454
}
5555

5656
/// Create a new context for running Python code.
@@ -86,7 +86,7 @@ impl Context {
8686
///
8787
/// This function panics if the variable doesn't exist, or the conversion fails.
8888
pub fn get<T: for<'p> FromPyObject<'p>>(&self, name: &str) -> T {
89-
self.get_with_gil(Python::acquire_gil().python(), name)
89+
Python::with_gil(|py| self.get_with_gil(py, name))
9090
}
9191

9292
/// Retrieve a global variable from the context.
@@ -112,7 +112,7 @@ impl Context {
112112
///
113113
/// This function panics if the conversion fails.
114114
pub fn set<T: ToPyObject>(&self, name: &str, value: T) {
115-
self.set_with_gil(Python::acquire_gil().python(), name, value)
115+
Python::with_gil(|py| self.set_with_gil(py, name, value));
116116
}
117117

118118
/// Set a global variable in the context.
@@ -155,7 +155,7 @@ impl Context {
155155
/// This function temporarily acquires the GIL.
156156
/// If you already have the GIL, you can use [`Context::add_wrapped_with_gil`] instead.
157157
pub fn add_wrapped(&self, wrapper: &impl Fn(Python) -> PyResult<&PyCFunction>) {
158-
self.add_wrapped_with_gil(Python::acquire_gil().python(), wrapper);
158+
Python::with_gil(|py| self.add_wrapped_with_gil(py, wrapper));
159159
}
160160

161161
/// Add a wrapped `#[pyfunction]` or `#[pymodule]` using its own `__name__`.
@@ -185,7 +185,7 @@ impl Context {
185185
///
186186
/// This function panics if the Python code fails.
187187
pub fn run<F: FnOnce(&PyDict)>(&self, code: PythonBlock<F>) {
188-
self.run_with_gil(Python::acquire_gil().python(), code);
188+
Python::with_gil(|py| self.run_with_gil(py, code));
189189
}
190190

191191
/// Run Python code using this context.

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//!
2121
//! _NOTE:_ Rust **nightly** toolchain is required. Feature `proc_macro_span` is still unstable,
2222
//! for more details check out [issue #54725](https://github.com/rust-lang/rust/issues/54725) - Tracking issue for `proc_macro::Span` inspection APIs
23-
//!
23+
//!
2424
//! ## Using Rust variables
2525
//!
2626
//! To reference Rust variables, use `'var`, as shown in the example above.
@@ -154,11 +154,11 @@ impl<F: FnOnce(&PyDict)> FromInlinePython<F> for () {
154154
/// Assigning a `python!{}` block to a `Context` will run the Python code and capture the resulting context.
155155
impl<F: FnOnce(&PyDict)> FromInlinePython<F> for Context {
156156
fn from_python_macro(bytecode: &'static [u8], set_variables: F) -> Self {
157-
let gil_guard = Python::acquire_gil();
158-
let py = gil_guard.python();
159-
let context = Context::new_with_gil(py);
160-
context.run_with_gil(py, PythonBlock { bytecode, set_variables });
161-
context
157+
Python::with_gil(|py| {
158+
let context = Context::new_with_gil(py);
159+
context.run_with_gil(py, PythonBlock { bytecode, set_variables });
160+
context
161+
})
162162
}
163163
}
164164

0 commit comments

Comments
 (0)