1
1
use proc_macro:: Span ;
2
2
use proc_macro2:: TokenStream ;
3
- use pyo3:: { PyAny , PyErr , PyResult , PyTypeInfo , Python , ToPyObject } ;
3
+ use pyo3:: { prelude :: * , types :: PyTraceback , Bound , PyErr , PyResult , PyTypeInfo , Python , ToPyObject } ;
4
4
use quote:: { quote, quote_spanned} ;
5
5
6
6
/// Format a nice error message for a python compilation error.
7
7
pub fn compile_error_msg ( py : Python , error : PyErr , tokens : TokenStream ) -> TokenStream {
8
8
let value = error. to_object ( py) ;
9
9
10
10
if value. is_none ( py) {
11
- let error = format ! ( "python: {}" , error. get_type ( py) . name( ) . unwrap( ) ) ;
11
+ let error = format ! ( "python: {}" , error. get_type_bound ( py) . name( ) . unwrap( ) ) ;
12
12
return quote ! ( compile_error! { #error} ) ;
13
13
}
14
14
15
- if error. matches ( py, pyo3:: exceptions:: PySyntaxError :: type_object ( py) ) {
15
+ if error. matches ( py, pyo3:: exceptions:: PySyntaxError :: type_object_bound ( py) ) {
16
16
let line: Option < usize > = value. getattr ( py, "lineno" ) . ok ( ) . and_then ( |x| x. extract ( py) . ok ( ) ) ;
17
17
let msg: Option < String > = value. getattr ( py, "msg" ) . ok ( ) . and_then ( |x| x. extract ( py) . ok ( ) ) ;
18
18
if let ( Some ( line) , Some ( msg) ) = ( line, msg) {
@@ -23,10 +23,10 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
23
23
}
24
24
}
25
25
26
- if let Some ( tb) = & error. traceback ( py) {
26
+ if let Some ( tb) = & error. traceback_bound ( py) {
27
27
if let Ok ( ( file, line) ) = get_traceback_info ( tb) {
28
28
if file == Span :: call_site ( ) . source_file ( ) . path ( ) . to_string_lossy ( ) {
29
- if let Ok ( msg) = value. as_ref ( py) . str ( ) {
29
+ if let Ok ( msg) = value. bind ( py) . str ( ) {
30
30
if let Some ( span) = span_for_line ( tokens, line) {
31
31
let error = format ! ( "python: {}" , msg) ;
32
32
return quote_spanned ! ( span. into( ) => compile_error!{ #error} ) ;
@@ -36,11 +36,11 @@ pub fn compile_error_msg(py: Python, error: PyErr, tokens: TokenStream) -> Token
36
36
}
37
37
}
38
38
39
- let error = format ! ( "python: {}" , value. as_ref ( py) . str ( ) . unwrap( ) ) ;
39
+ let error = format ! ( "python: {}" , value. bind ( py) . str ( ) . unwrap( ) ) ;
40
40
quote ! ( compile_error! { #error} )
41
41
}
42
42
43
- fn get_traceback_info ( tb : & PyAny ) -> PyResult < ( String , usize ) > {
43
+ fn get_traceback_info ( tb : & Bound < ' _ , PyTraceback > ) -> PyResult < ( String , usize ) > {
44
44
let frame = tb. getattr ( "tb_frame" ) ?;
45
45
let code = frame. getattr ( "f_code" ) ?;
46
46
let file: String = code. getattr ( "co_filename" ) ?. extract ( ) ?;
0 commit comments