Commit graph

103 commits

Author SHA1 Message Date
Lily Foote 76dabd4e60
Replace as_ref(py) with Bound APIs (#3863) 2024-02-19 22:39:54 +00:00
Icxolu 4ce9c35983
port Python::get_type to Bound API (#3846)
* port `Python::get_type` to `Bound` API

* fix `is_subclass_and_is_instance` FIXME
2024-02-18 18:27:19 +00:00
David Hewitt f04ad56df4
implement PyTypeMethods (#3705)
* implement `PyTypeMethods`

* introduce `PyType` bound constructors

* `from_type_ptr_bound` instead of `from_type_ptr_borrowed`

* correct conditional code

* just make `from_type_ptr_bound` create an owned `Bound`

* correct docstrings

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* Rework as `PyType::from_borrowed_type_ptr`

* correct doc link to `from_borrowed_type_ptr`

Co-authored-by: Lily Foote <code@lilyf.org>

* remove unneeded lifetime name

---------

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
Co-authored-by: Lily Foote <code@lilyf.org>
2024-02-18 03:07:48 +00:00
Icxolu 1d295a12a0
port PyObject::downcast to Bound API (#3856)
* port `PyObject::downcast` to `Bound` API

* relax traits bounds for unchecked variant in `Bound` API

* deprecate `Python::(checked_)cast_as`

* reword deprecation warning
2024-02-18 01:11:43 +00:00
Lily Foote 0dd568d397
Use the new bound API instead of .as_ref(py) (#3853)
* Use the new bound API instead of .as_ref(py)

* Move import into a nested scope

* Use to_cow instead of to_str for compatibility

`to_str` is not available before Python 3.10 on the limited api.

* Relax &self lifetimes

* Use Bound<'py, PyAny> in test Mapping signatures

* Use .as_bytes(py)

* Simplify ThrowCallback::throw signature

* Avoid .as_any call with Py api instead of Bound
2024-02-18 00:09:56 +00:00
Lily Foote 940804fe0d
Pyerr value bound (#3820)
* Implement PyErr::value_bound

* Use PyErr::value_bound in conversions

* Implement PyErr::from_value_bound

* Remove unnecessary clone

* Return a reference from PyErr::value_bound

* Avoid clone in PyErr::from_value_bound

* Use PyErr::from_value_bound instead of from_value

* Remove unnecessary .as_borrowed() calls

* Remove unused import

* Simplify UnraisableCapture.hook

* Use Bound::from_owned_ptr_or_opt in fn cause

* Update PyErrState::lazy to take Py<PyAny>

This is easier to work with elsewhere than `&PyAny` or
`Bound<'py, PyAny>`.

* Add Bound PyUnicodeDecodeError constructors

* Update PyErr::from_value_bound to take Bound

* Simplify PyErr::from_value

* Simplify PyErr::value

* Remove unnecessary reference

* Simplify Pyerr::cause implementation

* Simplify PyUnicodeDecodeError::new_bound
2024-02-17 00:27:45 +00:00
Icxolu 05aedc9032
port PyErr::warn to Bound API (#3842)
* port `PyErr::new_type`

* port `PyErr::warn` and `PyErr::warn_explicit`
2024-02-16 00:12:43 +00:00
Icxolu 0c12d9137f
port Python::import to Bound API (#3832)
* port `Python::import` to `Bound` API

* tidy up imports in tests/test_datetime_import.rs

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-02-14 00:24:37 +00:00
David Hewitt 94b7d7e434
add DowncastIntoError::into_inner (#3829) 2024-02-12 21:40:05 +00:00
Lily Foote 1279467d27
Pyerr isinstance (#3826)
* Implement PyErr::is_instance_bound

* Update is_instance_bound to take a reference

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Remove spurious clone

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-02-12 08:32:51 +00:00
Lily Foote baf5c8ec0a
Implement PyErr::get_type_bound (#3819)
* Implement PyErr::get_type_bound

* Update docs for PyErr::get_type_bound

* Fix doctest for cloning PyErr

* Import the whole prelude in docs example

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Remove unnecessary self lifetime

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Remove more unnecessary self lifetimes

* Use variables to avoid dangling pointers

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Avoid using ffi in fn ptype on Py_3_12

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Add missing imports to fn ptype

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
2024-02-11 21:07:28 +00:00
Icxolu 4d423b0c67 port Python::run to Bound API 2024-02-09 22:09:16 +01:00
David Hewitt 367eeaeeab add bound method variants for PyTypeInfo 2024-02-08 22:27:05 +00:00
David Hewitt 76d1b34cd5 Revert "Merge pull request #3578 from davidhewitt/typed-helpers"
This reverts commit 7b07d6d21b, reversing
changes made to 99858236bd.
2024-02-03 20:56:23 +00:00
David Hewitt c47565666d add PyString::new_bound 2024-01-29 13:14:00 +00:00
Icxolu f86053e2c2 implement PyTracebackMethods 2024-01-27 11:34:32 +01:00
David Hewitt 3da1aac2dd add gil-refs feature to aid migration 2023-12-29 14:42:31 +00:00
David Hewitt 1b61cb015a Add .as_borrowed() conversion from gil-refs to Bound<T> 2023-12-26 09:49:03 +00:00
David Hewitt 877e34ac86 implement PyErr::write_unraisable_bound 2023-12-24 19:35:29 +00:00
David Hewitt 0f242c399d make DowncastError and DowncastIntoError public 2023-12-21 13:03:59 +00:00
David Hewitt c08c6c0a41 make new downcast errors public API 2023-12-21 12:28:12 +00:00
David Hewitt 704e9fc7b5 Py2 -> Bound 2023-12-21 12:04:45 +00:00
Tpt 8b614745cf Adds std::duration::Duration from/to Python conversions 2023-12-20 08:57:26 +01:00
David Hewitt 1451418ee4 Add Py2 variants of PyDowncastError 2023-12-19 20:59:57 +00:00
Adam Reichold 416d3c488f Rename name to qualname and full_name to name to better match Python 2023-12-19 16:51:24 +01:00
David Hewitt bd0174aa5d Change return types of py.None(), py.NotImplemented() and py.Ellipsis() to typed singletons 2023-11-17 16:16:19 +00:00
David Hewitt 2daddb4734 unify 3.12 and pre-3.12 exception handling pathways 2023-09-30 23:27:27 +01:00
David Hewitt 4b5ff05721
Merge pull request #3471 from davidhewitt/normalized-exceptions-2
always normalize exceptions before raising
2023-09-28 20:27:18 +00:00
David Hewitt 673c45aac3
Merge pull request #3470 from davidhewitt/tests-capture-warnings
add `assert_warnings` test helper
2023-09-24 13:09:01 +00:00
David Hewitt 1e8833e15e always normalize exceptions before raising 2023-09-24 13:38:23 +01:00
David Hewitt d089c92680 unify 3.12 / non-3.12 error handling branches 2023-09-23 22:18:59 +01:00
David Hewitt c8f82be32c add assert_warnings test helper 2023-09-23 13:14:18 +01:00
Tpt 6d8e8b302f Unwrap the underlying PyErr when converting an io::Error back to a PyErr
Exposes it directly instead of loosing all information outside the message
2023-08-18 15:56:25 +02:00
David Hewitt 7f32ed96db
Merge pull request #3396 from Tpt/IntoInnerError
Uses io::Error code when converting io::IntoInnerError to PyErr
2023-08-18 08:27:35 +00:00
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
David Hewitt 64adab1a76 add as_ptr and into_ptr inherent methods 2023-08-11 10:49:42 +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 2d1b8e02a8 merge PyErr internal states for simplicity 2023-07-17 22:21:28 +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 272997555e optimize is_instance for PyBaseException 2023-07-11 21:48:46 +01: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