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

Add PyTypeMethods trait with qualname method #3854

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,11 @@ struct PyDowncastErrorArguments {

impl PyErrArguments for PyDowncastErrorArguments {
fn arguments(self, py: Python<'_>) -> PyObject {
use crate::types::typeobject::PyTypeMethods;
format!(
"'{}' object cannot be converted to '{}'",
self.from
.as_ref(py)
.bind(py)
.qualname()
.as_deref()
.unwrap_or("<failed to extract type name>"),
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ pub use crate::types::set::PySetMethods;
pub use crate::types::string::PyStringMethods;
pub use crate::types::traceback::PyTracebackMethods;
pub use crate::types::tuple::PyTupleMethods;
pub use crate::types::typeobject::PyTypeMethods;
2 changes: 1 addition & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,4 @@ mod slice;
pub(crate) mod string;
pub(crate) mod traceback;
pub(crate) mod tuple;
mod typeobject;
pub(crate) mod typeobject;
37 changes: 36 additions & 1 deletion src/types/typeobject.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::err::{self, PyResult};
use crate::{ffi, PyAny, PyTypeInfo, Python};
use crate::{ffi, Bound, PyAny, PyTypeInfo, Python};
use std::borrow::Cow;
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
use std::ffi::CStr;
Expand Down Expand Up @@ -108,6 +108,41 @@ impl PyType {
}
}

/// Implementation of functionality for [`PyType`].
///
/// These methods are defined for the `Bound<'py, PyType>` smart pointer, so to use method call
/// syntax these methods are separated into a trait, because stable Rust does not yet support
/// `arbitrary_self_types`.
pub trait PyTypeMethods<'py> {
/// Gets the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name) of the `PyType`.
fn qualname(&self) -> PyResult<String>;
}

impl<'py> PyTypeMethods<'py> for Bound<'py, PyType> {
fn qualname(&self) -> PyResult<String> {
use crate::types::any::PyAnyMethods;
#[cfg(any(Py_LIMITED_API, PyPy, not(Py_3_11)))]
let name = self
.as_any()
.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;

let obj = unsafe {
ffi::PyType_GetQualName(self.as_ptr() as *mut ffi::PyTypeObject)
.assume_owned_or_err(self.py())?
};

obj.extract()
};

name
}
}

#[cfg(test)]
mod tests {
use crate::types::{PyBool, PyLong};
Expand Down
2 changes: 1 addition & 1 deletion tests/test_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl ClassMethod {
fn method_owned(cls: Py<PyType>) -> PyResult<String> {
Ok(format!(
"{}.method_owned()!",
Python::with_gil(|gil| cls.as_ref(gil).qualname())?
Python::with_gil(|gil| cls.bind(gil).qualname())?
))
}
}
Expand Down
Loading