Skip to content

Commit bb7bd18

Browse files
committed
Bound 'py lifetime by GILPool when it's possible
1 parent 97fd658 commit bb7bd18

18 files changed

+170
-138
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Changed
1111

1212
* `PyObject` and `Py<T>` reference counts are now decremented sooner after `drop()`. [#851](https://github.com/PyO3/pyo3/pull/851)
13-
* When the GIL is held, the refcount is now decreased immediately on drop. (Previously would wait until just before releasing the GIL.)
14-
* When the GIL is not held, the refcount is now decreased when the GIL is next acquired. (Previously would wait until next time the GIL was released.)
13+
* When the GIL is held, the refcount is now decreased immediately on drop. (Previously would wait until just before releasing the GIL.)
1514

1615
### Added
1716
* `_PyDict_NewPresized`. [#849](https://github.com/PyO3/pyo3/pull/849)
@@ -20,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2019
### Fixed
2120
* `__radd__` and other `__r*__` methods now correctly work with operators. [#839](https://github.com/PyO3/pyo3/pull/839)
2221
* Garbage Collector causing random panics when traversing objects that were mutably borrowed. [#855](https://github.com/PyO3/pyo3/pull/855)
22+
* `&'static Py~` is not allowed as arguments. [#869](https://github.com/PyO3/pyo3/pull/869)
2323

2424
## [0.9.2]
2525

pyo3-derive-backend/src/module.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ fn function_c_wrapper(name: &Ident, spec: &method::FnSpec<'_>) -> TokenStream {
220220
{
221221
const _LOCATION: &'static str = concat!(stringify!(#name), "()");
222222

223-
let _py = pyo3::Python::assume_gil_acquired();
223+
let _pool = pyo3::GILPool::new();
224+
let _py = _pool.py();
224225
pyo3::run_callback(_py, || {
225-
let _pool = pyo3::GILPool::new(_py);
226226
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
227227
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
228228

pyo3-derive-backend/src/pymethod.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ fn impl_wrap_common(
105105
{
106106
const _LOCATION: &'static str = concat!(
107107
stringify!(#cls), ".", stringify!(#python_name), "()");
108-
let _py = pyo3::Python::assume_gil_acquired();
108+
let _pool = pyo3::GILPool::new();
109+
let _py = _pool.py();
109110
pyo3::run_callback(_py, || {
110-
let _pool = pyo3::GILPool::new(_py);
111111
#slf
112112
pyo3::callback::convert(_py, #body)
113113
})
@@ -124,9 +124,9 @@ fn impl_wrap_common(
124124
{
125125
const _LOCATION: &'static str = concat!(
126126
stringify!(#cls), ".", stringify!(#python_name), "()");
127-
let _py = pyo3::Python::assume_gil_acquired();
127+
let _pool = pyo3::GILPool::new();
128+
let _py = _pool.py();
128129
pyo3::run_callback(_py, || {
129-
let _pool = pyo3::GILPool::new(_py);
130130
#slf
131131
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
132132
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
@@ -155,9 +155,9 @@ pub fn impl_proto_wrap(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
155155
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
156156
{
157157
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
158-
let _py = pyo3::Python::assume_gil_acquired();
158+
let _pool = pyo3::GILPool::new();
159+
let _py = _pool.py();
159160
pyo3::run_callback(_py, || {
160-
let _pool = pyo3::GILPool::new(_py);
161161
let _slf = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
162162
#borrow_self
163163
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
@@ -193,9 +193,9 @@ pub fn impl_wrap_new(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
193193
use pyo3::type_object::PyTypeInfo;
194194

195195
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
196-
let _py = pyo3::Python::assume_gil_acquired();
196+
let _pool = pyo3::GILPool::new();
197+
let _py = _pool.py();
197198
pyo3::run_callback(_py, || {
198-
let _pool = pyo3::GILPool::new(_py);
199199
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
200200
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
201201

@@ -225,9 +225,9 @@ pub fn impl_wrap_class(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
225225
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
226226
{
227227
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
228-
let _py = pyo3::Python::assume_gil_acquired();
228+
let _pool = pyo3::GILPool::new();
229+
let _py = _pool.py();
229230
pyo3::run_callback(_py, || {
230-
let _pool = pyo3::GILPool::new(_py);
231231
let _cls = pyo3::types::PyType::from_type_ptr(_py, _cls as *mut pyo3::ffi::PyTypeObject);
232232
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
233233
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
@@ -257,9 +257,9 @@ pub fn impl_wrap_static(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
257257
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
258258
{
259259
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
260-
let _py = pyo3::Python::assume_gil_acquired();
260+
let _pool = pyo3::GILPool::new();
261+
let _py = _pool.py();
261262
pyo3::run_callback(_py, || {
262-
let _pool = pyo3::GILPool::new(_py);
263263
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
264264
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
265265

@@ -315,9 +315,9 @@ pub(crate) fn impl_wrap_getter(
315315
{
316316
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
317317

318-
let _py = pyo3::Python::assume_gil_acquired();
318+
let _pool = pyo3::GILPool::new();
319+
let _py = _pool.py();
319320
pyo3::run_callback(_py, || {
320-
let _pool = pyo3::GILPool::new(_py);
321321
let _slf = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
322322
#borrow_self
323323
pyo3::callback::convert(_py, #getter_impl)
@@ -372,9 +372,9 @@ pub(crate) fn impl_wrap_setter(
372372
_value: *mut pyo3::ffi::PyObject, _: *mut ::std::os::raw::c_void) -> pyo3::libc::c_int
373373
{
374374
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
375-
let _py = pyo3::Python::assume_gil_acquired();
375+
let _pool = pyo3::GILPool::new();
376+
let _py = _pool.py();
376377
pyo3::run_callback(_py, || {
377-
let _pool = pyo3::GILPool::new(_py);
378378
let _slf = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
379379
#borrow_self
380380
let _value = _py.from_borrowed_ptr::<pyo3::types::PyAny>(_value);

src/class/basic.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::callback::HashCallbackOutput;
1212
use crate::class::methods::PyMethodDef;
1313
use crate::{
1414
callback, exceptions, ffi, run_callback, FromPyObject, GILPool, IntoPy, ObjectProtocol, PyAny,
15-
PyCell, PyClass, PyErr, PyObject, PyResult, Python,
15+
PyCell, PyClass, PyErr, PyObject, PyResult,
1616
};
1717
use std::os::raw::c_int;
1818

@@ -218,10 +218,9 @@ where
218218
where
219219
T: for<'p> PyObjectGetAttrProtocol<'p>,
220220
{
221-
let py = Python::assume_gil_acquired();
221+
let pool = GILPool::new();
222+
let py = pool.py();
222223
run_callback(py, || {
223-
let _pool = GILPool::new(py);
224-
225224
// Behave like python's __getattr__ (as opposed to __getattribute__) and check
226225
// for existing fields and methods first
227226
let existing = ffi::PyObject_GenericGetAttr(slf, arg);
@@ -485,9 +484,9 @@ where
485484
where
486485
T: for<'p> PyObjectRichcmpProtocol<'p>,
487486
{
488-
let py = Python::assume_gil_acquired();
487+
let pool = GILPool::new();
488+
let py = pool.py();
489489
run_callback(py, || {
490-
let _pool = GILPool::new(py);
491490
let slf = py.from_borrowed_ptr::<crate::PyCell<T>>(slf);
492491
let arg = py.from_borrowed_ptr::<PyAny>(arg);
493492

src/class/buffer.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! c-api
77
use crate::err::PyResult;
88
use crate::gil::GILPool;
9-
use crate::{callback, ffi, run_callback, PyCell, PyClass, PyRefMut, Python};
9+
use crate::{callback, ffi, run_callback, PyCell, PyClass, PyRefMut};
1010
use std::os::raw::c_int;
1111

1212
/// Buffer protocol interface
@@ -91,9 +91,9 @@ where
9191
where
9292
T: for<'p> PyBufferGetBufferProtocol<'p>,
9393
{
94-
let py = Python::assume_gil_acquired();
94+
let pool = GILPool::new();
95+
let py = pool.py();
9596
run_callback(py, || {
96-
let _pool = GILPool::new(py);
9797
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);
9898
let result = T::bf_getbuffer(slf.try_borrow_mut()?, arg1, arg2).into();
9999
callback::convert(py, result)
@@ -126,9 +126,9 @@ where
126126
where
127127
T: for<'p> PyBufferReleaseBufferProtocol<'p>,
128128
{
129-
let py = Python::assume_gil_acquired();
129+
let pool = GILPool::new();
130+
let py = pool.py();
130131
run_callback(py, || {
131-
let _pool = GILPool::new(py);
132132
let slf = py.from_borrowed_ptr::<crate::PyCell<T>>(slf);
133133
let result = T::bf_releasebuffer(slf.try_borrow_mut()?, arg1).into();
134134
crate::callback::convert(py, result)

src/class/gc.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ where
8989
where
9090
T: for<'p> PyGCTraverseProtocol<'p>,
9191
{
92-
let py = Python::assume_gil_acquired();
93-
let _pool = crate::GILPool::new(py);
92+
let pool = crate::GILPool::new();
93+
let py = pool.py();
9494
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);
9595

9696
let visit = PyVisit {
9797
visit,
9898
arg,
9999
_py: py,
100100
};
101-
102-
if let Ok(borrow) = slf.try_borrow() {
101+
let borrow = slf.try_borrow();
102+
if let Ok(borrow) = borrow {
103103
match borrow.__traverse__(visit) {
104104
Ok(()) => 0,
105105
Err(PyTraverseError(code)) => code,
@@ -136,8 +136,8 @@ where
136136
where
137137
T: for<'p> PyGCClearProtocol<'p>,
138138
{
139-
let py = Python::assume_gil_acquired();
140-
let _pool = crate::GILPool::new(py);
139+
let pool = crate::GILPool::new();
140+
let py = pool.py();
141141
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);
142142

143143
slf.borrow_mut().__clear__();

src/class/macros.rs

+28-29
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ macro_rules! py_unary_func {
88
where
99
T: for<'p> $trait<'p>,
1010
{
11-
let py = $crate::Python::assume_gil_acquired();
11+
let pool = $crate::GILPool::new();
12+
let py = pool.py();
1213
$crate::run_callback(py, || {
13-
let _pool = $crate::GILPool::new(py);
1414
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
1515
$crate::callback::convert(py, $call!(slf, $f)$(.map($conv))?)
1616
})
@@ -34,9 +34,9 @@ macro_rules! py_unary_refmut_func {
3434
where
3535
T: for<'p> $trait<'p>,
3636
{
37-
let py = $crate::Python::assume_gil_acquired();
37+
let pool = $crate::GILPool::new();
38+
let py = pool.py();
3839
$crate::run_callback(py, || {
39-
let _pool = $crate::GILPool::new(py);
4040
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
4141
let res = $class::$f(slf.borrow_mut()).into();
4242
$crate::callback::convert(py, res $(.map($conv))?)
@@ -69,9 +69,9 @@ macro_rules! py_binary_func {
6969
T: for<'p> $trait<'p>,
7070
{
7171
use $crate::ObjectProtocol;
72-
let py = $crate::Python::assume_gil_acquired();
72+
let pool = $crate::GILPool::new();
73+
let py = pool.py();
7374
$crate::run_callback(py, || {
74-
let _pool = $crate::GILPool::new(py);
7575
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
7676
let arg = py.from_borrowed_ptr::<$crate::PyAny>(arg);
7777
$crate::callback::convert(py, $call!(slf, $f, arg)$(.map($conv))?)
@@ -99,9 +99,9 @@ macro_rules! py_binary_num_func {
9999
T: for<'p> $trait<'p>,
100100
{
101101
use $crate::ObjectProtocol;
102-
let py = $crate::Python::assume_gil_acquired();
102+
let pool = $crate::GILPool::new();
103+
let py = pool.py();
103104
$crate::run_callback(py, || {
104-
let _pool = $crate::GILPool::new(py);
105105
let lhs = py.from_borrowed_ptr::<$crate::PyAny>(lhs);
106106
let rhs = py.from_borrowed_ptr::<$crate::PyAny>(rhs);
107107

@@ -125,9 +125,9 @@ macro_rules! py_binary_reverse_num_func {
125125
T: for<'p> $trait<'p>,
126126
{
127127
use $crate::ObjectProtocol;
128-
let py = $crate::Python::assume_gil_acquired();
128+
let pool = $crate::GILPool::new();
129+
let py = pool.py();
129130
$crate::run_callback(py, || {
130-
let _pool = $crate::GILPool::new(py);
131131
// Swap lhs <-> rhs
132132
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(rhs);
133133
let arg = py.from_borrowed_ptr::<$crate::PyAny>(lhs);
@@ -155,9 +155,9 @@ macro_rules! py_binary_self_func {
155155
{
156156
use $crate::ObjectProtocol;
157157

158-
let py = $crate::Python::assume_gil_acquired();
158+
let pool = $crate::GILPool::new();
159+
let py = pool.py();
159160
$crate::run_callback(py, || {
160-
let _pool = $crate::GILPool::new(py);
161161
let slf_ = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
162162
let arg = py.from_borrowed_ptr::<$crate::PyAny>(arg);
163163
call_mut!(slf_, $f, arg)?;
@@ -184,9 +184,9 @@ macro_rules! py_ssizearg_func {
184184
where
185185
T: for<'p> $trait<'p>,
186186
{
187-
let py = $crate::Python::assume_gil_acquired();
187+
let pool = $crate::GILPool::new();
188+
let py = pool.py();
188189
$crate::run_callback(py, || {
189-
let _pool = $crate::GILPool::new(py);
190190
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
191191
$crate::callback::convert(py, $call!(slf, $f; arg.into()))
192192
})
@@ -209,9 +209,9 @@ macro_rules! py_ternary_func {
209209
{
210210
use $crate::ObjectProtocol;
211211

212-
let py = $crate::Python::assume_gil_acquired();
212+
let pool = $crate::GILPool::new();
213+
let py = pool.py();
213214
$crate::run_callback(py, || {
214-
let _pool = $crate::GILPool::new(py);
215215
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
216216
let arg1 = py
217217
.from_borrowed_ptr::<$crate::types::PyAny>(arg1)
@@ -245,9 +245,9 @@ macro_rules! py_ternary_num_func {
245245
{
246246
use $crate::ObjectProtocol;
247247

248-
let py = $crate::Python::assume_gil_acquired();
248+
let pool = $crate::GILPool::new();
249+
let py = pool.py();
249250
$crate::run_callback(py, || {
250-
let _pool = $crate::GILPool::new(py);
251251
let arg1 = py
252252
.from_borrowed_ptr::<$crate::types::PyAny>(arg1)
253253
.extract()?;
@@ -280,9 +280,9 @@ macro_rules! py_ternary_reverse_num_func {
280280
T: for<'p> $trait<'p>,
281281
{
282282
use $crate::ObjectProtocol;
283-
let py = $crate::Python::assume_gil_acquired();
283+
let pool = $crate::GILPool::new();
284+
let py = pool.py();
284285
$crate::run_callback(py, || {
285-
let _pool = $crate::GILPool::new(py);
286286
// Swap lhs <-> rhs
287287
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(arg2);
288288
let slf = slf.try_borrow()?;
@@ -312,9 +312,9 @@ macro_rules! py_dummy_ternary_self_func {
312312
{
313313
use $crate::ObjectProtocol;
314314

315-
let py = $crate::Python::assume_gil_acquired();
315+
let pool = $crate::GILPool::new();
316+
let py = pool.py();
316317
$crate::run_callback(py, || {
317-
let _pool = $crate::GILPool::new(py);
318318
let slf_cell = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
319319
let arg1 = py.from_borrowed_ptr::<$crate::PyAny>(arg1);
320320
call_mut!(slf_cell, $f, arg1)?;
@@ -338,9 +338,9 @@ macro_rules! py_func_set {
338338
{
339339
use $crate::ObjectProtocol;
340340

341-
let py = $crate::Python::assume_gil_acquired();
341+
let pool = $crate::GILPool::new();
342+
let py = pool.py();
342343
$crate::run_callback(py, || {
343-
let _pool = $crate::GILPool::new(py);
344344
let slf = py.from_borrowed_ptr::<$crate::PyCell<$generic>>(slf);
345345

346346
if value.is_null() {
@@ -374,10 +374,9 @@ macro_rules! py_func_del {
374374
{
375375
use $crate::ObjectProtocol;
376376

377-
let py = $crate::Python::assume_gil_acquired();
377+
let pool = $crate::GILPool::new();
378+
let py = pool.py();
378379
$crate::run_callback(py, || {
379-
let _pool = $crate::GILPool::new(py);
380-
381380
if value.is_null() {
382381
let slf = py.from_borrowed_ptr::<$crate::PyCell<U>>(slf);
383382
let name = py
@@ -408,9 +407,9 @@ macro_rules! py_func_set_del {
408407
{
409408
use $crate::ObjectProtocol;
410409

411-
let py = $crate::Python::assume_gil_acquired();
410+
let pool = $crate::GILPool::new();
411+
let py = pool.py();
412412
$crate::run_callback(py, || {
413-
let _pool = $crate::GILPool::new(py);
414413
let slf = py.from_borrowed_ptr::<$crate::PyCell<$generic>>(slf);
415414
let name = py.from_borrowed_ptr::<$crate::PyAny>(name);
416415

0 commit comments

Comments
 (0)