@@ -1914,23 +1914,39 @@ mod _ssl {
19141914 "certificate verify failed" => "CERTIFICATE_VERIFY_FAILED" ,
19151915 _ => default_errstr,
19161916 } ;
1917- let msg = if let Some ( lib ) = e . library ( ) {
1918- // add `library` attribute
1919- let attr_name = vm . ctx . as_ref ( ) . intern_str ( " library" ) ;
1920- cls . set_attr ( attr_name , vm . ctx . new_str ( lib) . into ( ) ) ;
1917+
1918+ // Build message
1919+ let lib_obj = e . library ( ) ;
1920+ let msg = if let Some ( lib) = lib_obj {
19211921 format ! ( "[{lib}] {errstr} ({file}:{line})" )
19221922 } else {
19231923 format ! ( "{errstr} ({file}:{line})" )
19241924 } ;
1925- // add `reason` attribute
1926- let attr_name = vm. ctx . as_ref ( ) . intern_str ( "reason" ) ;
1927- cls. set_attr ( attr_name, vm. ctx . new_str ( errstr) . into ( ) ) ;
19281925
1926+ // Create exception instance
19291927 let reason = sys:: ERR_GET_REASON ( e. code ( ) ) ;
1930- vm. new_exception (
1928+ let exc = vm. new_exception (
19311929 cls,
19321930 vec ! [ vm. ctx. new_int( reason) . into( ) , vm. ctx. new_str( msg) . into( ) ] ,
1933- )
1931+ ) ;
1932+
1933+ // Set attributes on instance, not class
1934+ let exc_obj: PyObjectRef = exc. into ( ) ;
1935+
1936+ // Set reason attribute (always set, even if just the error string)
1937+ let reason_value = vm. ctx . new_str ( errstr) ;
1938+ let _ = exc_obj. set_attr ( "reason" , reason_value, vm) ;
1939+
1940+ // Set library attribute (None if not available)
1941+ let library_value: PyObjectRef = if let Some ( lib) = lib_obj {
1942+ vm. ctx . new_str ( lib) . into ( )
1943+ } else {
1944+ vm. ctx . none ( )
1945+ } ;
1946+ let _ = exc_obj. set_attr ( "library" , library_value, vm) ;
1947+
1948+ // Convert back to PyBaseExceptionRef
1949+ exc_obj. downcast ( ) . unwrap ( )
19341950 }
19351951 None => vm. new_exception_empty ( cls) ,
19361952 }
0 commit comments