Merge pull request #1166 from alex/abi3-name

Make PyType::name abi3 compatible
This commit is contained in:
Yuji Kanagawa 2020-09-09 14:16:07 +09:00 committed by GitHub
commit a009c23bb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 18 deletions

View File

@ -408,8 +408,8 @@ impl Model for UserModel {
.call_method("get_results", (), None)
.unwrap();
if py_result.get_type().name() != "list" {
panic!("Expected a list for the get_results() method signature, got {}", py_result.get_type().name());
if py_result.get_type().name().unwrap() != "list" {
panic!("Expected a list for the get_results() method signature, got {}", py_result.get_type().name().unwrap());
}
py_result.extract()
})
@ -536,8 +536,8 @@ impl Model for UserModel {
.call_method("get_results", (), None)
.unwrap();
if py_result.get_type().name() != "list" {
panic!("Expected a list for the get_results() method signature, got {}", py_result.get_type().name());
if py_result.get_type().name().unwrap() != "list" {
panic!("Expected a list for the get_results() method signature, got {}", py_result.get_type().name().unwrap());
}
py_result.extract()
})

View File

@ -72,7 +72,7 @@ impl<'a> Enum<'a> {
};
quote!(
#(#var_extracts)*
let type_name = obj.get_type().name();
let type_name = obj.get_type().name()?;
let from = obj
.repr()
.map(|s| format!("{} ({})", s.to_string_lossy(), type_name))

View File

@ -478,10 +478,13 @@ impl<'a> std::fmt::Display for PyDowncastError<'a> {
write!(
f,
"Can't convert {} to {}",
self.from
.repr()
.map(|s| s.to_string_lossy())
.unwrap_or_else(|_| self.from.get_type().name()),
self.from.repr().map(|s| s.to_string_lossy()).or_else(|_| {
self.from
.get_type()
.name()
.map_err(|_| std::fmt::Error)
.map(|s| s.into())
})?,
self.to
)
}

View File

@ -38,8 +38,8 @@ macro_rules! impl_exception_boilerplate {
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let type_name = self.get_type().name();
f.debug_struct(&*type_name)
let type_name = self.get_type().name().map_err(|_| std::fmt::Error)?;
f.debug_struct(type_name)
// TODO: print out actual fields!
.finish()
}
@ -47,7 +47,7 @@ macro_rules! impl_exception_boilerplate {
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let type_name = self.get_type().name();
let type_name = self.get_type().name().map_err(|_| std::fmt::Error)?;
write!(f, "{}", type_name)?;
if let Ok(s) = self.str() {
write!(f, ": {}", &s.to_string_lossy())

View File

@ -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)]
@ -39,8 +37,8 @@ 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) -> PyResult<&str> {
self.getattr("__qualname__")?.extract()
}
/// Checks whether `self` is subclass of type `T`.

View File

@ -76,7 +76,7 @@ impl ClassMethod {
#[classmethod]
/// Test class method.
fn method(cls: &PyType) -> PyResult<String> {
Ok(format!("{}.method()!", cls.name()))
Ok(format!("{}.method()!", cls.name()?))
}
}
@ -104,7 +104,7 @@ struct ClassMethodWithArgs {}
impl ClassMethodWithArgs {
#[classmethod]
fn method(cls: &PyType, input: &PyString) -> PyResult<String> {
Ok(format!("{}.method({})", cls.name(), input))
Ok(format!("{}.method({})", cls.name()?, input))
}
}