Commit Graph

2301 Commits

Author SHA1 Message Date
Tpt 1062f244ea Pick a relevant ErrorKind when building an io::Error from a OSError subclass 2023-08-17 18:33:51 +02:00
Tpt 7c366cb930 Uses io::Error code when converting io::IntoInnerError to PyErr 2023-08-17 18:25:36 +02:00
Alex Gaynor 9f1b56b659
Remove usage of `AsPyPointer` in `IntoPy<PyObject>` trait implementation 2023-08-17 07:05:52 -04:00
Iliya Malecki d1f0561036 added speedy BigInt extraction 2023-08-17 08:15:50 +01:00
Alex Gaynor c259e77ca2
Remove usage of `AsPyPointer` in traits for convergint to PyObject
Refs #3358
2023-08-16 07:03:57 -04:00
Alex Gaynor 1a904a7429
Migrate `PyIterator::from_object` and `PyByteArray::from` from `AsPyPointer` to `&PyAny` 2023-08-15 18:03:56 -04:00
Alex Gaynor 5061b501a9
Remove IntoPyPointer
it wasn't used in any public APIs following #3359
2023-08-14 16:28:20 -04:00
David Hewitt a541925f00
Merge pull request #3359 from davidhewitt/py-pointer-methods
add as_ptr and into_ptr inherent methods
2023-08-11 19:50:22 +00:00
David Hewitt 8031794f2a
Merge pull request #3378 from GoldsteinE/implicit-builtins
add `__builtins__` to globals in `py.run()` if they're missing
2023-08-11 14:11:31 +00:00
Goldstein 0be94a5442
add `__builtins__` to globals in `py.run()` if they're missing
Python code doesn't like to run without `__builtins__`, so adding them
if missing seems to be a good idea. Also that's what CPython >3.10 does.

See https://github.com/python/cpython/pull/24564
Resolves #3370
2023-08-11 15:48:05 +03:00
David Hewitt 64adab1a76 add as_ptr and into_ptr inherent methods 2023-08-11 10:49:42 +01:00
Tpt 1e5a49557d Makes PathBuf FromPyObject implementation work on all os.PathLike
PyOS_FSPath is in abi3-py36
2023-08-08 22:15:56 +02:00
Adam Reichold a371fbe4f8 Implement DoubleEndedIterator for PyListIterator by caching the length while still validating it before access. 2023-08-07 21:37:50 +02:00
Adam Reichold 4ce3e9649f Implement DoubleEndedIterator for PyTupleIterator 2023-08-07 21:37:50 +02:00
David Hewitt ef43731993 update tests of refcounting to use a non-immortal object 2023-07-30 15:37:06 +01:00
David Hewitt 4e957e8bd4 update object.h definitions for Python 3.12 2023-07-30 15:37:06 +01:00
Juniper Tyree 16fe7a83a7 Add a PySlice::full() constructor for :: 2023-07-29 07:39:47 +00:00
David Hewitt e2c88d5a4e optimize `float` -> `f64` conversions on non-abi3 2023-07-28 20:45:37 +01:00
David Hewitt 34881fc952 add PyAny::downcast_exact 2023-07-28 15:11:56 +01:00
David Hewitt a15614844d add PyErr::display 2023-07-24 22:14:55 +01:00
David Hewitt ab078258d1 fix exception handling on Python 3.12 2023-07-19 22:33:25 +01:00
Zak Stucke f2b7e86e2e Prevent traceback loss on conversion to and from PyErr 2023-07-19 12:07:41 +03:00
David Hewitt 2e2dde910a Preserve panic message after exception is normalized 2023-07-18 22:20:41 +01:00
David Hewitt 421e13a89c
Merge pull request #3323 from davidhewitt/pyerr-simplification
merge PyErr internal states for simplicity
2023-07-17 21:46:39 +00:00
David Hewitt 2d1b8e02a8 merge PyErr internal states for simplicity 2023-07-17 22:21:28 +01:00
David Hewitt e5a7400f24
Merge pull request #3319 from davidhewitt/used-underscore-binding
clippy: deny / fix used-underscope-binding lint
2023-07-16 20:53:56 +00:00
David Hewitt 45ff25cd2a clippy: deny / fix used-underscope-binding lint 2023-07-16 20:49:45 +01:00
David Hewitt 6f30215566 collect arrays of objects prior to filling tuple in fixed-size conversions 2023-07-16 20:17:02 +01:00
Adam Reichold 65312b43b2
Merge pull request #3312 from davidhewitt/opt-baseexception
optimize is_instance for PyBaseException
2023-07-14 16:37:24 +00:00
David Hewitt f39dd52cf9 remove concept of "normalize" from PyErr docs 2023-07-14 13:20:48 +01:00
David Hewitt 92b724f64f normalize exception in `PyErr::matches` and `PyErr::get_type` 2023-07-14 13:18:30 +01:00
David Hewitt b65cbb958b ci: updates for rust 1.71 2023-07-14 12:10:37 +01:00
David Hewitt 272997555e optimize is_instance for PyBaseException 2023-07-11 21:48:46 +01:00
Adam Reichold ff78b92e77 Start adding a performance section to the guide. 2023-07-09 14:32:59 +02:00
Adam Reichold bd7aed4b12 Add implementation of Iterator::size_hint for PyIterator
When the Python iterator backing `PyIterator` has a `__length_hint__` special
method, we can use this as a lower bound for Rust's `Iterator::size_hint` to
e.g. support pre-allocation of collections.

