Skip to content

Commit

Permalink
Make PyType::name abi3 compatible
Browse files Browse the repository at this point in the history
The implementation is more complex, because there's no equivalent to tp_name in the limited API
  • Loading branch information
alex committed Sep 8, 2020
1 parent e8936be commit 588f99c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ impl<'a> std::fmt::Display for PyDowncastError<'a> {
self.from
.repr()
.map(|s| s.to_string_lossy())
.unwrap_or_else(|_| self.from.get_type().name()),
.unwrap_or_else(|_| Cow::Borrowed(self.from.get_type().name())),
self.to
)
}
Expand Down
9 changes: 5 additions & 4 deletions src/types/typeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use crate::err::{PyErr, PyResult};
use crate::instance::PyNativeType;
use crate::type_object::PyTypeObject;
use crate::{ffi, AsPyPointer, PyAny, Python};
use std::borrow::Cow;
use std::ffi::CStr;

/// Represents a reference to a Python `type object`.
#[repr(transparent)]
Expand Down Expand Up @@ -39,8 +37,11 @@ impl PyType {
}

/// Gets the name of the `PyType`.
pub fn name(&self) -> Cow<str> {
unsafe { CStr::from_ptr((*self.as_type_ptr()).tp_name).to_string_lossy() }
pub fn name(&self) -> &str {
self.getattr("__qualname__")
.expect("Error accessing __qualname__")
.extract()
.expect("__qualname__ not a string")
}

/// Checks whether `self` is subclass of type `T`.
Expand Down

0 comments on commit 588f99c

Please sign in to comment.