Skip to content

Commit 2a1595b

Browse files
committed
Prepare for pyo3 0.26
1 parent 574ea0c commit 2a1595b

24 files changed

+252
-243
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use pyo3::prelude::*;
6262

6363
#[pyo3_async_runtimes::async_std::main]
6464
async fn main() -> PyResult<()> {
65-
let fut = Python::with_gil(|py| {
65+
let fut = Python::attach(|py| {
6666
let asyncio = py.import("asyncio")?;
6767
// convert asyncio.sleep into a Rust Future
6868
pyo3_async_runtimes::async_std::into_future(asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?)
@@ -92,7 +92,7 @@ use pyo3::prelude::*;
9292

9393
#[pyo3_async_runtimes::tokio::main]
9494
async fn main() -> PyResult<()> {
95-
let fut = Python::with_gil(|py| {
95+
let fut = Python::attach(|py| {
9696
let asyncio = py.import("asyncio")?;
9797
// convert asyncio.sleep into a Rust Future
9898
pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?)
@@ -234,7 +234,7 @@ use pyo3::prelude::*;
234234

235235
#[pyo3_async_runtimes::tokio::main]
236236
async fn main() -> PyResult<()> {
237-
let future = Python::with_gil(|py| -> PyResult<_> {
237+
let future = Python::attach(|py| -> PyResult<_> {
238238
// import the module containing the py_sleep function
239239
let example = py.import("example")?;
240240

@@ -354,7 +354,7 @@ use pyo3::prelude::*;
354354
async fn main() -> PyResult<()> {
355355
// PyO3 is initialized - Ready to go
356356
357-
let fut = Python::with_gil(|py| -> PyResult<_> {
357+
let fut = Python::attach(|py| -> PyResult<_> {
358358
let asyncio = py.import("asyncio")?;
359359
360360
// convert asyncio.sleep into a Rust Future
@@ -500,18 +500,18 @@ pyo3-async-runtimes = { version = "0.26", features = ["async-std-runtime"] }
500500
use pyo3::{prelude::*, types::PyType};
501501

502502
fn main() -> PyResult<()> {
503-
pyo3::prepare_freethreaded_python();
503+
Python::initialize();
504504

505-
Python::with_gil(|py| {
505+
Python::attach(|py| {
506506
let uvloop = py.import("uvloop")?;
507507
uvloop.call_method0("install")?;
508508

509509
// store a reference for the assertion
510-
let uvloop = PyObject::from(uvloop);
510+
let uvloop: Py<PyAny> = uvloop.into();
511511

512512
pyo3_async_runtimes::async_std::run(py, async move {
513513
// verify that we are on a uvloop.Loop
514-
Python::with_gil(|py| -> PyResult<()> {
514+
Python::attach(|py| -> PyResult<()> {
515515
assert!(uvloop
516516
.bind(py)
517517
.getattr("Loop")?

examples/async_std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use pyo3::prelude::*;
22

33
#[pyo3_async_runtimes::async_std::main]
44
async fn main() -> PyResult<()> {
5-
let fut = Python::with_gil(|py| {
5+
let fut = Python::attach(|py| {
66
let asyncio = py.import("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future

examples/tokio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use pyo3::prelude::*;
22

33
#[pyo3_async_runtimes::tokio::main]
44
async fn main() -> PyResult<()> {
5-
let fut = Python::with_gil(|py| {
5+
let fut = Python::attach(|py| {
66
let asyncio = py.import("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future

examples/tokio_current_thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use pyo3::prelude::*;
22

33
#[pyo3_async_runtimes::tokio::main(flavor = "current_thread")]
44
async fn main() -> PyResult<()> {
5-
let fut = Python::with_gil(|py| {
5+
let fut = Python::attach(|py| {
66
let asyncio = py.import("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future

examples/tokio_multi_thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use pyo3::prelude::*;
22

33
#[pyo3_async_runtimes::tokio::main(flavor = "multi_thread", worker_threads = 10)]
44
async fn main() -> PyResult<()> {
5-
let fut = Python::with_gil(|py| {
5+
let fut = Python::attach(|py| {
66
let asyncio = py.import("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future

pyo3-async-runtimes-macros/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ pub fn async_std_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
4949
#body
5050
}
5151

52-
pyo3::prepare_freethreaded_python();
52+
Python::initialize();
5353

54-
pyo3::Python::with_gil(|py| {
54+
pyo3::Python::attach(|py| {
5555
pyo3_async_runtimes::async_std::run(py, main())
5656
.map_err(|e| {
5757
e.print_and_set_sys_last_vars(py);
@@ -129,7 +129,7 @@ pub fn tokio_main(args: TokenStream, item: TokenStream) -> TokenStream {
129129
///
130130
/// // blocking test functions can optionally accept an event_loop parameter
131131
/// #[pyo3_async_runtimes::async_std::test]
132-
/// fn test_blocking_sleep_with_event_loop(event_loop: PyObject) -> PyResult<()> {
132+
/// fn test_blocking_sleep_with_event_loop(event_loop: Py<PyAny>) -> PyResult<()> {
133133
/// thread::sleep(Duration::from_secs(1));
134134
/// Ok(())
135135
/// }
@@ -154,7 +154,7 @@ pub fn async_std_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
154154
}
155155
} else {
156156
quote! {
157-
let event_loop = Python::with_gil(|py| {
157+
let event_loop = Python::attach(|py| {
158158
pyo3_async_runtimes::async_std::get_current_loop(py).unwrap().into()
159159
});
160160
Box::pin(pyo3_async_runtimes::async_std::re_exports::spawn_blocking(move || {
@@ -226,7 +226,7 @@ pub fn async_std_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
226226
///
227227
/// // blocking test functions can optionally accept an event_loop parameter
228228
/// #[pyo3_async_runtimes::tokio::test]
229-
/// fn test_blocking_sleep_with_event_loop(event_loop: PyObject) -> PyResult<()> {
229+
/// fn test_blocking_sleep_with_event_loop(event_loop: Py<PyAny>) -> PyResult<()> {
230230
/// thread::sleep(Duration::from_secs(1));
231231
/// Ok(())
232232
/// }
@@ -265,7 +265,7 @@ pub fn tokio_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
265265
}
266266
} else {
267267
quote! {
268-
let event_loop = Python::with_gil(|py| {
268+
let event_loop = Python::attach(|py| {
269269
pyo3_async_runtimes::tokio::get_current_loop(py).unwrap().into()
270270
});
271271
Box::pin(async move {

pyo3-async-runtimes-macros/src/tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn parse_knobs(
268268
#body
269269
}
270270

271-
pyo3::prepare_freethreaded_python();
271+
Python::initialize();
272272

273273
let mut builder = #builder;
274274
#builder_init;
@@ -277,7 +277,7 @@ fn parse_knobs(
277277

278278
#rt_init
279279

280-
pyo3::Python::with_gil(|py| {
280+
pyo3::Python::attach(|py| {
281281
pyo3_async_runtimes::tokio::run(py, main())
282282
.map_err(|e| {
283283
e.print_and_set_sys_last_vars(py);

pytests/common/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ async def sleep_for_1s(sleep_for):
1414
await sleep_for(1)
1515
"#;
1616

17-
pub(super) async fn test_into_future(event_loop: PyObject) -> PyResult<()> {
18-
let fut = Python::with_gil(|py| {
17+
pub(super) async fn test_into_future(event_loop: Py<PyAny>) -> PyResult<()> {
18+
let fut = Python::attach(|py| {
1919
let test_mod = PyModule::from_code(
2020
py,
2121
&CString::new(TEST_MOD).unwrap(),
@@ -39,8 +39,8 @@ pub(super) fn test_blocking_sleep() -> PyResult<()> {
3939
Ok(())
4040
}
4141

42-
pub(super) async fn test_other_awaitables(event_loop: PyObject) -> PyResult<()> {
43-
let fut = Python::with_gil(|py| {
42+
pub(super) async fn test_other_awaitables(event_loop: Py<PyAny>) -> PyResult<()> {
43+
let fut = Python::attach(|py| {
4444
let functools = py.import("functools")?;
4545
let time = py.import("time")?;
4646

pytests/test_async_std_asyncio.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn sleep<'p>(py: Python<'p>, secs: Bound<'p, PyAny>) -> PyResult<Bound<'p, PyAny
3030

3131
#[pyo3_async_runtimes::async_std::test]
3232
async fn test_future_into_py() -> PyResult<()> {
33-
let fut = Python::with_gil(|py| {
33+
let fut = Python::attach(|py| {
3434
let sleeper_mod = PyModule::new(py, "rust_sleeper")?;
3535

3636
sleeper_mod.add_wrapped(wrap_pyfunction!(sleep))?;
@@ -54,11 +54,11 @@ async fn test_future_into_py() -> PyResult<()> {
5454

5555
#[pyo3_async_runtimes::async_std::test]
5656
async fn test_async_sleep() -> PyResult<()> {
57-
let asyncio = Python::with_gil(|py| py.import("asyncio").map(PyObject::from))?;
57+
let asyncio = Python::attach(|py| py.import("asyncio").map(Py::<PyAny>::from))?;
5858

5959
task::sleep(Duration::from_secs(1)).await;
6060

61-
Python::with_gil(|py| {
61+
Python::attach(|py| {
6262
pyo3_async_runtimes::async_std::into_future(asyncio.bind(py).call_method1("sleep", (1.0,))?)
6363
})?
6464
.await?;
@@ -73,7 +73,7 @@ fn test_blocking_sleep() -> PyResult<()> {
7373

7474
#[pyo3_async_runtimes::async_std::test]
7575
async fn test_into_future() -> PyResult<()> {
76-
common::test_into_future(Python::with_gil(|py| {
76+
common::test_into_future(Python::attach(|py| {
7777
pyo3_async_runtimes::async_std::get_current_loop(py)
7878
.unwrap()
7979
.into()
@@ -83,7 +83,7 @@ async fn test_into_future() -> PyResult<()> {
8383

8484
#[pyo3_async_runtimes::async_std::test]
8585
async fn test_other_awaitables() -> PyResult<()> {
86-
common::test_other_awaitables(Python::with_gil(|py| {
86+
common::test_other_awaitables(Python::attach(|py| {
8787
pyo3_async_runtimes::async_std::get_current_loop(py)
8888
.unwrap()
8989
.into()
@@ -93,7 +93,7 @@ async fn test_other_awaitables() -> PyResult<()> {
9393

9494
#[pyo3_async_runtimes::async_std::test]
9595
async fn test_panic() -> PyResult<()> {
96-
let fut = Python::with_gil(|py| -> PyResult<_> {
96+
let fut = Python::attach(|py| -> PyResult<_> {
9797
pyo3_async_runtimes::async_std::into_future(
9898
pyo3_async_runtimes::async_std::future_into_py::<_, ()>(py, async {
9999
panic!("this panic was intentional!")
@@ -103,7 +103,7 @@ async fn test_panic() -> PyResult<()> {
103103

104104
match fut.await {
105105
Ok(_) => panic!("coroutine should panic"),
106-
Err(e) => Python::with_gil(|py| {
106+
Err(e) => Python::attach(|py| {
107107
if e.is_instance_of::<pyo3_async_runtimes::err::RustPanic>(py) {
108108
Ok(())
109109
} else {
@@ -115,7 +115,7 @@ async fn test_panic() -> PyResult<()> {
115115

116116
#[pyo3_async_runtimes::async_std::test]
117117
async fn test_local_future_into_py() -> PyResult<()> {
118-
Python::with_gil(|py| {
118+
Python::attach(|py| {
119119
let non_send_secs = Rc::new(1);
120120

121121
#[allow(deprecated)]
@@ -135,7 +135,7 @@ async fn test_local_future_into_py() -> PyResult<()> {
135135
async fn test_cancel() -> PyResult<()> {
136136
let completed = Arc::new(Mutex::new(false));
137137

138-
let py_future = Python::with_gil(|py| -> PyResult<PyObject> {
138+
let py_future = Python::attach(|py| -> PyResult<Py<PyAny>> {
139139
let completed = Arc::clone(&completed);
140140
Ok(
141141
pyo3_async_runtimes::async_std::future_into_py(py, async move {
@@ -148,13 +148,13 @@ async fn test_cancel() -> PyResult<()> {
148148
)
149149
})?;
150150

151-
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {
151+
if let Err(e) = Python::attach(|py| -> PyResult<_> {
152152
py_future.bind(py).call_method0("cancel")?;
153153
pyo3_async_runtimes::async_std::into_future(py_future.into_bound(py))
154154
})?
155155
.await
156156
{
157-
Python::with_gil(|py| -> PyResult<()> {
157+
Python::attach(|py| -> PyResult<()> {
158158
assert!(e.value(py).is_instance(
159159
py.import("asyncio")?
160160
.getattr("CancelledError")?
@@ -188,7 +188,7 @@ async def gen():
188188
#[cfg(feature = "unstable-streams")]
189189
#[pyo3_async_runtimes::async_std::test]
190190
async fn test_async_gen_v1() -> PyResult<()> {
191-
let stream = Python::with_gil(|py| {
191+
let stream = Python::attach(|py| {
192192
let test_mod = PyModule::from_code(
193193
py,
194194
&CString::new(ASYNC_STD_TEST_MOD).unwrap(),
@@ -200,7 +200,7 @@ async fn test_async_gen_v1() -> PyResult<()> {
200200
})?;
201201

202202
let vals = stream
203-
.map(|item| Python::with_gil(|py| -> PyResult<i32> { item?.bind(py).extract() }))
203+
.map(|item| Python::attach(|py| -> PyResult<i32> { item?.bind(py).extract() }))
204204
.try_collect::<Vec<i32>>()
205205
.await?;
206206

@@ -210,14 +210,14 @@ async fn test_async_gen_v1() -> PyResult<()> {
210210
}
211211

212212
#[pyo3_async_runtimes::async_std::test]
213-
fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
214-
let locals = Python::with_gil(|py| -> PyResult<TaskLocals> {
213+
fn test_local_cancel(event_loop: Py<PyAny>) -> PyResult<()> {
214+
let locals = Python::attach(|py| -> PyResult<TaskLocals> {
215215
TaskLocals::new(event_loop.into_bound(py)).copy_context(py)
216216
})?;
217217
async_std::task::block_on(pyo3_async_runtimes::async_std::scope_local(locals, async {
218218
let completed = Arc::new(Mutex::new(false));
219219

220-
let py_future = Python::with_gil(|py| -> PyResult<PyObject> {
220+
let py_future = Python::attach(|py| -> PyResult<Py<PyAny>> {
221221
let completed = Arc::clone(&completed);
222222
Ok(
223223
pyo3_async_runtimes::async_std::future_into_py(py, async move {
@@ -230,13 +230,13 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
230230
)
231231
})?;
232232

233-
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {
233+
if let Err(e) = Python::attach(|py| -> PyResult<_> {
234234
py_future.bind(py).call_method0("cancel")?;
235235
pyo3_async_runtimes::async_std::into_future(py_future.into_bound(py))
236236
})?
237237
.await
238238
{
239-
Python::with_gil(|py| -> PyResult<()> {
239+
Python::attach(|py| -> PyResult<()> {
240240
assert!(e.value(py).is_instance(
241241
py.import("asyncio")?
242242
.getattr("CancelledError")?
@@ -284,7 +284,7 @@ asyncio.new_event_loop().run_until_complete(main())
284284

285285
#[pyo3_async_runtimes::async_std::test]
286286
fn test_multiple_asyncio_run() -> PyResult<()> {
287-
Python::with_gil(|py| {
287+
Python::attach(|py| {
288288
pyo3_async_runtimes::async_std::run(py, async move {
289289
async_std::task::sleep(Duration::from_millis(500)).await;
290290
Ok(())
@@ -310,9 +310,9 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
310310
fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
311311
#![allow(deprecated)]
312312
#[pyfunction]
313-
pub(crate) fn async_callback(py: Python, callback: PyObject) -> PyResult<Bound<PyAny>> {
313+
pub(crate) fn async_callback(py: Python, callback: Py<PyAny>) -> PyResult<Bound<PyAny>> {
314314
pyo3_async_runtimes::async_std::future_into_py(py, async move {
315-
Python::with_gil(|py| {
315+
Python::attach(|py| {
316316
pyo3_async_runtimes::async_std::into_future(callback.bind(py).call0()?)
317317
})?
318318
.await?;
@@ -329,7 +329,7 @@ fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
329329
#[cfg(feature = "unstable-streams")]
330330
#[pyo3_async_runtimes::async_std::test]
331331
async fn test_async_gen_v2() -> PyResult<()> {
332-
let stream = Python::with_gil(|py| {
332+
let stream = Python::attach(|py| {
333333
let test_mod = PyModule::from_code(
334334
py,
335335
&CString::new(ASYNC_STD_TEST_MOD).unwrap(),
@@ -341,7 +341,7 @@ async fn test_async_gen_v2() -> PyResult<()> {
341341
})?;
342342

343343
let vals = stream
344-
.map(|item| Python::with_gil(|py| -> PyResult<i32> { item.bind(py).extract() }))
344+
.map(|item| Python::attach(|py| -> PyResult<i32> { item.bind(py).extract() }))
345345
.try_collect::<Vec<i32>>()
346346
.await?;
347347

@@ -365,7 +365,7 @@ asyncio.run(main())
365365

366366
#[pyo3_async_runtimes::async_std::test]
367367
fn test_contextvars() -> PyResult<()> {
368-
Python::with_gil(|py| {
368+
Python::attach(|py| {
369369
let d = [
370370
("asyncio", py.import("asyncio")?.into()),
371371
("contextvars", py.import("contextvars")?.into()),
@@ -380,9 +380,9 @@ fn test_contextvars() -> PyResult<()> {
380380
}
381381

382382
fn main() -> pyo3::PyResult<()> {
383-
pyo3::prepare_freethreaded_python();
383+
Python::initialize();
384384

385-
Python::with_gil(|py| {
385+
Python::attach(|py| {
386386
pyo3_async_runtimes::async_std::run(py, pyo3_async_runtimes::testing::main())
387387
})
388388
}

0 commit comments

Comments
 (0)