Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename name to qualname and add name to better match Python and ensure abi3 fallbacks. #3660

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion guide/src/class/numeric.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl Number {

fn __repr__(slf: &PyCell<Self>) -> PyResult<String> {
// Get the class name dynamically in case `Number` is subclassed
let class_name: &str = slf.get_type().name()?;
let class_name: String = slf.get_type().qualname()?;
Ok(format!("{}({})", class_name, slf.borrow().0))
}

Expand Down
4 changes: 2 additions & 2 deletions guide/src/class/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ the subclass name. This is typically done in Python code by accessing
impl Number {
fn __repr__(slf: &PyCell<Self>) -> PyResult<String> {
// This is the equivalent of `self.__class__.__name__` in Python.
let class_name: &str = slf.get_type().name()?;
let class_name: String = slf.get_type().qualname()?;
// To access fields of the Rust struct, we need to borrow the `PyCell`.
Ok(format!("{}({})", class_name, slf.borrow().0))
}
Expand Down Expand Up @@ -263,7 +263,7 @@ impl Number {
}

fn __repr__(slf: &PyCell<Self>) -> PyResult<String> {
let class_name: &str = slf.get_type().name()?;
let class_name: String = slf.get_type().qualname()?;
Ok(format!("{}({})", class_name, slf.borrow().0))
}

Expand Down
3 changes: 3 additions & 0 deletions guide/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Python::with_gil(|py| {
});
```

### `PyType::name` is now `PyType::qualname`

`PyType::name` has been renamed to `PyType::qualname` to indicate that it does indeed return the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name), matching the `__qualname__` attribute. The newly added `PyType::name` yields the full name including the module name now which corresponds to `__module__.__name__` on the level of attributes.

## from 0.19.* to 0.20

Expand Down
1 change: 1 addition & 0 deletions newsfragments/3660.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`PyType::name` is now `PyType::qualname` whereas `PyType::name` efficiently accesses the full name which includes the module name.
7 changes: 7 additions & 0 deletions pytests/src/misc.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use pyo3::prelude::*;
use std::borrow::Cow;

#[pyfunction]
fn issue_219() {
// issue 219: acquiring GIL inside #[pyfunction] deadlocks.
Python::with_gil(|_| {});
}

#[pyfunction]
fn get_type_full_name(obj: &PyAny) -> PyResult<Cow<'_, str>> {
obj.get_type().name()
}

#[pymodule]
pub fn misc(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(issue_219, m)?)?;
m.add_function(wrap_pyfunction!(get_type_full_name, m)?)?;
Ok(())
}
6 changes: 6 additions & 0 deletions pytests/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ def test_import_in_subinterpreter_forbidden():
)

_xxsubinterpreters.destroy(sub_interpreter)


def test_type_full_name_includes_module():
numpy = pytest.importorskip("numpy")

assert pyo3_pytests.misc.get_type_full_name(numpy.bool_(True)) == "numpy.bool_"
10 changes: 7 additions & 3 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ impl std::fmt::Display for PyErr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Python::with_gil(|py| {
let value = self.value(py);
let type_name = value.get_type().name().map_err(|_| std::fmt::Error)?;
let type_name = value.get_type().qualname().map_err(|_| std::fmt::Error)?;
write!(f, "{}", type_name)?;
if let Ok(s) = value.str() {
write!(f, ": {}", &s.to_string_lossy())
Expand Down Expand Up @@ -748,7 +748,8 @@ impl PyErrArguments for PyDowncastErrorArguments {
"'{}' object cannot be converted to '{}'",
self.from
.as_ref(py)
.name()
.qualname()
.as_deref()
.unwrap_or("<failed to extract type name>"),
self.to
)
Expand All @@ -775,7 +776,10 @@ impl<'a> std::fmt::Display for PyDowncastError<'a> {
write!(
f,
"'{}' object cannot be converted to '{}'",
self.from.get_type().name().map_err(|_| std::fmt::Error)?,
self.from
.get_type()
.qualname()
.map_err(|_| std::fmt::Error)?,
self.to
)
}
Expand Down
58 changes: 55 additions & 3 deletions src/types/typeobject.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::err::{self, PyResult};
use crate::{ffi, PyAny, PyTypeInfo, Python};
use std::borrow::Cow;
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
use std::ffi::CStr;

/// Represents a reference to a Python `type object`.
#[repr(transparent)]
Expand Down Expand Up @@ -30,9 +33,58 @@ impl PyType {
py.from_borrowed_ptr(p as *mut ffi::PyObject)
}

/// Gets the name of the `PyType`.
pub fn name(&self) -> PyResult<&str> {
self.getattr(intern!(self.py(), "__qualname__"))?.extract()
/// Gets the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name) of the `PyType`.
pub fn qualname(&self) -> PyResult<String> {
#[cfg(any(Py_LIMITED_API, PyPy, not(Py_3_11)))]
let name = self.getattr(intern!(self.py(), "__qualname__"))?.extract();

#[cfg(not(any(Py_LIMITED_API, PyPy, not(Py_3_11))))]
let name = {
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::types::any::PyAnyMethods;

let obj = unsafe {
ffi::PyType_GetQualName(self.as_type_ptr()).assume_owned_or_err(self.py())?
};

obj.extract()
};

name
}

/// Gets the full name, which includes the module, of the `PyType`.
pub fn name(&self) -> PyResult<Cow<'_, str>> {
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
{
let ptr = self.as_type_ptr();

let name = unsafe { CStr::from_ptr((*ptr).tp_name) }.to_str()?;

#[cfg(Py_3_10)]
if unsafe { ffi::PyType_HasFeature(ptr, ffi::Py_TPFLAGS_IMMUTABLETYPE) } != 0 {
return Ok(Cow::Borrowed(name));
}

Ok(Cow::Owned(name.to_owned()))
}

#[cfg(any(Py_LIMITED_API, PyPy))]
{
davidhewitt marked this conversation as resolved.
Show resolved Hide resolved
let module = self.getattr(intern!(self.py(), "__module__"))?;

#[cfg(not(Py_3_11))]
let name = self.getattr(intern!(self.py(), "__name__"))?;

#[cfg(Py_3_11)]
let name = {
use crate::ffi_ptr_ext::FfiPtrExt;

unsafe { ffi::PyType_GetName(self.as_type_ptr()).assume_owned_or_err(self.py())? }
};

Ok(Cow::Owned(format!("{}.{}", module, name)))
}
}

/// Checks whether `self` is a subclass of `other`.
Expand Down
5 changes: 4 additions & 1 deletion tests/test_coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ fn cancelled_coroutine() {
None,
)
.unwrap_err();
assert_eq!(err.value(gil).get_type().name().unwrap(), "CancelledError");
assert_eq!(
err.value(gil).get_type().qualname().unwrap(),
"CancelledError"
);
})
}

Expand Down
8 changes: 4 additions & 4 deletions tests/test_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ impl ClassMethod {
#[classmethod]
/// Test class method.
fn method(cls: &PyType) -> PyResult<String> {
Ok(format!("{}.method()!", cls.name()?))
Ok(format!("{}.method()!", cls.qualname()?))
}

#[classmethod]
fn method_owned(cls: Py<PyType>) -> PyResult<String> {
Ok(format!(
"{}.method_owned()!",
Python::with_gil(|gil| cls.as_ref(gil).name().map(ToString::to_string))?
Python::with_gil(|gil| cls.as_ref(gil).qualname())?
))
}
}
Expand Down Expand Up @@ -109,7 +109,7 @@ struct ClassMethodWithArgs {}
impl ClassMethodWithArgs {
#[classmethod]
fn method(cls: &PyType, input: &PyString) -> PyResult<String> {
Ok(format!("{}.method({})", cls.name()?, input))
Ok(format!("{}.method({})", cls.qualname()?, input))
}
}

Expand Down Expand Up @@ -937,7 +937,7 @@ impl r#RawIdents {
fn test_raw_idents() {
Python::with_gil(|py| {
let raw_idents_type = py.get_type::<r#RawIdents>();
assert_eq!(raw_idents_type.name().unwrap(), "RawIdents");
assert_eq!(raw_idents_type.qualname().unwrap(), "RawIdents");
py_run!(
py,
raw_idents_type,
Expand Down
Loading