Commit graph

41 commits

Author SHA1 Message Date
bors[bot] 3b2c175f8d
Merge #2947
2947: change PyModule::add_class to return an error if class creation fails r=adamreichold a=davidhewitt

Related to #2942 

At the moment there are panics deep in the `#[pyclass]` machinery when initialising the type fails. This PR adjusts a number of these functions to return `PyResult` instead, so that we can handle the error more appropriately further down the pipeline.

For example, take the following snippet:

```rust
#[pyclass(extends = PyBool)]
struct ExtendsBool;

#[pymodule]
fn pyo3_scratch(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_class::<ExtendsBool>()?;
    Ok(())
}
```

Currently, importing this module will fail with a panic:

```
TypeError: type 'bool' is not an acceptable base type
thread '<unnamed>' panicked at 'An error occurred while initializing class ExtendsBool', /Users/david/Dev/pyo3/src/pyclass.rs:412:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/david/.virtualenvs/pyo3/lib/python3.10/site-packages/pyo3_scratch/__init__.py", line 1, in <module>
    from .pyo3_scratch import *
pyo3_runtime.PanicException: An error occurred while initializing class ExtendsBool
```

After this PR, this import still fails, but with a slightly cleaner, more Pythonic error:

```
TypeError: type 'bool' is not an acceptable base type

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/david/.virtualenvs/pyo3/lib/python3.10/site-packages/pyo3_scratch/__init__.py", line 1, in <module>
    from .pyo3_scratch import *
RuntimeError: An error occurred while initializing class ExtendsBool
```

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
2023-02-18 16:12:17 +00:00
bors[bot] c858ced8d4
Merge #2944
2944: optimize sequence conversion for list and tuple r=adamreichold a=davidhewitt

closes #2943 

Avoid using `PyObject_IsInstance` for checking if lists or tuples are sequences, as we know they are always sequences.

Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
2023-02-15 23:16:35 +00:00
David Hewitt 4a41917365 add benchmarks for sequence conversions 2023-02-15 08:28:29 +00:00
David Hewitt 59630beb51 add benchmark for mapping conversion from dicts 2023-02-15 08:27:18 +00:00
David Hewitt 00ddd21535 change PyModule::add_class to return an error if class creation fails 2023-02-14 22:08:35 +00:00
David Hewitt dbeb3b4453 move some private internals out of public implementation 2023-02-11 21:28:27 +00:00
David Hewitt 1a4153f718 remove unneeded into_iter calls 2023-01-27 08:20:03 +00:00
David Hewitt 710c895d01 ci: run checks for all platforms on PR 2022-12-25 20:02:21 +00:00
David Hewitt 65ead640ee benchmark PySet::new 2022-12-04 08:57:27 +00:00
Georg Brandl c489809938 Py/PyAny: deprecate cast_as() in favor of downcast()
They are (practically) identical on PyAny, and `downcast()` is the more
useful name.
2022-11-18 07:00:40 +01:00
David Hewitt de8fa18946 clippy: fixes flagged by beta toolchain 2022-08-14 07:44:11 +01:00
mejrs 984fdf57c7 Use Python:;with_gil in tests 2022-07-19 19:34:23 +02:00
Adam Reichold 74e93a2c29 Add benchmark highlighting the costs of failed calls to FromPyObject::extract. 2022-04-10 13:04:27 +02:00
Adam Reichold db109867d3 Add intern! macro which can be used to amortize the cost of creating Python objects by storing them inside a GILOnceCell. 2022-04-03 21:16:42 +02:00
mejrs 6f1cf1b662 Add more lints 2022-03-23 08:07:28 +01:00
Georg Brandl a21cf677e3 docs: remove/deprecate pyproto 2022-02-23 07:50:55 +00:00
David Hewitt 492b7e4c0f macros: optimize generated code for #[derive(FromPyObject)] 2021-12-22 08:34:05 +00:00
David Hewitt ff6fb5dcc2 benches: add bench_frompyobject 2021-12-22 00:27:13 +00:00
Bruno Kolenbrander 7dbbf7194a
Fix the main branch (#2046) 2021-12-09 01:03:50 +01:00
David Hewitt 4b2345fe80 pymethods: support __call__ proto 2021-10-23 00:06:46 +01:00
David Hewitt a20864a903 ci: test and fix s390x build 2021-08-30 13:50:44 +01:00
Aviram Hassan c6255e6734 - PyList, PyTuple and PySequence's get_item now accepts only usize indices instead of isize.
- `PyList` and `PyTuple`'s `get_item` now always uses the safe API. See `get_item_unchecked` for retrieving index without checks.
2021-08-17 15:01:22 +01:00
David Hewitt ee9a1678d5 rust: updates for rust & clippy 1.54 2021-07-31 08:19:38 +01:00
David Hewitt dc58815143 benches: add bench_err 2021-07-24 08:50:35 +01:00
David Hewitt d6437af4a5 benches: add benchmarks for container creation 2021-06-06 08:26:20 +01:00
David Hewitt 681f9ccefa bench_tuple: add benchmark for PyTuple::new 2021-06-05 08:46:05 +01:00
messense fa07a5e377 Migrate all benchmark to criterion 2021-05-26 14:25:00 +08:00
David Hewitt 0cc77c77b2 benches: add bench_gil 2021-05-17 07:37:03 +01:00
David Hewitt 48823e22d6 pyproto: deprecate py_methods 2021-04-20 00:21:19 +01:00
David Hewitt c933ac223e bench_pyclass: add protocol method 2021-02-24 23:56:00 +00:00
David Hewitt fcb51677c2 benches: add benchmark for type object initialization 2021-02-18 08:19:07 +00:00
David Hewitt 02b90c462f benches: add map / set extraction 2020-11-26 10:02:59 +00:00
David Hewitt d1248d5743 benches: add bench_call 2020-11-25 20:54:12 +00:00
Alex Gaynor c54b35f297 Added a benchmark for set iter 2020-09-08 17:47:49 -04:00
David Hewitt e1533129c9 Remove bench_pyclass 2020-05-03 11:12:31 +01:00
David Hewitt be09aa00aa New benchmarks 2020-05-02 23:38:36 +01:00
David Hewitt fe57f64435 Improve performance on pointer drop
Co-Authored-By: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com>
2020-04-09 08:51:43 +01:00
Samuele Maci 7034332f46 Address clippy::inconsistent_digit_grouping issues 2019-08-17 14:10:35 +02:00
konstin 936f0153e8 Migrate to rust 2018 2019-02-01 14:23:29 +01:00
kngwyu be77846979 Use LinkedList<[T; 256]> in ReleasePool 2018-11-21 16:00:26 +09:00
kngwyu 6222232c21 Add bench_dict 2018-11-14 15:40:28 +09:00