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