Commit Graph

5258 Commits

Author SHA1 Message Date
Adam Reichold 426e18932b Simplify version handling of UI tests.
Due to checking in error outputs these tests only work reliably on stable and
not on intermediate version between our MSRV (currently 1.48) and the current
stable version.

Hence this simplifies things to run only MSRV-compatible tests for the MSRV
builds, anything else for stable builds except for those tests which require the
nightly feature, i.e. the `Ungil` being distinct from the `Send` trait.

Finally, `not_send3` is disabled when using the nightly feature since while `Rc`
is not send, it also not GIL-bound and hence can be passed into `allow_threads`
as it does not actually spawn a new thread.
2023-05-25 21:34:43 +02:00
bors[bot] 32c335e072
Merge #3168 #3176
3168: Do not apply deferred ref count updates and prevent the GIL from being acquired inside of __traverse__ implementations. r=davidhewitt a=adamreichold

Closes #2301
Closes #3165


3176: Prevent dropping unsendable classes on other threads. r=davidhewitt a=adamreichold

Continuing the discussed from https://github.com/PyO3/pyo3/pull/3169#issuecomment-1556571504 and https://github.com/PyO3/pyo3/pull/3169#issuecomment-1556661723:

We already have checks in place to avoid borrowing these classes on other threads but it was still possible to send them to another thread and drop them there (while holding the GIL).
    
This change avoids running the `Drop` implementation in such a case even though Python will still free the underlying memory. This might leak resources owned by the object, but it avoids undefined behaviour due to access the unsendable type from another thread.
    
This does assume that the object was not unsafely integrated into an intrusive data structures which still point to the now freed memory. In that case, the only recourse would be to abort the process as freeing the memory is unavoidable when the tp_dealloc slot is called. (And moving it elsewhere into a new allocation would still break any existing pointers.)

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-05-25 18:18:46 +00:00
Adam Reichold e85bfcc3bf Factor out UnraisableCapture helper type and use it to check that dropping unsendable elsewhere calls into sys.unraisablehook 2023-05-25 20:17:55 +02:00
Adam Reichold 08bdc32b6e Move traverse-bare-self UI test to version-gated section as its error output is not stable between MSRV and current stable. 2023-05-25 20:02:51 +02:00
Adam Reichold 7d4d3ca364 Move the check for a locked GIL into a cold function and closer to the locking. 2023-05-25 20:01:35 +02:00
Adam Reichold 18397828e5 Move locking the GIL and handling the resulting panics into a library function. 2023-05-25 20:01:35 +02:00
Adam Reichold 810ad00a76 Do not apply deferred ref count updates and prevent the GIL from being acquired inside of implementations. 2023-05-25 20:01:35 +02:00
Adam Reichold 501ff8a17d Prevent dropping unsendable classes on other threads.
We already have checks in place to avoid borrowing these classes on other
threads but it was still possible to send them to another thread and drop them
there (while holding the GIL).

This change avoids running the `Drop` implementation in such a case even though
Python will still free the underlying memory. This might leak resources owned by
the object, but it avoids undefined behaviour due to access the unsendable type
from another thread.

This does assume that the object was not unsafely integrated into an intrusive
data structures which still point to the now freed memory. In that case, the
only recourse would be to abort the process as freeing the memory is unavoidable
when the tp_dealloc slot is called. (And moving it elsewhere into a new
allocation would still break any existing pointers.)
2023-05-25 19:10:42 +02:00
bors[bot] d71af73456
Merge #3181
3181: Upgrade dependencies for pyo3-ffi-check r=adamreichold a=alex



Co-authored-by: Alex Gaynor <alex.gaynor@gmail.com>
2023-05-24 13:18:17 +00:00
Alex Gaynor df2cf498fb
Upgrade memoffset for pyo3-ffi-check 2023-05-24 09:09:11 -04:00
bors[bot] dfc667fd0e
Merge #3177
3177: RFC: Use a const initializer for `GIL_COUNT` if possible r=adamreichold a=lifthrasiir

