@@ -455,7 +455,6 @@ fn create_normalized_exception<'py>(
455455
456456#[ cfg( test) ]
457457mod tests {
458-
459458 use crate :: {
460459 exceptions:: PyValueError , sync:: PyOnceLock , Py , PyAny , PyErr , PyErrArguments , Python ,
461460 } ;
@@ -559,4 +558,59 @@ mod tests {
559558 assert ! ( context. is_instance_of:: <PyRuntimeError >( py) )
560559 } )
561560 }
561+
562+ #[ test]
563+ #[ cfg( Py_3_12 ) ]
564+ fn compare_create_normalized_exception_with_pyerr_setobject ( ) {
565+ use crate :: {
566+ conversion:: IntoPyObjectExt , err:: err_state:: PyErrStateNormalized ,
567+ exceptions:: PyRuntimeError , ffi, type_object:: PyTypeInfo , types:: any:: PyAnyMethods ,
568+ Bound ,
569+ } ;
570+
571+ fn test_exception < ' py > ( ptype : & Bound < ' py , PyAny > , pvalue : Bound < ' py , PyAny > ) {
572+ let py = ptype. py ( ) ;
573+
574+ let exc1 = super :: create_normalized_exception ( ptype, pvalue. clone ( ) ) ;
575+
576+ unsafe {
577+ ffi:: PyErr_SetObject ( ptype. as_ptr ( ) , pvalue. as_ptr ( ) ) ;
578+ }
579+ let exc2 = PyErrStateNormalized :: take ( py)
580+ . unwrap ( )
581+ . pvalue
582+ . into_bound ( py) ;
583+
584+ let err1 = PyErr :: from_value ( exc1. into_any ( ) ) ;
585+ let err2 = PyErr :: from_value ( exc2. into_any ( ) ) ;
586+
587+ assert ! ( err1. get_type( py) . is( err2. get_type( py) ) ) ;
588+ assert ! ( err1. context( py) . xor( err2. context( py) ) . is_none( ) ) ;
589+ assert ! ( err1. traceback( py) . xor( err2. traceback( py) ) . is_none( ) ) ;
590+ assert ! ( err1. cause( py) . xor( err2. cause( py) ) . is_none( ) ) ;
591+ assert_eq ! ( err1. to_string( ) , err2. to_string( ) ) ;
592+ }
593+
594+ Python :: attach ( |py| {
595+ test_exception ( & PyRuntimeError :: type_object ( py) , py. None ( ) . into_bound ( py) ) ;
596+
597+ test_exception (
598+ & PyRuntimeError :: type_object ( py) ,
599+ "Boom" . into_bound_py_any ( py) . unwrap ( ) ,
600+ ) ;
601+
602+ test_exception (
603+ & PyRuntimeError :: type_object ( py) ,
604+ ( 3 , 2 , 1 , "Boom" ) . into_bound_py_any ( py) . unwrap ( ) ,
605+ ) ;
606+
607+ test_exception (
608+ & PyRuntimeError :: type_object ( py) ,
609+ PyRuntimeError :: new_err ( "Boom" )
610+ . into_value ( py)
611+ . into_any ( )
612+ . into_bound ( py) ,
613+ ) ;
614+ } )
615+ }
562616}
0 commit comments