pyo3/benches
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
..
bench_call.rs Add more lints 2022-03-23 08:07:28 +01:00
bench_dict.rs add benchmark for mapping conversion from dicts 2023-02-15 08:27:18 +00:00
bench_err.rs Add more lints 2022-03-23 08:07:28 +01:00
bench_frompyobject.rs Py/PyAny: deprecate cast_as() in favor of downcast() 2022-11-18 07:00:40 +01:00
bench_gil.rs clippy: fixes flagged by beta toolchain 2022-08-14 07:44:11 +01:00
bench_intern.rs ci: run checks for all platforms on PR 2022-12-25 20:02:21 +00:00
bench_list.rs add benchmarks for sequence conversions 2023-02-15 08:28:29 +00:00
bench_pyclass.rs change PyModule::add_class to return an error if class creation fails 2023-02-14 22:08:35 +00:00
bench_pyobject.rs Use Python:;with_gil in tests 2022-07-19 19:34:23 +02:00
bench_set.rs remove unneeded into_iter calls 2023-01-27 08:20:03 +00:00
bench_tuple.rs add benchmarks for sequence conversions 2023-02-15 08:28:29 +00:00