Skip to content

Commit 3228b4c

Browse files
authored
Merge pull request #585 from andersk/drain-gil
Require the GIL to be held in ReleasePool::drain
2 parents 305b774 + e70e9ab commit 3228b4c

File tree

14 files changed

+54
-57
lines changed

14 files changed

+54
-57
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2929
* Implementing the Using the `gc` parameter for `pyclass` (e.g. `#[pyclass(gc)]`) without implementing the `class::PyGCProtocol` trait is now a compile-time error. Failing to implement this trait could lead to segfaults. [#532](https://github.com/PyO3/pyo3/pull/532)
3030
* `PyByteArray::data` has been replaced with `PyDataArray::to_vec` because returning a `&[u8]` is unsound. (See [this comment](https://github.com/PyO3/pyo3/issues/373#issuecomment-512332696) for a great write-up for why that was unsound)
3131
* Replace `mashup` with `paste`.
32+
* `GILPool` gained a `Python` marker to prevent it from being misused to release Python objects without the GIL held.
3233

3334
## [0.7.0] - 2018-05-26
3435

pyo3-derive-backend/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ fn function_c_wrapper(name: &Ident, spec: &method::FnSpec<'_>) -> TokenStream {
204204
{
205205
const _LOCATION: &'static str = concat!(stringify!(#name), "()");
206206

207-
let _pool = pyo3::GILPool::new();
208207
let _py = pyo3::Python::assume_gil_acquired();
208+
let _pool = pyo3::GILPool::new(_py);
209209
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
210210
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
211211

pyo3-derive-backend/src/pymethod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ fn impl_wrap_common(
113113
{
114114
const _LOCATION: &'static str = concat!(
115115
stringify!(#cls), ".", stringify!(#name), "()");
116-
let _pool = pyo3::GILPool::new();
117116
let _py = pyo3::Python::assume_gil_acquired();
117+
let _pool = pyo3::GILPool::new(_py);
118118
#slf
119119
let _result = {
120120
pyo3::derive_utils::IntoPyResult::into_py_result(#body)
@@ -135,8 +135,8 @@ fn impl_wrap_common(
135135
{
136136
const _LOCATION: &'static str = concat!(
137137
stringify!(#cls), ".", stringify!(#name), "()");
138-
let _pool = pyo3::GILPool::new();
139138
let _py = pyo3::Python::assume_gil_acquired();
139+
let _pool = pyo3::GILPool::new(_py);
140140
#slf
141141
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
142142
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
@@ -163,8 +163,8 @@ pub fn impl_proto_wrap(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec<'_>) ->
163163
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
164164
{
165165
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
166-
let _pool = pyo3::GILPool::new();
167166
let _py = pyo3::Python::assume_gil_acquired();
167+
let _pool = pyo3::GILPool::new(_py);
168168
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
169169
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
170170
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
@@ -194,8 +194,8 @@ pub fn impl_wrap_new(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec<'_>) -> T
194194
use pyo3::type_object::PyTypeInfo;
195195

196196
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
197-
let _pool = pyo3::GILPool::new();
198197
let _py = pyo3::Python::assume_gil_acquired();
198+
let _pool = pyo3::GILPool::new(_py);
199199
match pyo3::type_object::PyRawObject::new(_py, #cls::type_object(), _cls) {
200200
Ok(_obj) => {
201201
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
@@ -240,8 +240,8 @@ fn impl_wrap_init(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec<'_>) -> Toke
240240
_kwargs: *mut pyo3::ffi::PyObject) -> pyo3::libc::c_int
241241
{
242242
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
243-
let _pool = pyo3::GILPool::new();
244243
let _py = pyo3::Python::assume_gil_acquired();
244+
let _pool = pyo3::GILPool::new(_py);
245245
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
246246
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
247247
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
@@ -274,8 +274,8 @@ pub fn impl_wrap_class(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec<'_>) ->
274274
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
275275
{
276276
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
277-
let _pool = pyo3::GILPool::new();
278277
let _py = pyo3::Python::assume_gil_acquired();
278+
let _pool = pyo3::GILPool::new(_py);
279279
let _cls = pyo3::types::PyType::from_type_ptr(_py, _cls as *mut pyo3::ffi::PyTypeObject);
280280
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
281281
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
@@ -303,8 +303,8 @@ pub fn impl_wrap_static(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec<'_>) -
303303
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
304304
{
305305
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
306-
let _pool = pyo3::GILPool::new();
307306
let _py = pyo3::Python::assume_gil_acquired();
307+
let _pool = pyo3::GILPool::new(_py);
308308
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
309309
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
310310

@@ -329,8 +329,8 @@ pub(crate) fn impl_wrap_getter(cls: &syn::Type, name: &syn::Ident, takes_py: boo
329329
{
330330
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
331331

332-
let _pool = pyo3::GILPool::new();
333332
let _py = pyo3::Python::assume_gil_acquired();
333+
let _pool = pyo3::GILPool::new(_py);
334334
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
335335

336336
let result = pyo3::derive_utils::IntoPyResult::into_py_result(#fncall);
@@ -370,8 +370,8 @@ pub(crate) fn impl_wrap_setter(
370370
_value: *mut pyo3::ffi::PyObject, _: *mut ::std::os::raw::c_void) -> pyo3::libc::c_int
371371
{
372372
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
373-
let _pool = pyo3::GILPool::new();
374373
let _py = pyo3::Python::assume_gil_acquired();
374+
let _pool = pyo3::GILPool::new(_py);
375375
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
376376
let _value = _py.from_borrowed_ptr(_value);
377377

src/class/basic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ where
215215
where
216216
T: for<'p> PyObjectGetAttrProtocol<'p>,
217217
{
218-
let _pool = crate::GILPool::new();
219218
let py = Python::assume_gil_acquired();
219+
let _pool = crate::GILPool::new(py);
220220

221221
// Behave like python's __getattr__ (as opposed to __getattribute__) and check
222222
// for existing fields and methods first
@@ -450,8 +450,8 @@ where
450450
where
451451
T: for<'p> PyObjectRichcmpProtocol<'p>,
452452
{
453-
let _pool = crate::GILPool::new();
454453
let py = Python::assume_gil_acquired();
454+
let _pool = crate::GILPool::new(py);
455455
let slf = py.from_borrowed_ptr::<T>(slf);
456456
let arg = py.from_borrowed_ptr::<PyAny>(arg);
457457

src/class/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ where
8585
where
8686
T: for<'p> PyBufferGetBufferProtocol<'p>,
8787
{
88-
let _pool = crate::GILPool::new();
8988
let py = crate::Python::assume_gil_acquired();
89+
let _pool = crate::GILPool::new(py);
9090
let slf = py.mut_from_borrowed_ptr::<T>(slf);
9191

9292
let result = slf.bf_getbuffer(arg1, arg2).into();

src/class/gc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ where
8585
where
8686
T: for<'p> PyGCTraverseProtocol<'p>,
8787
{
88-
let _pool = crate::GILPool::new();
8988
let py = Python::assume_gil_acquired();
89+
let _pool = crate::GILPool::new(py);
9090
let slf = py.mut_from_borrowed_ptr::<T>(slf);
9191

9292
let visit = PyVisit {
@@ -122,8 +122,8 @@ where
122122
where
123123
T: for<'p> PyGCClearProtocol<'p>,
124124
{
125-
let _pool = crate::GILPool::new();
126125
let py = Python::assume_gil_acquired();
126+
let _pool = crate::GILPool::new(py);
127127
let slf = py.mut_from_borrowed_ptr::<T>(slf);
128128

129129
slf.__clear__();

src/class/macros.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ macro_rules! py_unary_func {
1717
where
1818
T: for<'p> $trait<'p>,
1919
{
20-
let _pool = $crate::GILPool::new();
2120
let py = $crate::Python::assume_gil_acquired();
21+
let _pool = $crate::GILPool::new(py);
2222
let slf = py.mut_from_borrowed_ptr::<T>(slf);
2323
let res = slf.$f().into();
2424
$crate::callback::cb_convert($conv, py, res.map(|x| x))
@@ -36,8 +36,8 @@ macro_rules! py_unary_pyref_func {
3636
T: for<'p> $trait<'p>,
3737
{
3838
use $crate::instance::PyRefMut;
39-
let _pool = $crate::GILPool::new();
4039
let py = $crate::Python::assume_gil_acquired();
40+
let _pool = $crate::GILPool::new(py);
4141
let slf = py.mut_from_borrowed_ptr::<T>(slf);
4242
let res = $class::$f(PyRefMut::from_mut(slf)).into();
4343
$crate::callback::cb_convert($conv, py, res)
@@ -54,8 +54,8 @@ macro_rules! py_len_func {
5454
where
5555
T: for<'p> $trait<'p>,
5656
{
57-
let _pool = $crate::GILPool::new();
5857
let py = Python::assume_gil_acquired();
58+
let _pool = $crate::GILPool::new(py);
5959
let slf = py.mut_from_borrowed_ptr::<T>(slf);
6060

6161
let result = slf.$f().into();
@@ -84,8 +84,8 @@ macro_rules! py_binary_func {
8484
T: for<'p> $trait<'p>,
8585
{
8686
use $crate::ObjectProtocol;
87-
let _pool = $crate::GILPool::new();
8887
let py = $crate::Python::assume_gil_acquired();
88+
let _pool = $crate::GILPool::new(py);
8989
let slf = py.mut_from_borrowed_ptr::<T>(slf);
9090
let arg = py.from_borrowed_ptr::<$crate::types::PyAny>(arg);
9191

@@ -112,8 +112,8 @@ macro_rules! py_binary_num_func {
112112
T: for<'p> $trait<'p>,
113113
{
114114
use $crate::ObjectProtocol;
115-
let _pool = $crate::GILPool::new();
116115
let py = $crate::Python::assume_gil_acquired();
116+
let _pool = $crate::GILPool::new(py);
117117
let lhs = py.from_borrowed_ptr::<$crate::types::PyAny>(lhs);
118118
let rhs = py.from_borrowed_ptr::<$crate::types::PyAny>(rhs);
119119

@@ -144,8 +144,8 @@ macro_rules! py_binary_self_func {
144144
{
145145
use $crate::ObjectProtocol;
146146

147-
let _pool = $crate::GILPool::new();
148147
let py = $crate::Python::assume_gil_acquired();
148+
let _pool = $crate::GILPool::new(py);
149149
let slf1 = py.mut_from_borrowed_ptr::<T>(slf);
150150
let arg = py.from_borrowed_ptr::<$crate::types::PyAny>(arg);
151151

@@ -180,8 +180,8 @@ macro_rules! py_ssizearg_func {
180180
where
181181
T: for<'p> $trait<'p>,
182182
{
183-
let _pool = $crate::GILPool::new();
184183
let py = $crate::Python::assume_gil_acquired();
184+
let _pool = $crate::GILPool::new(py);
185185
let slf = py.mut_from_borrowed_ptr::<T>(slf);
186186
let result = slf.$f(arg.into()).into();
187187
$crate::callback::cb_convert($conv, py, result)
@@ -213,8 +213,8 @@ macro_rules! py_ternary_func {
213213
{
214214
use $crate::ObjectProtocol;
215215

216-
let _pool = $crate::GILPool::new();
217216
let py = $crate::Python::assume_gil_acquired();
217+
let _pool = $crate::GILPool::new(py);
218218
let slf = py.mut_from_borrowed_ptr::<T>(slf);
219219
let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1);
220220
let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2);
@@ -247,8 +247,8 @@ macro_rules! py_ternary_num_func {
247247
{
248248
use $crate::ObjectProtocol;
249249

250-
let _pool = $crate::GILPool::new();
251250
let py = $crate::Python::assume_gil_acquired();
251+
let _pool = $crate::GILPool::new(py);
252252
let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1);
253253
let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2);
254254
let arg3 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg3);
@@ -284,8 +284,8 @@ macro_rules! py_ternary_self_func {
284284
{
285285
use $crate::ObjectProtocol;
286286

287-
let _pool = $crate::GILPool::new();
288287
let py = $crate::Python::assume_gil_acquired();
288+
let _pool = $crate::GILPool::new(py);
289289
let slf1 = py.mut_from_borrowed_ptr::<T>(slf);
290290
let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1);
291291
let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2);
@@ -323,8 +323,8 @@ macro_rules! py_func_set {
323323
{
324324
use $crate::ObjectProtocol;
325325

326-
let _pool = $crate::GILPool::new();
327326
let py = $crate::Python::assume_gil_acquired();
327+
let _pool = $crate::GILPool::new(py);
328328
let slf = py.mut_from_borrowed_ptr::<$generic>(slf);
329329

330330
let result = if value.is_null() {
@@ -371,8 +371,8 @@ macro_rules! py_func_del {
371371
{
372372
use $crate::ObjectProtocol;
373373

374-
let _pool = $crate::GILPool::new();
375374
let py = $crate::Python::assume_gil_acquired();
375+
let _pool = $crate::GILPool::new(py);
376376

377377
let result = if value.is_null() {
378378
let slf = py.mut_from_borrowed_ptr::<U>(slf);
@@ -413,8 +413,8 @@ macro_rules! py_func_set_del {
413413
{
414414
use $crate::ObjectProtocol;
415415

416-
let _pool = $crate::GILPool::new();
417416
let py = $crate::Python::assume_gil_acquired();
417+
let _pool = $crate::GILPool::new(py);
418418
let slf = py.mut_from_borrowed_ptr::<$generic>(slf);
419419
let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name);
420420

src/class/sequence.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ where
221221
where
222222
T: for<'p> PySequenceSetItemProtocol<'p>,
223223
{
224-
let _pool = crate::GILPool::new();
225224
let py = Python::assume_gil_acquired();
225+
let _pool = crate::GILPool::new(py);
226226
let slf = py.mut_from_borrowed_ptr::<T>(slf);
227227

228228
let result = if value.is_null() {
@@ -295,8 +295,8 @@ mod sq_ass_item_impl {
295295
where
296296
T: for<'p> PySequenceDelItemProtocol<'p>,
297297
{
298-
let _pool = crate::GILPool::new();
299298
let py = Python::assume_gil_acquired();
299+
let _pool = crate::GILPool::new(py);
300300
let slf = py.mut_from_borrowed_ptr::<T>(slf);
301301

302302
let result = if value.is_null() {
@@ -341,8 +341,8 @@ mod sq_ass_item_impl {
341341
where
342342
T: for<'p> PySequenceSetItemProtocol<'p> + for<'p> PySequenceDelItemProtocol<'p>,
343343
{
344-
let _pool = crate::GILPool::new();
345344
let py = Python::assume_gil_acquired();
345+
let _pool = crate::GILPool::new(py);
346346
let slf = py.mut_from_borrowed_ptr::<T>(slf);
347347

348348
let result = if value.is_null() {

src/derive_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ pub unsafe fn make_module(
139139
return module;
140140
}
141141

142-
let _pool = GILPool::new();
143142
let py = Python::assume_gil_acquired();
143+
let _pool = GILPool::new(py);
144144
let module = match py.from_owned_ptr_or_err::<PyModule>(module) {
145145
Ok(m) => m,
146146
Err(e) => {

0 commit comments

Comments
 (0)