Extending native types does not always work
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
Bug Description
I tried to extend various classes implementing PySizedLayout as stated in the docs, but it seems only a few of those classes can be used as base class. I tried:
PyDictandPySet, which work fine.PyBoolandPySlice, which gives this error:TypeError: type 'bool' (or 'slice') is not an acceptable base type- Most importantly,
PyFloatandPyFrozenSetgive the following:Job 1, 'python3 test.py' terminated by signal SIGSEGV (Address boundary error)
Steps to Reproduce
- Cargo.toml
[package]
name = "pyo3_test"
version = "0.1.0"
edition = "2021"
[dependencies]
pyo3 = { git = "https://github.com/PyO3/pyo3", features = ["extension-module"] }
[lib]
name = "pyo3_test"
crate-type = ["cdylib"]
- Rust code:
use pyo3::types::PyFloat;
use pyo3::PyAny;
use pyo3::{pyclass, pymethods, pymodule, types::PyModule, PyResult, Python};
#[pyclass(extends=PyFloat)] // Or replace with other Py* struct
struct Number;
#[pymethods]
impl Number {
#[new]
#[pyo3(signature = (*_args, **_kwargs))]
pub fn new(_args: &PyAny, _kwargs: Option<&PyAny>) -> Self {
Self {}
}
}
#[pymodule]
fn pyo3_test(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<Number>()?;
Ok(())
}
- Python code:
from pyo3_test import Number
test = Number()
print(test)
- Commands (inside venv)
$ maturin develop
$ python3 test.py
Backtrace
For PyFloat for example:
fish: Job 1, 'python3 test.py' terminated by signal SIGSEGV (Address boundary error)
and for PySlice:
TypeError: type 'slice' is not an acceptable base type
thread '<unnamed>' panicked at 'An error occurred while initializing class Number', /home/<user>/.cargo/git/checkouts/pyo3-a22e69bc62b9f0fd/6757391/src/pyclass.rs:412:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
File "/home/<user>/<path>/pyo3-test/test.py", line 1, in <module>
from pyo3_test import Number
File "/home/<user>/<path>/pyo3-test/venv/lib/python3.10/site-packages/pyo3_test/__init__.py", line 1, in <module>
from .pyo3_test import *
pyo3_runtime.PanicException: An error occurred while initializing class Number
Your operating system and version
PopOS 22.04
Your Python version (python --version)
Python 3.10.6
Your Rust version (rustc --version)
rustc 1.67.0 (fc594f156 2023-01-24)
Your PyO3 version
master
How did you install python? Did you use a virtualenv?
python installed with apt (IIRC) and using a venv.
Additional Info
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the provided Cargo.toml, Rust class, and Python script, then inspect pyclass.rs around the panic at line 412 and the PySizedLayout inheritance documentation. Compare behavior for PyDict, PySet, PyBool, PySlice, PyFloat, and PyFrozenSet. Done means unsupported bases fail safely and supported native types no longer trigger a segmentation fault.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100