@@ -3,7 +3,7 @@ mod common;
3
3
use std:: { rc:: Rc , time:: Duration } ;
4
4
5
5
use async_std:: task;
6
- use pyo3:: { prelude:: * , wrap_pyfunction} ;
6
+ use pyo3:: { prelude:: * , types :: PyType , wrap_pyfunction} ;
7
7
8
8
#[ pyfunction]
9
9
fn sleep_for ( py : Python , secs : & PyAny ) -> PyResult < PyObject > {
@@ -54,24 +54,6 @@ async fn test_async_sleep() -> PyResult<()> {
54
54
Ok ( ( ) )
55
55
}
56
56
57
- #[ pyo3_asyncio:: async_std:: test]
58
- async fn test_cancel ( ) -> PyResult < ( ) > {
59
- let py_future = Python :: with_gil ( |py| {
60
- pyo3_asyncio:: async_std:: into_coroutine ( py, async {
61
- async_std:: task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
62
- Ok ( Python :: with_gil ( |py| py. None ( ) ) )
63
- } )
64
- } ) ?;
65
-
66
- Python :: with_gil ( |py| -> PyResult < _ > {
67
- py_future. as_ref ( py) . call_method0 ( "cancel" ) ?;
68
- pyo3_asyncio:: into_future ( py_future. as_ref ( py) )
69
- } ) ?
70
- . await ?;
71
-
72
- Ok ( ( ) )
73
- }
74
-
75
57
#[ pyo3_asyncio:: async_std:: test]
76
58
fn test_blocking_sleep ( ) -> PyResult < ( ) > {
77
59
common:: test_blocking_sleep ( )
@@ -113,3 +95,34 @@ async fn test_local_coroutine() -> PyResult<()> {
113
95
114
96
Ok ( ( ) )
115
97
}
98
+
99
+ #[ pyo3_asyncio:: async_std:: test]
100
+ async fn test_cancel ( ) -> PyResult < ( ) > {
101
+ let py_future = Python :: with_gil ( |py| {
102
+ pyo3_asyncio:: async_std:: into_coroutine ( py, async {
103
+ async_std:: task:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
104
+ Ok ( Python :: with_gil ( |py| py. None ( ) ) )
105
+ } )
106
+ } ) ?;
107
+
108
+ if let Err ( e) = Python :: with_gil ( |py| -> PyResult < _ > {
109
+ py_future. as_ref ( py) . call_method0 ( "cancel" ) ?;
110
+ pyo3_asyncio:: into_future ( py_future. as_ref ( py) )
111
+ } ) ?
112
+ . await
113
+ {
114
+ Python :: with_gil ( |py| -> PyResult < ( ) > {
115
+ assert ! ( py
116
+ . import( "asyncio.exceptions" ) ?
117
+ . getattr( "CancelledError" ) ?
118
+ . downcast:: <PyType >( )
119
+ . unwrap( )
120
+ . is_instance( e. pvalue( py) ) ?) ;
121
+ Ok ( ( ) )
122
+ } ) ?;
123
+ } else {
124
+ panic ! ( "expected CancelledError" ) ;
125
+ }
126
+
127
+ Ok ( ( ) )
128
+ }
0 commit comments