@@ -18,7 +18,7 @@ use pyo3_asyncio::TaskLocals;
18
18
use futures:: { StreamExt , TryStreamExt } ;
19
19
20
20
#[ pyfunction]
21
- fn sleep < ' p > ( py : Python < ' p > , secs : & ' p PyAny ) -> PyResult < & ' p PyAny > {
21
+ fn sleep < ' p > ( py : Python < ' p > , secs : Bound < ' p , PyAny > ) -> PyResult < Bound < ' p , PyAny > > {
22
22
let secs = secs. extract ( ) ?;
23
23
24
24
pyo3_asyncio:: async_std:: future_into_py ( py, async move {
@@ -30,11 +30,11 @@ fn sleep<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
30
30
#[ pyo3_asyncio:: async_std:: test]
31
31
async fn test_future_into_py ( ) -> PyResult < ( ) > {
32
32
let fut = Python :: with_gil ( |py| {
33
- let sleeper_mod = PyModule :: new ( py, "rust_sleeper" ) ?;
33
+ let sleeper_mod = PyModule :: new_bound ( py, "rust_sleeper" ) ?;
34
34
35
35
sleeper_mod. add_wrapped ( wrap_pyfunction ! ( sleep) ) ?;
36
36
37
- let test_mod = PyModule :: from_code (
37
+ let test_mod = PyModule :: from_code_bound (
38
38
py,
39
39
common:: TEST_MOD ,
40
40
"test_future_into_py_mod.py" ,
@@ -53,13 +53,15 @@ async fn test_future_into_py() -> PyResult<()> {
53
53
54
54
#[ pyo3_asyncio:: async_std:: test]
55
55
async fn test_async_sleep ( ) -> PyResult < ( ) > {
56
- let asyncio =
57
- Python :: with_gil ( |py| py. import ( "asyncio" ) . map ( |asyncio| PyObject :: from ( asyncio) ) ) ?;
56
+ let asyncio = Python :: with_gil ( |py| {
57
+ py. import_bound ( "asyncio" )
58
+ . map ( |asyncio| PyObject :: from ( asyncio) )
59
+ } ) ?;
58
60
59
61
task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
60
62
61
63
Python :: with_gil ( |py| {
62
- pyo3_asyncio:: async_std:: into_future ( asyncio. as_ref ( py) . call_method1 ( "sleep" , ( 1.0 , ) ) ?)
64
+ pyo3_asyncio:: async_std:: into_future ( asyncio. bind ( py) . call_method1 ( "sleep" , ( 1.0 , ) ) ?)
63
65
} ) ?
64
66
. await ?;
65
67
@@ -146,14 +148,14 @@ async fn test_cancel() -> PyResult<()> {
146
148
} ) ?;
147
149
148
150
if let Err ( e) = Python :: with_gil ( |py| -> PyResult < _ > {
149
- py_future. as_ref ( py) . call_method0 ( "cancel" ) ?;
150
- pyo3_asyncio:: async_std:: into_future ( py_future. as_ref ( py) )
151
+ py_future. bind ( py) . call_method0 ( "cancel" ) ?;
152
+ pyo3_asyncio:: async_std:: into_future ( py_future. into_bound ( py) )
151
153
} ) ?
152
154
. await
153
155
{
154
156
Python :: with_gil ( |py| -> PyResult < ( ) > {
155
- assert ! ( e. value ( py) . is_instance(
156
- py. import ( "asyncio" ) ?
157
+ assert ! ( e. value_bound ( py) . is_instance(
158
+ py. import_bound ( "asyncio" ) ?
157
159
. getattr( "CancelledError" ) ?
158
160
. downcast:: <PyType >( )
159
161
. unwrap( )
@@ -186,7 +188,7 @@ async def gen():
186
188
#[ pyo3_asyncio:: async_std:: test]
187
189
async fn test_async_gen_v1 ( ) -> PyResult < ( ) > {
188
190
let stream = Python :: with_gil ( |py| {
189
- let test_mod = PyModule :: from_code (
191
+ let test_mod = PyModule :: from_code_bound (
190
192
py,
191
193
ASYNC_STD_TEST_MOD ,
192
194
"test_rust_coroutine/async_std_test_mod.py" ,
@@ -197,7 +199,7 @@ async fn test_async_gen_v1() -> PyResult<()> {
197
199
} ) ?;
198
200
199
201
let vals = stream
200
- . map ( |item| Python :: with_gil ( |py| -> PyResult < i32 > { Ok ( item?. as_ref ( py) . extract ( ) ?) } ) )
202
+ . map ( |item| Python :: with_gil ( |py| -> PyResult < i32 > { Ok ( item?. bind ( py) . extract ( ) ?) } ) )
201
203
. try_collect :: < Vec < i32 > > ( )
202
204
. await ?;
203
205
@@ -209,7 +211,7 @@ async fn test_async_gen_v1() -> PyResult<()> {
209
211
#[ pyo3_asyncio:: async_std:: test]
210
212
fn test_local_cancel ( event_loop : PyObject ) -> PyResult < ( ) > {
211
213
let locals = Python :: with_gil ( |py| -> PyResult < TaskLocals > {
212
- Ok ( TaskLocals :: new ( event_loop. as_ref ( py) ) . copy_context ( py) ?)
214
+ Ok ( TaskLocals :: new ( event_loop. into_bound ( py) ) . copy_context ( py) ?)
213
215
} ) ?;
214
216
async_std:: task:: block_on ( pyo3_asyncio:: async_std:: scope_local ( locals, async {
215
217
let completed = Arc :: new ( Mutex :: new ( false ) ) ;
@@ -226,14 +228,14 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
226
228
} ) ?;
227
229
228
230
if let Err ( e) = Python :: with_gil ( |py| -> PyResult < _ > {
229
- py_future. as_ref ( py) . call_method0 ( "cancel" ) ?;
230
- pyo3_asyncio:: async_std:: into_future ( py_future. as_ref ( py) )
231
+ py_future. bind ( py) . call_method0 ( "cancel" ) ?;
232
+ pyo3_asyncio:: async_std:: into_future ( py_future. into_bound ( py) )
231
233
} ) ?
232
234
. await
233
235
{
234
236
Python :: with_gil ( |py| -> PyResult < ( ) > {
235
- assert ! ( e. value ( py) . is_instance(
236
- py. import ( "asyncio" ) ?
237
+ assert ! ( e. value_bound ( py) . is_instance(
238
+ py. import_bound ( "asyncio" ) ?
237
239
. getattr( "CancelledError" ) ?
238
240
. downcast:: <PyType >( )
239
241
. unwrap( )
@@ -258,7 +260,7 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
258
260
fn test_mod ( _py : Python , m : & PyModule ) -> PyResult < ( ) > {
259
261
#![ allow( deprecated) ]
260
262
#[ pyfunction( name = "sleep" ) ]
261
- fn sleep_ ( py : Python ) -> PyResult < & PyAny > {
263
+ fn sleep_ ( py : Python ) -> PyResult < Bound < PyAny > > {
262
264
pyo3_asyncio:: async_std:: future_into_py ( py, async move {
263
265
async_std:: task:: sleep ( Duration :: from_millis ( 500 ) ) . await ;
264
266
Ok ( ( ) )
@@ -290,13 +292,13 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
290
292
} ) ?;
291
293
292
294
let d = [
293
- ( "asyncio" , py. import ( "asyncio" ) ?. into ( ) ) ,
295
+ ( "asyncio" , py. import_bound ( "asyncio" ) ?. into ( ) ) ,
294
296
( "test_mod" , wrap_pymodule ! ( test_mod) ( py) ) ,
295
297
]
296
- . into_py_dict ( py) ;
298
+ . into_py_dict_bound ( py) ;
297
299
298
- py. run ( MULTI_ASYNCIO_CODE , Some ( d) , None ) ?;
299
- py. run ( MULTI_ASYNCIO_CODE , Some ( d) , None ) ?;
300
+ py. run_bound ( MULTI_ASYNCIO_CODE , Some ( & d) , None ) ?;
301
+ py. run_bound ( MULTI_ASYNCIO_CODE , Some ( & d) , None ) ?;
300
302
Ok ( ( ) )
301
303
} )
302
304
}
@@ -305,10 +307,10 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
305
307
fn cvars_mod ( _py : Python , m : & PyModule ) -> PyResult < ( ) > {
306
308
#![ allow( deprecated) ]
307
309
#[ pyfunction]
308
- pub ( crate ) fn async_callback ( py : Python , callback : PyObject ) -> PyResult < & PyAny > {
310
+ pub ( crate ) fn async_callback ( py : Python , callback : PyObject ) -> PyResult < Bound < PyAny > > {
309
311
pyo3_asyncio:: async_std:: future_into_py ( py, async move {
310
312
Python :: with_gil ( |py| {
311
- pyo3_asyncio:: async_std:: into_future ( callback. as_ref ( py) . call0 ( ) ?)
313
+ pyo3_asyncio:: async_std:: into_future ( callback. bind ( py) . call0 ( ) ?)
312
314
} ) ?
313
315
. await ?;
314
316
@@ -325,7 +327,7 @@ fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
325
327
#[ pyo3_asyncio:: async_std:: test]
326
328
async fn test_async_gen_v2 ( ) -> PyResult < ( ) > {
327
329
let stream = Python :: with_gil ( |py| {
328
- let test_mod = PyModule :: from_code (
330
+ let test_mod = PyModule :: from_code_bound (
329
331
py,
330
332
ASYNC_STD_TEST_MOD ,
331
333
"test_rust_coroutine/async_std_test_mod.py" ,
@@ -336,7 +338,7 @@ async fn test_async_gen_v2() -> PyResult<()> {
336
338
} ) ?;
337
339
338
340
let vals = stream
339
- . map ( |item| Python :: with_gil ( |py| -> PyResult < i32 > { Ok ( item. as_ref ( py) . extract ( ) ?) } ) )
341
+ . map ( |item| Python :: with_gil ( |py| -> PyResult < i32 > { Ok ( item. bind ( py) . extract ( ) ?) } ) )
340
342
. try_collect :: < Vec < i32 > > ( )
341
343
. await ?;
342
344
@@ -362,14 +364,14 @@ asyncio.run(main())
362
364
fn test_contextvars ( ) -> PyResult < ( ) > {
363
365
Python :: with_gil ( |py| {
364
366
let d = [
365
- ( "asyncio" , py. import ( "asyncio" ) ?. into ( ) ) ,
366
- ( "contextvars" , py. import ( "contextvars" ) ?. into ( ) ) ,
367
+ ( "asyncio" , py. import_bound ( "asyncio" ) ?. into ( ) ) ,
368
+ ( "contextvars" , py. import_bound ( "contextvars" ) ?. into ( ) ) ,
367
369
( "cvars_mod" , wrap_pymodule ! ( cvars_mod) ( py) ) ,
368
370
]
369
- . into_py_dict ( py) ;
371
+ . into_py_dict_bound ( py) ;
370
372
371
- py. run ( CONTEXTVARS_CODE , Some ( d) , None ) ?;
372
- py. run ( CONTEXTVARS_CODE , Some ( d) , None ) ?;
373
+ py. run_bound ( CONTEXTVARS_CODE , Some ( & d) , None ) ?;
374
+ py. run_bound ( CONTEXTVARS_CODE , Some ( & d) , None ) ?;
373
375
Ok ( ( ) )
374
376
} )
375
377
}
0 commit comments