This is implemented using `PyObject_LengthHint` which is not available in the
stable ABI and hence so is `Iterator::size_hint`. This should be fine since this
is an optimization in any case and the stable ABI is expected to have slightly
worse performance overall.
2023-07-09 10:16:20 +02:00
David Hewitt 81c0328d91 use concrete inner for `PyErr:matches` 2023-07-04 21:03:24 +01:00
David Hewitt 76f3a395ef move `unsafe` block inside `error_on_minusone` calls 2023-07-04 21:03:24 +01:00
David Hewitt 43477a8e30 use error_on_minusone in more cases 2023-07-04 21:03:24 +01:00
David Hewitt 7613f65c89 apply conventions for ffi calls 2023-07-04 21:03:24 +01:00
David Hewitt bf2f441567 prefer inner / _private naming 2023-07-04 21:03:24 +01:00
Adam Reichold 54ab9090be
Merge pull request #3269 from grantslatton/timezone-conversion-bugfix
Fix fixed offset timezone conversion bug.
2023-07-04 18:22:13 +00:00
Alex Gaynor 0b78bb851e
Allow `#[new]` to return existing instances
fixes #2384
2023-07-02 19:57:53 -04:00
Adam Reichold e006b34b5e Apparently, PySet_Add does not steal a reference, hence we should not forget to clean up ours. 2023-07-02 20:43:43 +02:00
David Hewitt 135535e668
Merge pull request #3280 from davidhewitt/option-to-tz
clarify ownership of opt_to_pyobj helper function
2023-06-28 21:23:33 +00:00
Adam Reichold afc1d4cc42
Merge pull request #3281 from davidhewitt/set-discard
handle exceptions properly in `PySet::discard`
2023-06-28 17:09:01 +00:00
David Hewitt b7d391d994 clarify ownership of opt_to_pyobj helper function 2023-06-28 08:54:56 +01:00
David Hewitt c0b9502c0e handle exceptions properly in `PySet::discard` 2023-06-28 08:54:06 +01:00
David Hewitt 4867ef8dd3 stop suppressing unrelated exceptions in `PyAny::hasattr` 2023-06-27 23:20:22 +01:00
David Hewitt 20f909c97f refactor `PyDict::get_item[_with_error]` implementations 2023-06-25 20:13:52 +01:00
David Hewitt db91642bae add `PyDict::get_item_with_error` for PyPy 2023-06-25 19:26:08 +01:00
Grant Slatton b1e7ed8a58 Fix fixed offset timezone conversion bug.
See https://github.com/PyO3/pyo3/issues/3267
2023-06-25 11:06:46 -07:00
Adam Reichold 4afa994449 Mark Python::with_pool as safe on stable. 2023-06-21 09:12:10 +02:00
Adam Reichold d7e147509b Add Python::with_pool as a safer alternative to Python::new_pool. 2023-06-21 08:05:19 +02:00
mejrs 686f5eb0ad Simplify doctests macro 2023-06-19 18:12:01 +02:00
Adam Reichold 5eed73f1c0 Rework pyobject_native_type_info! to expect callables
Most native types have static type objects which just need to be turned into a
pointer using addr_of_mut!, but sometimes like for exceptions we do call a
function which lazily initializes a type object.

