@@ -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]
3232async 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]
5656async 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]
7575async 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]
8585async 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]
9595async 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]
117117async 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<()> {
135135async 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]
190190async 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]
286286fn 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<()> {
310310fn 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]
331331async 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]
367367fn 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
382382fn 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