@@ -106,24 +106,38 @@ impl VirtualMachine {
106106 )
107107 }
108108
109+ pub fn new_os_error ( & self , msg : impl ToPyObject ) -> PyRef < PyBaseException > {
110+ self . new_os_subtype_error ( self . ctx . exceptions . os_error . to_owned ( ) , None , msg)
111+ . upcast ( )
112+ }
113+
109114 pub fn new_os_subtype_error (
110115 & self ,
111116 exc_type : PyTypeRef ,
112117 errno : Option < i32 > ,
113118 msg : impl ToPyObject ,
114119 ) -> PyRef < PyOSError > {
115120 debug_assert_eq ! ( exc_type. slots. basicsize, std:: mem:: size_of:: <PyOSError >( ) ) ;
121+ let msg = msg. to_pyobject ( self ) ;
122+
123+ fn new_os_subtype_error_impl (
124+ vm : & VirtualMachine ,
125+ exc_type : PyTypeRef ,
126+ errno : Option < i32 > ,
127+ msg : PyObjectRef ,
128+ ) -> PyRef < PyOSError > {
129+ let args = match errno {
130+ Some ( e) => vec ! [ vm. new_pyobj( e) , msg] ,
131+ None => vec ! [ msg] ,
132+ } ;
133+ let payload =
134+ PyOSError :: py_new ( & exc_type, args. into ( ) , vm) . expect ( "new_os_error usage error" ) ;
135+ payload
136+ . into_ref_with_type ( vm, exc_type)
137+ . expect ( "new_os_error usage error" )
138+ }
116139
117- let errno_obj: PyObjectRef = match errno {
118- Some ( e) => self . new_pyobj ( e) ,
119- None => self . ctx . none ( ) ,
120- } ;
121- let args = vec ! [ errno_obj, msg. to_pyobject( self ) ] ;
122- let payload =
123- PyOSError :: py_new ( & exc_type, args. into ( ) , self ) . expect ( "new_os_error usage error" ) ;
124- payload
125- . into_ref_with_type ( self , exc_type)
126- . expect ( "new_os_error usage error" )
140+ new_os_subtype_error_impl ( self , exc_type, errno, msg)
127141 }
128142
129143 /// Instantiate an exception with no arguments.
@@ -582,7 +596,6 @@ impl VirtualMachine {
582596 define_exception_fn ! ( fn new_eof_error, eof_error, EOFError ) ;
583597 define_exception_fn ! ( fn new_attribute_error, attribute_error, AttributeError ) ;
584598 define_exception_fn ! ( fn new_type_error, type_error, TypeError ) ;
585- define_exception_fn ! ( fn new_os_error, os_error, OSError ) ;
586599 define_exception_fn ! ( fn new_system_error, system_error, SystemError ) ;
587600
588601 // TODO: remove & replace with new_unicode_decode_error_real
0 commit comments