This makes the pyobject_native_type_info! macro expect callable and passes it
the GIL token so that we do not need to call Python::assume_gil_acquired. The
case of static type objects is handled by the
pyobject_native_static_type_object! helper macro.
2023-06-19 10:30:03 +02:00
Adam Reichold 42bbd52e15 Keep the dynamic borrow checking enabled for debug builds. 2023-06-18 15:38:37 +02:00
Adam Reichold 96ad57d783 We already carefully handle re-entrancy for OWNED_OBJECTS, so let's avoid the runtime checking overhead. 2023-06-18 15:25:46 +02:00
Adam Reichold 85d5b6ee77 Drop reference pool's dirty flag is the additional cost does not outweigh the improvement compared to locking an uncontented mutex. 2023-06-18 15:25:46 +02:00
Adam Reichold cbead1f437 Fix two left-over usages of cfg(addr_of). 2023-06-17 19:35:50 +02:00
Luca Trevisani ab7a65cbb5
Improve `PyModule::from_code` examples 2023-06-16 23:45:49 +02:00
Adam Reichold ba0c7e15b3
Merge pull request #3156 from adriangb/frozenset-ops
Add `PyFrozenSetBuilder`
2023-06-14 06:06:32 +00:00
David Hewitt 4f3fcdbf0f remove all functionality deprecated in PyO3 0.18 2023-06-13 22:27:43 +01:00
Adrian Garcia Badaracco a28641af68
Add `PyFrozenSetBuilder` 2023-06-13 15:59:35 -05:00
Adam Reichold a053489c8b
Merge pull request #2709 from davidhewitt/py3.12
ci: add Python 3.12-dev jobs
2023-06-12 08:59:35 +00:00
Adam Reichold b110466c57
Merge pull request #3221 from davidhewitt/msrv-tidy-ups
docs: remove unneeded cfg, fixup internal banner
2023-06-12 06:33:48 +00:00
David Hewitt 199261f436
Merge pull request #3197 from mhils/ipaddr
Add `IpAddr` <-> `ipaddress.IPv(4/6)Address` conversion
2023-06-12 04:00:43 +00:00
David Hewitt 621898b07d ci: add Python 3.12-dev jobs 2023-06-11 21:57:17 +01:00
David Hewitt 7fd59455b1 ci: pool can be dirty from other threads 2023-06-11 21:52:23 +01:00
David Hewitt 46942b9a14 docs: remove workarounds for `#[doc = concat!(...)]` following msrv bump 2023-06-08 21:04:49 +01:00
Maximilian Hils 980f05a88e Add `IpAddr` <-> `ipaddress.IPv(4/6)Address` conversion 2023-06-08 13:17:45 +02:00
Adam Reichold 0d30bb7d3d Bump Rust edition to 2021 and make current Clippy happy. 2023-06-07 21:15:38 +02:00
bors[bot] e1f028f3e5
Merge #3209
3209: Remove the conditional compilation flags which are made redundant  by the MSRV bump r=davidhewitt a=adamreichold



Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-06-05 14:09:05 +00:00
Adam Reichold eb04f54683 Remove cfg min_const_generics made unnecessary by MSRV bump. 2023-06-05 09:42:40 +02:00
Adam Reichold f7bd6ce224 Remove cfg option_insert made unnecessary by MSRV bump. 2023-06-05 09:37:13 +02:00
bors[bot] dbf7b233aa
Merge #3203
3203: support ordering magic methods for `#[pyclass]` r=adamreichold a=davidhewitt

