From e75f768ea824ae535cf92733c00c71b7936b13ab Mon Sep 17 00:00:00 2001 From: Sebastian Puetz Date: Mon, 20 Jul 2020 19:09:43 +0200 Subject: [PATCH] Move the ThreadChecker field in front of dict and weakref. Offsets for dict and weakref are calculated from the end of the PyCell struct. When using the non-dummy ThreadChecker, the offsets were invalid since the `ThreadCheckerImpl` is not zero-sized. --- CHANGELOG.md | 1 + src/pycell.rs | 2 +- tests/test_unsendable_dict.rs | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/test_unsendable_dict.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index fc8e0bc9..44ec446a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed - Conversion from types with an `__index__` method to Rust BigInts. [#1027](https://github.com/PyO3/pyo3/pull/1027) +- Fix segfault with #[pyclass(dict, unsendable)] [#1058](https://github.com/PyO3/pyo3/pull/1058) ## [0.11.1] - 2020-06-30 ### Added diff --git a/src/pycell.rs b/src/pycell.rs index 64efea72..94ac6192 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -161,9 +161,9 @@ impl PyCellInner { #[repr(C)] pub struct PyCell { inner: PyCellInner, + thread_checker: T::ThreadChecker, dict: T::Dict, weakref: T::WeakRef, - thread_checker: T::ThreadChecker, } unsafe impl PyNativeType for PyCell {} diff --git a/tests/test_unsendable_dict.rs b/tests/test_unsendable_dict.rs new file mode 100644 index 00000000..7c728e64 --- /dev/null +++ b/tests/test_unsendable_dict.rs @@ -0,0 +1,21 @@ +use pyo3::prelude::*; +use pyo3::py_run; + +#[pyclass(dict, unsendable)] +struct UnsendableDictClass {} + +#[pymethods] +impl UnsendableDictClass { + #[new] + fn new() -> Self { + UnsendableDictClass {} + } +} + +#[test] +fn test_unsendable_dict() { + let gil = Python::acquire_gil(); + let py = gil.python(); + let inst = Py::new(py, UnsendableDictClass {}).unwrap(); + py_run!(py, inst, "assert inst.__dict__ == {}"); +}