@@ -4,7 +4,7 @@ use crate::type_object::PyTypeInfo;
4
4
use crate :: types:: any:: PyAnyMethods ;
5
5
use crate :: types:: {
6
6
string:: PyStringMethods , traceback:: PyTracebackMethods , typeobject:: PyTypeMethods , PyTraceback ,
7
- PyType ,
7
+ PyTuple , PyTupleMethods , PyType ,
8
8
} ;
9
9
use crate :: {
10
10
exceptions:: { self , PyBaseException } ,
@@ -59,17 +59,7 @@ impl<'a, 'py> DowncastError<'a, 'py> {
59
59
}
60
60
}
61
61
62
- pub ( crate ) fn new_from_borrowed (
63
- from : Borrowed < ' a , ' py , PyAny > ,
64
- to : impl Into < Cow < ' static , str > > ,
65
- ) -> Self {
66
- Self {
67
- from,
68
- to : TypeNameOrValue :: Name ( to. into ( ) ) ,
69
- }
70
- }
71
-
72
- pub ( crate ) fn new_from_type ( from : Borrowed < ' a , ' py , PyAny > , to : Bound < ' py , PyType > ) -> Self {
62
+ pub ( crate ) fn new_from_type ( from : Borrowed < ' a , ' py , PyAny > , to : Bound < ' py , PyAny > ) -> Self {
73
63
Self {
74
64
from,
75
65
to : TypeNameOrValue :: Value ( to) ,
@@ -94,7 +84,7 @@ impl<'py> DowncastIntoError<'py> {
94
84
}
95
85
}
96
86
97
- pub ( crate ) fn new_from_type ( from : Bound < ' py , PyAny > , to : Bound < ' py , PyType > ) -> Self {
87
+ pub ( crate ) fn new_from_type ( from : Bound < ' py , PyAny > , to : Bound < ' py , PyAny > ) -> Self {
98
88
Self {
99
89
from,
100
90
to : TypeNameOrValue :: Value ( to) ,
@@ -114,7 +104,7 @@ impl<'py> DowncastIntoError<'py> {
114
104
#[ derive( Debug ) ]
115
105
enum TypeNameOrValue < ' py > {
116
106
Name ( Cow < ' static , str > ) ,
117
- Value ( Bound < ' py , PyType > ) ,
107
+ Value ( Bound < ' py , PyAny > ) ,
118
108
}
119
109
/// Helper conversion trait that allows to use custom arguments for lazy exception construction.
120
110
pub trait PyErrArguments : Send + Sync {
@@ -766,7 +756,7 @@ impl PyErrArguments for PyDowncastErrorArguments {
766
756
767
757
enum OwnedTypeNameOrValue {
768
758
Name ( Cow < ' static , str > ) ,
769
- Value ( Py < PyType > ) ,
759
+ Value ( Py < PyAny > ) ,
770
760
}
771
761
772
762
/// Python exceptions that can be converted to [`PyErr`].
@@ -850,11 +840,24 @@ impl std::fmt::Display for TypeNameOrValue<'_> {
850
840
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
851
841
match self {
852
842
Self :: Name ( name) => name. fmt ( f) ,
853
- Self :: Value ( t) => t
854
- . qualname ( )
855
- . map_err ( |_| std:: fmt:: Error ) ?
856
- . to_string_lossy ( )
857
- . fmt ( f) ,
843
+ Self :: Value ( t) => {
844
+ if let Ok ( t) = t. downcast :: < PyType > ( ) {
845
+ t. qualname ( )
846
+ . map_err ( |_| std:: fmt:: Error ) ?
847
+ . to_string_lossy ( )
848
+ . fmt ( f)
849
+ } else if let Ok ( t) = t. downcast :: < PyTuple > ( ) {
850
+ for ( i, t) in t. iter ( ) . enumerate ( ) {
851
+ if i > 0 {
852
+ f. write_str ( " | " ) ?;
853
+ }
854
+ TypeNameOrValue :: Value ( t) . fmt ( f) ?;
855
+ }
856
+ Ok ( ( ) )
857
+ } else {
858
+ t. fmt ( f)
859
+ }
860
+ }
858
861
}
859
862
}
860
863
}
0 commit comments