Closes #2089 

This adds `__lt__`, `__le__`, `__eq__`, `__ne__`, `__gt__`, and `__ge__` as per the Python implementations of what we call `__richcmp__`.

There's a UI test confirming that the user cannot implement split forms and `__richcmp__` simultaneously.

There's also a benchmark comparing implementing these split methods against using `__richcmp__`. I couldn't see a meaningful performance difference, so I'm tempted to deprecate `__richcmp__`, given that's not a magic method which exists in Python. Potentially we can provide options such as the opt-in `#[pyclass(eq, ord)]` to avoid boilerplate for people who don't want to implement six different methods.



Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-06-05 07:13:23 +00:00
David Hewitt 5bc3e6f15e fix clippy warnings uncovered by msrv bump 2023-06-05 07:08:27 +01:00
Adam Reichold 5738edfdd4 Bump MSRV to 1.56 2023-06-04 23:02:30 +01:00
David Hewitt 8850d5d384 support ordering magic methods for `#[pyclass]` 2023-06-04 13:58:18 +01:00
David Hewitt 45b5ede26a remove copyright headers from source files 2023-06-03 22:38:54 +01:00
bors[bot] fa949ff627
Merge #3198
3198: Add abi3 + num_bigint conversion r=davidhewitt a=youknowone

<!--
Please consider adding the following to your pull request:
 - an entry for this PR in newsfragments - see [https://pyo3.rs/main/contributing.html#documenting-changes]
 - docs to all new functions and / or detail in the guide
 - tests for all new or changed functions

PyO3's CI pipeline will check your pull request. To run its tests
locally, you can run ```cargo xtask ci```. See its documentation
 [here](https://github.com/PyO3/pyo3/tree/main/xtask#readme).
-->

Fix #3196

Co-authored-by: Jeong YunWon <jeong@youknowone.org>
2023-06-02 19:06:50 +00:00
Jake Lishman 8d98b4248e
Fix `abi3` conversion of `__complex__` classes
Python classes that were not `complex` but implemented the `__complex__`
magic would have that method called via `PyComplex_AsCComplex` when
running against the full API, but the limited-API version
`PyComplex_RealAsDouble` does not attempt this conversion.  If the input
object is not already complex, we can call the magic before proceeding.
2023-06-02 10:09:45 +01:00
Jake Lishman 194d0c791f
Add `PyAny::lookup_special`
`PyAny::lookup_special` is an approximate equivalent to the CPython
internal `_PyObject_LookupSpecial`, which is used to resolve lookups of
"magic" methods.  These are only looked up from the type, and skip the
instance dictionary during the lookup.  Despite this, they are still
required to resolve the descriptor protocol.

Many magic methods have slots on the `PyTypeObject` or respective
subobjects, but these are not necessarily available when targeting the
limited API or PyPy.  In these cases, the requisite logic can be worked
around using safe but likely slower APIs.

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>

Fix up lookup-special
2023-06-02 10:09:45 +01:00
Jeong YunWon 6d830e05cd Add abi3 + num_bigint conversion 2023-06-02 16:17:49 +09:00
Adam Reichold 0f628c942d Be more explicit of the soundness hole implied by tying Ungil to Send and mention the available solution. 2023-05-25 21:37:33 +02:00
Adam Reichold 4fc7b5a6e0 Manually link to the send_wrapper crate to avoid having it as a build dependencies for the docs. 2023-05-25 21:37:33 +02:00
Adam Reichold 90fcc63eda Replace the nightly UI tests by doctests since we cannot keep up with the changing error outputs of nightly in any case. 2023-05-25 21:37:32 +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 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] 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
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
Kang Seonghoon dee45d3644 Use a const initializer for `GIL_COUNT` if possible. 2023-05-22 11:31:20 +09:00
David Hewitt 82a879ff6b add `PyAny::is_exact_instance` and `PyAny::is_exact_instance_of` 2023-05-21 12:44:54 +02:00
David Hewitt 248230b8e4 refactor PyAny::is_instance_of for performance 2023-05-18 22:25:27 +01: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] 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] 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