Normally [`LocalKey::try_with`](https://doc.rust-lang.org/stable/std/thread/struct.LocalKey.html#method.try_with) needs to check for the initialization flag to lazily initialize the TLS. This behaves badly with a compiler optimization and it seems that multiple calls to `gil_is_acquired()` cannot be correctly eliminated. Rust 1.59 added a `const { ... }` initializer (a special form only allowed here for now) in `thread_local!` which removes the initialization flag, allowing those kind of optimizations.

I should note that the performance impact is probably minimal, because the check branch is extremely well predicted and the optimization is only possible when every PyO3 code that leads to `gil_is_acquired()` is inlined, a pretty uncommon situation. I couldn't demonstrate any consistent improvement or regression from my machine, which performance seems to be flaky as well. But at least we would have one less branch there. I'll leave this as an RFC so that someone else can prove or disprove that this is indeed beneficial.

Co-authored-by: Kang Seonghoon <public+git@mearie.org>
2023-05-24 06:24:12 +00:00
Kang Seonghoon 291fc36d0c Use a const initializer for `OWNED_OBJECTS` if possible.
Since `Vec::with_capacity` is not a const function, it now does not
allocate any initial memory.
2023-05-24 10:42:07 +09:00
bors[bot] 3a773c7f91
Merge #3166
3166: RFC: Drop EnsureGIL to remove one layer of indirection from the implementation of Python::with_gil r=davidhewitt a=adamreichold

I am not sure if other people would consider this a simplification as well, but it helped me to remove one layer of indirection used by the implementation of `Python::with_gil`, especially flattening the `Option<_>` layers in `EnsureGIL` and `GILGuard::pool`. This makes it obvious that we always create `GILPool` when we actually acquire the GIL. (Of course, there still might be extra `GILPool` instances created manually.)

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-05-23 21:35:00 +00:00
Adam Reichold 62f424b690 Drop EnsureGIL to remove one layer of indirection from the implementation of Python::with_gil 2023-05-23 23:31:47 +02:00
bors[bot] 408bf11fe0
Merge #3178
3178: Fix a bad span of `_slf` for custom receivers in `#[pymethods]`. r=adamreichold a=lifthrasiir

This turned out to be a remnant of #1506. It notably resulted in a very confusing error:

```
error[E0308]: mismatched types
    --> tests\test_methods.rs:1456:9
     |
1456 |           #[pymethods]
     |           ^^^^^^^^^^^^
     |           |
     |           expected `Py<Issue1506>`, found `*mut PyObject`
     |           arguments to this function are incorrect
...
1461 | / issue_1506!(
1462 | |     #[pymethods]
1463 | |     impl Issue1506 {
1464 | |         fn issue_1506(
...    |
1536 | |     }
1537 | | );
     | |_- in this macro invocation
     |
     = note:   expected struct `pyo3::Py<Issue1506>`
             found raw pointer `*mut pyo3::ffi::PyObject`
```

The actual cause is that `SelfType::receiver` is entirely ignored in this case and `_slf` refers to the original argument, but it sounds like that `TryFrom::try_from` somehow resulted in `*mut PyObject`...

Co-authored-by: Kang Seonghoon <public+git@mearie.org>
2023-05-23 05:41:53 +00:00
Kang Seonghoon 24343f2caa Fix a span site of `_slf` for custom receivers. 2023-05-23 10:25:57 +09:00
Kang Seonghoon dee45d3644 Use a const initializer for `GIL_COUNT` if possible. 2023-05-22 11:31:20 +09:00
bors[bot] a3c4fd2fa0
Merge #3161
3161: add PyAny::is_exact_instance and PyAny::is_exact_instance_of r=adamreichold a=davidhewitt

Split off from #2881. I'll rebase after that merges.

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-05-21 10:46:06 +00:00
David Hewitt 82a879ff6b add `PyAny::is_exact_instance` and `PyAny::is_exact_instance_of` 2023-05-21 12:44:54 +02:00
bors[bot] cc6f9d7837
Merge #3170
3170: Update memoffset to 0.9 r=adamreichold a=est31

Updates memoffset to version 0.9

Co-authored-by: est31 <MTest31@outlook.com>
2023-05-21 08:34:29 +00:00
est31 6fcc790f8b Update memoffset to 0.9 2023-05-21 09:56:55 +02:00
bors[bot] 3ec966d97f
Merge #2881
2881: Rework `PyAny::is_instance_of` for performance r=adamreichold a=davidhewitt

I was talking to `@samuelcolvin` about the fastest way to identify object types (relevant e.g. for `pythonize` and also `pydantic-core`) and noticed that `PyAny::is_instance_of` is quite unoptimised because it expands to an ffi call to `PyObject_IsInstance`.

This PR proposes `PyAny::is_instance_of::<T>(obj)` is changed to be equivalent to `T::is_type_of(obj)`, plus add a sprinkling of inlining. We often have implementations such as `PyDict_Check` which can pretty much be optimised away to just checking a bit on the type object.

The accompanying benchmark to run through a bunch of object types is approx 40% faster after making this change.

For completeness, I've also added `PyAny::is_exact_instance` and `PyAny::is_exact_instance_of`, to pair with `T::is_exact_type_of`. (This could be split into a separate PR if preferred.)



Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-05-19 06:35:36 +00:00
David Hewitt 248230b8e4 refactor PyAny::is_instance_of for performance 2023-05-18 22:25:27 +01:00
David Hewitt 941ee18d22 add benchmark for isinstance chain 2023-05-18 22:10:45 +01:00
bors[bot] c2986dfccf
Merge #3158
3158: Add Py::get for GIL-independent access to frozen classes. r=davidhewitt a=adamreichold

`@davidhewitt` Is this what you had in mind for #3154?

The name is an obvious candidate for bikeshedding.

Trying to write an example, I noticed that making `PyCell::get_frozen` public is most likely not useful as there is no way to safely get a `&PyCell` without acquiring the GIL first?

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-05-18 07:46:50 +00:00
Adam Reichold b9766cfa11 Add PyClass::get and Py::get for GIL-independent access to frozen classes. 2023-05-18 08:57:55 +02:00
bors[bot] 3b4c7d38c7
Merge #3157
3157: Add support for `#[new]` which is also a `#[classmethod]` r=davidhewitt a=stuhood

Fixes #3077.

Co-authored-by: Stu Hood <stuhood@gmail.com>
2023-05-17 21:58:44 +00:00
bors[bot] 0f0024026b
Merge #3159
3159: Add OpenDAL to examples r=adamreichold a=suyanhanx

Hi there,

We have been using PyO3 to build our project's python binding and have been extremely satisfied with it.

And we want to say thank you for providing such a wonderful tool.




Co-authored-by: suyanhanx <suyanhanx@gmail.com>
2023-05-17 14:09:23 +00:00
suyanhanx e853dd831b
chore: Add OpenDAL as an example
Signed-off-by: suyanhanx <suyanhanx@gmail.com>
2023-05-17 21:35:12 +08:00
Stu Hood 20c5618160
Add support for combining the `#[new]` and `#[classmethod]` method types. 2023-05-16 15:51:14 -07:00
bors[bot] edb9522331
Merge #3152
3152: Fix patch version of our dependency on syn. r=davidhewitt a=adamreichold

Closes #3151 

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-05-10 20:32:33 +00:00
Adam Reichold 91cdfe4f7d Fix patch version of our dependency on syn. 2023-05-10 22:31:15 +02:00
bors[bot] c7d541b9ad
Merge #3150
3150: Fix Clippy CI job cache to really include the platform. r=messense a=adamreichold

Doesn't really matter since we never build the same target on different platforms, but fixing it will prevent future confusion when reading it. Sorry for the extra lap. 😫

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-05-10 06:36:01 +00:00
Adam Reichold 9257e78e90 Fix Clippy CI job cache to really include the platform. 2023-05-10 07:59:01 +02:00
bors[bot] 4a0bea2e93
Merge #3062
3062: Stop panic on `fmt::Display` r=davidhewitt a=samuelcolvin

Closes #3060

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2023-05-10 05:52:40 +00:00
bors[bot] 77b3496f26
Merge #3149
3149: Enable rust-cache on clippy CI jobs as we do for build CI jobs. r=davidhewitt a=adamreichold

Not sure if there was a reason we did not enable this for the Clippy jobs?

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-05-09 21:15:44 +00:00
bors[bot] 633b592ed5
Merge #2593
2593: docs: mention PyBuffer r=adamreichold a=davidhewitt

Uses PEP 688 `types.Buffer` to describe `PyBuffer<T>` in the conversion tables. Will leave as draft until PEP 688 is finalised.

Closes #954 

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-05-09 20:24:37 +00:00
Adam Reichold 0f12d72f7a Enable rust-cache on clippy CI jobs as we do for build CI jobs. 2023-05-09 22:05:57 +02:00
Adam Reichold d29b8a487f Pin web-sys, js-sys and wasm-bindgen to avoid syn 2.0 as it is incompatible with our MSRV. 2023-05-09 22:04:23 +02:00
David Hewitt 7c231a2372 docs: mention PyBuffer 2023-05-09 19:53:39 +01:00
bors[bot] 23e764d1ea
Merge #3146
3146: Added a few lines to document the main difference between maturin and… r=adamreichold a=why-not-try-calmer

… setuptools-rust as far as building manylinux-compliant wheels is concerned.

Co-authored-by: Adrien <mrnycticorax@gmail.com>
2023-05-09 12:55:25 +00:00
Adrien 1676100dfd Added a few lines to document the main difference between maturin and setuptools-rust as far as building manylinux-compliant wheels is concerned.
Added newfragment
2023-05-09 14:54:30 +02:00
Samuel Colvin 5aff2ccc9e stop panic on to_string, report using write_unraisable instead 2023-05-09 12:54:56 +02:00
bors[bot] c27a6333d6
Merge #3029
3029: use dynamic trampoline for all getters and setters r=adamreichold a=davidhewitt

This is an extension to the "trampoline" changes made in #2705 to re-use a single trampoline for all `#[getter]`s (and similar for all `#[setters]`). It works by setting the currently-unused `closure` member of the `ffi::PyGetSetDef` structure to point at a new `struct GetSetDefClosure` which contains function pointers to the `getter` / `setter` implementations.

A universal trampoline for all `getter`, for example, then works by reading the actual getter implementation out of the `GetSetDefClosure`.

Advantages of doing this:
- Very minimal simplification to the macro code / generated code size. It made a 4.4% reduction to `test_getter_setter` generated size, which is an exaggerated result as most code will probably have lots of bulk that isn't just the macro code.

Disadvantages:
- Additional level of complexity in the `getter` and `setter` trampolines and accompanying code.
- To keep the `GetSetDefClosure` objects alive, I've added them to the static `LazyTypeObject` inner.
- Very slight performance overhead at runtime (shouldn't be more than an additional pointer read). It's so slight I couldn't measure it.

Overall I'm happy to either merge or close this based on what reviewers think!

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-05-09 08:50:45 +00:00
bors[bot] 7b443cf813
Merge #2981
2981: Remove 0.17 deprecations r=adamreichold,davidhewitt a=davidhewitt

Since #2980 starts a breaking change for 0.19, let's also clean up all 0.17's deprecations.

I've removed `Python::acquire_gil` in its own commit, as that was a reasonably chunky removal.

Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-05-09 08:08:06 +00:00
Adam Reichold 3343d683da Explicitly install the rust-src component as we repeatedly had CI failures when this was installed on-demand in the MSRV Clippy job. 2023-05-09 09:39:23 +02:00
David Hewitt 77eb9bd480 news entry for 0.17 deprecations removals 2023-05-09 09:39:23 +02:00
David Hewitt 996305ac62 remove other deprecations from 0.17 2023-05-09 09:39:23 +02:00
David Hewitt dd24c9ea71 remove `Python::acquire_gil` 2023-05-09 09:39:23 +02:00
Adam Reichold 3c634dd9a9 Add migration guide entry on replacing acquire_gil by with_gil. 2023-05-09 09:39:23 +02:00