Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bound 'py lifetime by GILPool in the internal usecases #869

Merged
merged 2 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed

* `PyObject` and `Py<T>` reference counts are now decremented sooner after `drop()`. [#851](https://github.com/PyO3/pyo3/pull/851)
* When the GIL is held, the refcount is now decreased immediately on drop. (Previously would wait until just before releasing the GIL.)
* 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.)
* When the GIL is held, the refcount is now decreased immediately on drop. (Previously would wait until just before releasing the GIL.)
* 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.)

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

## [0.9.2]

Expand Down
4 changes: 2 additions & 2 deletions pyo3-derive-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ fn function_c_wrapper(name: &Ident, spec: &method::FnSpec<'_>) -> TokenStream {
{
const _LOCATION: &'static str = concat!(stringify!(#name), "()");

let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);

Expand Down
32 changes: 16 additions & 16 deletions pyo3-derive-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ fn impl_wrap_common(
{
const _LOCATION: &'static str = concat!(
stringify!(#cls), ".", stringify!(#python_name), "()");
let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
#slf
pyo3::callback::convert(_py, #body)
})
Expand All @@ -124,9 +124,9 @@ fn impl_wrap_common(
{
const _LOCATION: &'static str = concat!(
stringify!(#cls), ".", stringify!(#python_name), "()");
let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
#slf
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
Expand Down Expand Up @@ -155,9 +155,9 @@ pub fn impl_proto_wrap(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
let _slf = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
#borrow_self
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
Expand Down Expand Up @@ -193,9 +193,9 @@ pub fn impl_wrap_new(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
use pyo3::type_object::PyTypeInfo;

const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);

Expand Down Expand Up @@ -225,9 +225,9 @@ pub fn impl_wrap_class(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
let _cls = pyo3::types::PyType::from_type_ptr(_py, _cls as *mut pyo3::ffi::PyTypeObject);
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);
Expand Down Expand Up @@ -257,9 +257,9 @@ pub fn impl_wrap_static(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
_kwargs: *mut pyo3::ffi::PyObject) -> *mut pyo3::ffi::PyObject
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
let _args = _py.from_borrowed_ptr::<pyo3::types::PyTuple>(_args);
let _kwargs: Option<&pyo3::types::PyDict> = _py.from_borrowed_ptr_or_opt(_kwargs);

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

let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
let _slf = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
#borrow_self
pyo3::callback::convert(_py, #getter_impl)
Expand Down Expand Up @@ -372,9 +372,9 @@ pub(crate) fn impl_wrap_setter(
_value: *mut pyo3::ffi::PyObject, _: *mut ::std::os::raw::c_void) -> pyo3::libc::c_int
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
let _py = pyo3::Python::assume_gil_acquired();
let _pool = pyo3::GILPool::new();
let _py = _pool.python();
pyo3::run_callback(_py, || {
let _pool = pyo3::GILPool::new(_py);
let _slf = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
#borrow_self
let _value = _py.from_borrowed_ptr::<pyo3::types::PyAny>(_value);
Expand Down
11 changes: 5 additions & 6 deletions src/class/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::callback::HashCallbackOutput;
use crate::class::methods::PyMethodDef;
use crate::{
callback, exceptions, ffi, run_callback, FromPyObject, GILPool, IntoPy, ObjectProtocol, PyAny,
PyCell, PyClass, PyErr, PyObject, PyResult, Python,
PyCell, PyClass, PyErr, PyObject, PyResult,
};
use std::os::raw::c_int;

Expand Down Expand Up @@ -218,10 +218,9 @@ where
where
T: for<'p> PyObjectGetAttrProtocol<'p>,
{
let py = Python::assume_gil_acquired();
let pool = GILPool::new();
let py = pool.python();
run_callback(py, || {
let _pool = GILPool::new(py);

// Behave like python's __getattr__ (as opposed to __getattribute__) and check
// for existing fields and methods first
let existing = ffi::PyObject_GenericGetAttr(slf, arg);
Expand Down Expand Up @@ -485,9 +484,9 @@ where
where
T: for<'p> PyObjectRichcmpProtocol<'p>,
{
let py = Python::assume_gil_acquired();
let pool = GILPool::new();
let py = pool.python();
run_callback(py, || {
let _pool = GILPool::new(py);
let slf = py.from_borrowed_ptr::<crate::PyCell<T>>(slf);
let arg = py.from_borrowed_ptr::<PyAny>(arg);

Expand Down
10 changes: 5 additions & 5 deletions src/class/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! c-api
use crate::err::PyResult;
use crate::gil::GILPool;
use crate::{callback, ffi, run_callback, PyCell, PyClass, PyRefMut, Python};
use crate::{callback, ffi, run_callback, PyCell, PyClass, PyRefMut};
use std::os::raw::c_int;

/// Buffer protocol interface
Expand Down Expand Up @@ -91,9 +91,9 @@ where
where
T: for<'p> PyBufferGetBufferProtocol<'p>,
{
let py = Python::assume_gil_acquired();
let pool = GILPool::new();
let py = pool.python();
run_callback(py, || {
let _pool = GILPool::new(py);
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);
let result = T::bf_getbuffer(slf.try_borrow_mut()?, arg1, arg2).into();
callback::convert(py, result)
Expand Down Expand Up @@ -126,9 +126,9 @@ where
where
T: for<'p> PyBufferReleaseBufferProtocol<'p>,
{
let py = Python::assume_gil_acquired();
let pool = GILPool::new();
let py = pool.python();
run_callback(py, || {
let _pool = GILPool::new(py);
let slf = py.from_borrowed_ptr::<crate::PyCell<T>>(slf);
let result = T::bf_releasebuffer(slf.try_borrow_mut()?, arg1).into();
crate::callback::convert(py, result)
Expand Down
12 changes: 6 additions & 6 deletions src/class/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ where
where
T: for<'p> PyGCTraverseProtocol<'p>,
{
let py = Python::assume_gil_acquired();
let _pool = crate::GILPool::new(py);
let pool = crate::GILPool::new();
let py = pool.python();
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);

let visit = PyVisit {
visit,
arg,
_py: py,
};

if let Ok(borrow) = slf.try_borrow() {
let borrow = slf.try_borrow();
if let Ok(borrow) = borrow {
match borrow.__traverse__(visit) {
Ok(()) => 0,
Err(PyTraverseError(code)) => code,
Expand Down Expand Up @@ -136,8 +136,8 @@ where
where
T: for<'p> PyGCClearProtocol<'p>,
{
let py = Python::assume_gil_acquired();
let _pool = crate::GILPool::new(py);
let pool = crate::GILPool::new();
let py = pool.python();
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);

slf.borrow_mut().__clear__();
Expand Down
57 changes: 28 additions & 29 deletions src/class/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ macro_rules! py_unary_func {
where
T: for<'p> $trait<'p>,
{
let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
$crate::callback::convert(py, $call!(slf, $f)$(.map($conv))?)
})
Expand All @@ -34,9 +34,9 @@ macro_rules! py_unary_refmut_func {
where
T: for<'p> $trait<'p>,
{
let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let res = $class::$f(slf.borrow_mut()).into();
$crate::callback::convert(py, res $(.map($conv))?)
Expand Down Expand Up @@ -69,9 +69,9 @@ macro_rules! py_binary_func {
T: for<'p> $trait<'p>,
{
use $crate::ObjectProtocol;
let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg = py.from_borrowed_ptr::<$crate::PyAny>(arg);
$crate::callback::convert(py, $call!(slf, $f, arg)$(.map($conv))?)
Expand Down Expand Up @@ -99,9 +99,9 @@ macro_rules! py_binary_num_func {
T: for<'p> $trait<'p>,
{
use $crate::ObjectProtocol;
let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let lhs = py.from_borrowed_ptr::<$crate::PyAny>(lhs);
let rhs = py.from_borrowed_ptr::<$crate::PyAny>(rhs);

Expand All @@ -125,9 +125,9 @@ macro_rules! py_binary_reverse_num_func {
T: for<'p> $trait<'p>,
{
use $crate::ObjectProtocol;
let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
// Swap lhs <-> rhs
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(rhs);
let arg = py.from_borrowed_ptr::<$crate::PyAny>(lhs);
Expand Down Expand Up @@ -155,9 +155,9 @@ macro_rules! py_binary_self_func {
{
use $crate::ObjectProtocol;

let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf_ = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg = py.from_borrowed_ptr::<$crate::PyAny>(arg);
call_mut!(slf_, $f, arg)?;
Expand All @@ -184,9 +184,9 @@ macro_rules! py_ssizearg_func {
where
T: for<'p> $trait<'p>,
{
let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
$crate::callback::convert(py, $call!(slf, $f; arg.into()))
})
Expand All @@ -209,9 +209,9 @@ macro_rules! py_ternary_func {
{
use $crate::ObjectProtocol;

let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg1 = py
.from_borrowed_ptr::<$crate::types::PyAny>(arg1)
Expand Down Expand Up @@ -245,9 +245,9 @@ macro_rules! py_ternary_num_func {
{
use $crate::ObjectProtocol;

let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let arg1 = py
.from_borrowed_ptr::<$crate::types::PyAny>(arg1)
.extract()?;
Expand Down Expand Up @@ -280,9 +280,9 @@ macro_rules! py_ternary_reverse_num_func {
T: for<'p> $trait<'p>,
{
use $crate::ObjectProtocol;
let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
// Swap lhs <-> rhs
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(arg2);
let slf = slf.try_borrow()?;
Expand Down Expand Up @@ -312,9 +312,9 @@ macro_rules! py_dummy_ternary_self_func {
{
use $crate::ObjectProtocol;

let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf_cell = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg1 = py.from_borrowed_ptr::<$crate::PyAny>(arg1);
call_mut!(slf_cell, $f, arg1)?;
Expand All @@ -338,9 +338,9 @@ macro_rules! py_func_set {
{
use $crate::ObjectProtocol;

let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<$generic>>(slf);

if value.is_null() {
Expand Down Expand Up @@ -374,10 +374,9 @@ macro_rules! py_func_del {
{
use $crate::ObjectProtocol;

let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);

if value.is_null() {
let slf = py.from_borrowed_ptr::<$crate::PyCell<U>>(slf);
let name = py
Expand Down Expand Up @@ -408,9 +407,9 @@ macro_rules! py_func_set_del {
{
use $crate::ObjectProtocol;

let py = $crate::Python::assume_gil_acquired();
let pool = $crate::GILPool::new();
let py = pool.python();
$crate::run_callback(py, || {
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<$generic>>(slf);
let name = py.from_borrowed_ptr::<$crate::PyAny>(name);

Expand Down
Loading