Skip to content

Commit 4bb903e

Browse files
authored
ci: tidy up some with_gil(|gil| -> with_gil(|py| (PyO3#5104)
1 parent dd1f788 commit 4bb903e

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

guide/src/async-await.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ where
6666
6767
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
6868
let waker = cx.waker();
69-
Python::with_gil(|gil| {
70-
gil.allow_threads(|| pin!(&mut self.0).poll(&mut Context::from_waker(waker)))
69+
Python::with_gil(|py| {
70+
py.allow_threads(|| pin!(&mut self.0).poll(&mut Context::from_waker(waker)))
7171
})
7272
}
7373
}

src/coroutine/waker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ impl Wake for AsyncioWaker {
4141
}
4242

4343
fn wake_by_ref(self: &Arc<Self>) {
44-
Python::with_gil(|gil| {
45-
if let Some(loop_and_future) = self.0.get_or_init(gil, || None) {
44+
Python::with_gil(|py| {
45+
if let Some(loop_and_future) = self.0.get_or_init(py, || None) {
4646
loop_and_future
47-
.set_result(gil)
47+
.set_result(py)
4848
.expect("unexpected error in coroutine waker");
4949
}
5050
});

src/impl_/coroutine.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ impl<T: PyClass> Deref for RefGuard<T> {
5050

5151
impl<T: PyClass> Drop for RefGuard<T> {
5252
fn drop(&mut self) {
53-
Python::with_gil(|gil| {
53+
Python::with_gil(|py| {
5454
self.0
55-
.bind(gil)
55+
.bind(py)
5656
.get_class_object()
5757
.borrow_checker()
5858
.release_borrow()
@@ -87,9 +87,9 @@ impl<T: PyClass<Frozen = False>> DerefMut for RefMutGuard<T> {
8787

8888
impl<T: PyClass<Frozen = False>> Drop for RefMutGuard<T> {
8989
fn drop(&mut self) {
90-
Python::with_gil(|gil| {
90+
Python::with_gil(|py| {
9191
self.0
92-
.bind(gil)
92+
.bind(py)
9393
.get_class_object()
9494
.borrow_checker()
9595
.release_borrow_mut()

tests/test_coroutine.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ fn noop_coroutine() {
3232
async fn noop() -> usize {
3333
42
3434
}
35-
Python::with_gil(|gil| {
36-
let noop = wrap_pyfunction!(noop, gil).unwrap();
35+
Python::with_gil(|py| {
36+
let noop = wrap_pyfunction!(noop, py).unwrap();
3737
let test = "import asyncio; assert asyncio.run(noop()) == 42";
38-
py_run!(gil, noop, &handle_windows(test));
38+
py_run!(py, noop, &handle_windows(test));
3939
})
4040
}
4141

@@ -58,7 +58,7 @@ fn test_coroutine_qualname() {
5858
#[staticmethod]
5959
async fn my_staticmethod() {}
6060
}
61-
Python::with_gil(|gil| {
61+
Python::with_gil(|py| {
6262
let test = r#"
6363
for coro, name, qualname in [
6464
(my_fn(), "my_fn", "my_fn"),
@@ -69,12 +69,12 @@ fn test_coroutine_qualname() {
6969
assert coro.__name__ == name and coro.__qualname__ == qualname
7070
"#;
7171
let locals = [
72-
("my_fn", wrap_pyfunction!(my_fn, gil).unwrap().as_any()),
73-
("MyClass", gil.get_type::<MyClass>().as_any()),
72+
("my_fn", wrap_pyfunction!(my_fn, py).unwrap().as_any()),
73+
("MyClass", py.get_type::<MyClass>().as_any()),
7474
]
75-
.into_py_dict(gil)
75+
.into_py_dict(py)
7676
.unwrap();
77-
py_run!(gil, *locals, &handle_windows(test));
77+
py_run!(py, *locals, &handle_windows(test));
7878
})
7979
}
8080

@@ -93,10 +93,10 @@ fn sleep_0_like_coroutine() {
9393
})
9494
.await
9595
}
96-
Python::with_gil(|gil| {
97-
let sleep_0 = wrap_pyfunction!(sleep_0, gil).unwrap();
96+
Python::with_gil(|py| {
97+
let sleep_0 = wrap_pyfunction!(sleep_0, py).unwrap();
9898
let test = "import asyncio; assert asyncio.run(sleep_0()) == 42";
99-
py_run!(gil, sleep_0, &handle_windows(test));
99+
py_run!(py, sleep_0, &handle_windows(test));
100100
})
101101
}
102102

@@ -112,10 +112,10 @@ async fn sleep(seconds: f64) -> usize {
112112

113113
#[test]
114114
fn sleep_coroutine() {
115-
Python::with_gil(|gil| {
116-
let sleep = wrap_pyfunction!(sleep, gil).unwrap();
115+
Python::with_gil(|py| {
116+
let sleep = wrap_pyfunction!(sleep, py).unwrap();
117117
let test = r#"import asyncio; assert asyncio.run(sleep(0.1)) == 42"#;
118-
py_run!(gil, sleep, &handle_windows(test));
118+
py_run!(py, sleep, &handle_windows(test));
119119
})
120120
}
121121

@@ -126,17 +126,17 @@ async fn return_tuple() -> (usize, usize) {
126126

127127
#[test]
128128
fn tuple_coroutine() {
129-
Python::with_gil(|gil| {
130-
let func = wrap_pyfunction!(return_tuple, gil).unwrap();
129+
Python::with_gil(|py| {
130+
let func = wrap_pyfunction!(return_tuple, py).unwrap();
131131
let test = r#"import asyncio; assert asyncio.run(func()) == (42, 43)"#;
132-
py_run!(gil, func, &handle_windows(test));
132+
py_run!(py, func, &handle_windows(test));
133133
})
134134
}
135135

136136
#[test]
137137
fn cancelled_coroutine() {
138-
Python::with_gil(|gil| {
139-
let sleep = wrap_pyfunction!(sleep, gil).unwrap();
138+
Python::with_gil(|py| {
139+
let sleep = wrap_pyfunction!(sleep, py).unwrap();
140140
let test = r#"
141141
import asyncio
142142
async def main():
@@ -146,17 +146,17 @@ fn cancelled_coroutine() {
146146
await task
147147
asyncio.run(main())
148148
"#;
149-
let globals = PyDict::new(gil);
149+
let globals = PyDict::new(py);
150150
globals.set_item("sleep", sleep).unwrap();
151-
let err = gil
151+
let err = py
152152
.run(
153153
&CString::new(pyo3::unindent::unindent(&handle_windows(test))).unwrap(),
154154
Some(&globals),
155155
None,
156156
)
157157
.unwrap_err();
158158
assert_eq!(
159-
err.value(gil).get_type().qualname().unwrap(),
159+
err.value(py).get_type().qualname().unwrap(),
160160
"CancelledError"
161161
);
162162
})
@@ -174,8 +174,8 @@ fn coroutine_cancel_handle() {
174174
_ = cancel.cancelled().fuse() => 0,
175175
}
176176
}
177-
Python::with_gil(|gil| {
178-
let cancellable_sleep = wrap_pyfunction!(cancellable_sleep, gil).unwrap();
177+
Python::with_gil(|py| {
178+
let cancellable_sleep = wrap_pyfunction!(cancellable_sleep, py).unwrap();
179179
let test = r#"
180180
import asyncio;
181181
async def main():
@@ -185,11 +185,11 @@ fn coroutine_cancel_handle() {
185185
return await task
186186
assert asyncio.run(main()) == 0
187187
"#;
188-
let globals = PyDict::new(gil);
188+
let globals = PyDict::new(py);
189189
globals
190190
.set_item("cancellable_sleep", cancellable_sleep)
191191
.unwrap();
192-
gil.run(
192+
py.run(
193193
&CString::new(pyo3::unindent::unindent(&handle_windows(test))).unwrap(),
194194
Some(&globals),
195195
None,
@@ -206,8 +206,8 @@ fn coroutine_is_cancelled() {
206206
sleep(0.001).await;
207207
}
208208
}
209-
Python::with_gil(|gil| {
210-
let sleep_loop = wrap_pyfunction!(sleep_loop, gil).unwrap();
209+
Python::with_gil(|py| {
210+
let sleep_loop = wrap_pyfunction!(sleep_loop, py).unwrap();
211211
let test = r#"
212212
import asyncio;
213213
async def main():
@@ -217,9 +217,9 @@ fn coroutine_is_cancelled() {
217217
await task
218218
asyncio.run(main())
219219
"#;
220-
let globals = PyDict::new(gil);
220+
let globals = PyDict::new(py);
221221
globals.set_item("sleep_loop", sleep_loop).unwrap();
222-
gil.run(
222+
py.run(
223223
&CString::new(pyo3::unindent::unindent(&handle_windows(test))).unwrap(),
224224
Some(&globals),
225225
None,
@@ -234,8 +234,8 @@ fn coroutine_panic() {
234234
async fn panic() {
235235
panic!("test panic");
236236
}
237-
Python::with_gil(|gil| {
238-
let panic = wrap_pyfunction!(panic, gil).unwrap();
237+
Python::with_gil(|py| {
238+
let panic = wrap_pyfunction!(panic, py).unwrap();
239239
let test = r#"
240240
import asyncio
241241
coro = panic()
@@ -253,7 +253,7 @@ fn coroutine_panic() {
253253
else:
254254
assert False;
255255
"#;
256-
py_run!(gil, panic, &handle_windows(test));
256+
py_run!(py, panic, &handle_windows(test));
257257
})
258258
}
259259

@@ -284,7 +284,7 @@ fn test_async_method_receiver() {
284284
}
285285
}
286286

287-
Python::with_gil(|gil| {
287+
Python::with_gil(|py| {
288288
let test = r#"
289289
import asyncio
290290
@@ -314,10 +314,10 @@ fn test_async_method_receiver() {
314314
assert False
315315
assert asyncio.run(coro3) == 1
316316
"#;
317-
let locals = [("Counter", gil.get_type::<Counter>())]
318-
.into_py_dict(gil)
317+
let locals = [("Counter", py.get_type::<Counter>())]
318+
.into_py_dict(py)
319319
.unwrap();
320-
py_run!(gil, *locals, test);
320+
py_run!(py, *locals, test);
321321
});
322322

323323
assert!(IS_DROPPED.load(Ordering::SeqCst));
@@ -342,7 +342,7 @@ fn test_async_method_receiver_with_other_args() {
342342
}
343343
}
344344

345-
Python::with_gil(|gil| {
345+
Python::with_gil(|py| {
346346
let test = r#"
347347
import asyncio
348348
@@ -351,9 +351,9 @@ fn test_async_method_receiver_with_other_args() {
351351
assert asyncio.run(v.set_value(10)) == 10
352352
assert asyncio.run(v.get_value_plus_with(1, 1)) == 12
353353
"#;
354-
let locals = [("Value", gil.get_type::<Value>())]
355-
.into_py_dict(gil)
354+
let locals = [("Value", py.get_type::<Value>())]
355+
.into_py_dict(py)
356356
.unwrap();
357-
py_run!(gil, *locals, test);
357+
py_run!(py, *locals, test);
358358
});
359359
}

0 commit comments

Comments
 (0)