@@ -13,6 +13,22 @@ use crate::{
1313 vm:: VirtualMachine ,
1414} ;
1515
16+ macro_rules! define_exception_fn {
17+ (
18+ fn $fn_name: ident, $attr: ident, $python_repr: ident
19+ ) => {
20+ #[ doc = concat!(
21+ "Create a new python " ,
22+ stringify!( $python_repr) ,
23+ " object.\n Useful for raising errors from python functions implemented in rust."
24+ ) ]
25+ pub fn $fn_name( & self , msg: String ) -> PyBaseExceptionRef {
26+ let err = self . ctx. exceptions. $attr. to_owned( ) ;
27+ self . new_exception_msg( err, msg)
28+ }
29+ } ;
30+ }
31+
1632/// Collection of object creation helpers
1733impl VirtualMachine {
1834 /// Create a new python object
@@ -125,16 +141,6 @@ impl VirtualMachine {
125141 )
126142 }
127143
128- pub fn new_lookup_error ( & self , msg : String ) -> PyBaseExceptionRef {
129- let lookup_error = self . ctx . exceptions . lookup_error . to_owned ( ) ;
130- self . new_exception_msg ( lookup_error, msg)
131- }
132-
133- pub fn new_attribute_error ( & self , msg : String ) -> PyBaseExceptionRef {
134- let attribute_error = self . ctx . exceptions . attribute_error . to_owned ( ) ;
135- self . new_exception_msg ( attribute_error, msg)
136- }
137-
138144 pub fn new_no_attribute_error ( & self , obj : PyObjectRef , name : PyStrRef ) -> PyBaseExceptionRef {
139145 let msg = format ! (
140146 "'{}' object has no attribute '{}'" ,
@@ -149,11 +155,6 @@ impl VirtualMachine {
149155 attribute_error
150156 }
151157
152- pub fn new_type_error ( & self , msg : String ) -> PyBaseExceptionRef {
153- let type_error = self . ctx . exceptions . type_error . to_owned ( ) ;
154- self . new_exception_msg ( type_error, msg)
155- }
156-
157158 pub fn new_name_error ( & self , msg : String , name : PyStrRef ) -> PyBaseExceptionRef {
158159 let name_error_type = self . ctx . exceptions . name_error . to_owned ( ) ;
159160 let name_error = self . new_exception_msg ( name_error_type, msg) ;
@@ -199,11 +200,6 @@ impl VirtualMachine {
199200 ) )
200201 }
201202
202- pub fn new_os_error ( & self , msg : String ) -> PyBaseExceptionRef {
203- let os_error = self . ctx . exceptions . os_error . to_owned ( ) ;
204- self . new_exception_msg ( os_error, msg)
205- }
206-
207203 pub fn new_errno_error ( & self , errno : i32 , msg : String ) -> PyBaseExceptionRef {
208204 let vm = self ;
209205 let exc_type =
@@ -213,17 +209,6 @@ impl VirtualMachine {
213209 vm. new_exception ( exc_type. to_owned ( ) , vec ! [ errno_obj, vm. new_pyobj( msg) ] )
214210 }
215211
216- pub fn new_system_error ( & self , msg : String ) -> PyBaseExceptionRef {
217- let sys_error = self . ctx . exceptions . system_error . to_owned ( ) ;
218- self . new_exception_msg ( sys_error, msg)
219- }
220-
221- // TODO: remove & replace with new_unicode_decode_error_real
222- pub fn new_unicode_decode_error ( & self , msg : String ) -> PyBaseExceptionRef {
223- let unicode_decode_error = self . ctx . exceptions . unicode_decode_error . to_owned ( ) ;
224- self . new_exception_msg ( unicode_decode_error, msg)
225- }
226-
227212 pub fn new_unicode_decode_error_real (
228213 & self ,
229214 encoding : PyStrRef ,
@@ -254,12 +239,6 @@ impl VirtualMachine {
254239 exc
255240 }
256241
257- // TODO: remove & replace with new_unicode_encode_error_real
258- pub fn new_unicode_encode_error ( & self , msg : String ) -> PyBaseExceptionRef {
259- let unicode_encode_error = self . ctx . exceptions . unicode_encode_error . to_owned ( ) ;
260- self . new_exception_msg ( unicode_encode_error, msg)
261- }
262-
263242 pub fn new_unicode_encode_error_real (
264243 & self ,
265244 encoding : PyStrRef ,
@@ -290,49 +269,12 @@ impl VirtualMachine {
290269 exc
291270 }
292271
293- /// Create a new python ValueError object. Useful for raising errors from
294- /// python functions implemented in rust.
295- pub fn new_value_error ( & self , msg : String ) -> PyBaseExceptionRef {
296- let value_error = self . ctx . exceptions . value_error . to_owned ( ) ;
297- self . new_exception_msg ( value_error, msg)
298- }
299-
300- pub fn new_buffer_error ( & self , msg : String ) -> PyBaseExceptionRef {
301- let buffer_error = self . ctx . exceptions . buffer_error . to_owned ( ) ;
302- self . new_exception_msg ( buffer_error, msg)
303- }
304-
305272 // TODO: don't take ownership should make the success path faster
306273 pub fn new_key_error ( & self , obj : PyObjectRef ) -> PyBaseExceptionRef {
307274 let key_error = self . ctx . exceptions . key_error . to_owned ( ) ;
308275 self . new_exception ( key_error, vec ! [ obj] )
309276 }
310277
311- pub fn new_index_error ( & self , msg : String ) -> PyBaseExceptionRef {
312- let index_error = self . ctx . exceptions . index_error . to_owned ( ) ;
313- self . new_exception_msg ( index_error, msg)
314- }
315-
316- pub fn new_not_implemented_error ( & self , msg : String ) -> PyBaseExceptionRef {
317- let not_implemented_error = self . ctx . exceptions . not_implemented_error . to_owned ( ) ;
318- self . new_exception_msg ( not_implemented_error, msg)
319- }
320-
321- pub fn new_recursion_error ( & self , msg : String ) -> PyBaseExceptionRef {
322- let recursion_error = self . ctx . exceptions . recursion_error . to_owned ( ) ;
323- self . new_exception_msg ( recursion_error, msg)
324- }
325-
326- pub fn new_zero_division_error ( & self , msg : String ) -> PyBaseExceptionRef {
327- let zero_division_error = self . ctx . exceptions . zero_division_error . to_owned ( ) ;
328- self . new_exception_msg ( zero_division_error, msg)
329- }
330-
331- pub fn new_overflow_error ( & self , msg : String ) -> PyBaseExceptionRef {
332- let overflow_error = self . ctx . exceptions . overflow_error . to_owned ( ) ;
333- self . new_exception_msg ( overflow_error, msg)
334- }
335-
336278 #[ cfg( any( feature = "parser" , feature = "compiler" ) ) ]
337279 pub fn new_syntax_error_maybe_incomplete (
338280 & self ,
@@ -531,16 +473,6 @@ impl VirtualMachine {
531473 exc
532474 }
533475
534- pub fn new_runtime_error ( & self , msg : String ) -> PyBaseExceptionRef {
535- let runtime_error = self . ctx . exceptions . runtime_error . to_owned ( ) ;
536- self . new_exception_msg ( runtime_error, msg)
537- }
538-
539- pub fn new_memory_error ( & self , msg : String ) -> PyBaseExceptionRef {
540- let memory_error_type = self . ctx . exceptions . memory_error . to_owned ( ) ;
541- self . new_exception_msg ( memory_error_type, msg)
542- }
543-
544476 pub fn new_stop_iteration ( & self , value : Option < PyObjectRef > ) -> PyBaseExceptionRef {
545477 let dict = self . ctx . new_dict ( ) ;
546478 let args = if let Some ( value) = value {
@@ -607,8 +539,31 @@ impl VirtualMachine {
607539 )
608540 }
609541
610- pub fn new_eof_error ( & self , msg : String ) -> PyBaseExceptionRef {
611- let eof_error = self . ctx . exceptions . eof_error . to_owned ( ) ;
612- self . new_exception_msg ( eof_error, msg)
613- }
542+ define_exception_fn ! ( fn new_lookup_error, lookup_error, LookupError ) ;
543+ define_exception_fn ! ( fn new_eof_error, eof_error, EOFError ) ;
544+ define_exception_fn ! ( fn new_attribute_error, attribute_error, AttributeError ) ;
545+ define_exception_fn ! ( fn new_type_error, type_error, TypeError ) ;
546+ define_exception_fn ! ( fn new_os_error, os_error, OSError ) ;
547+ define_exception_fn ! ( fn new_system_error, system_error, SystemError ) ;
548+
549+ // TODO: remove & replace with new_unicode_decode_error_real
550+ define_exception_fn ! ( fn new_unicode_decode_error, unicode_decode_error, UnicodeDecodeError ) ;
551+
552+ // TODO: remove & replace with new_unicode_encode_error_real
553+ define_exception_fn ! ( fn new_unicode_encode_error, unicode_encode_error, UnicodeEncodeError ) ;
554+
555+ define_exception_fn ! ( fn new_value_error, value_error, ValueError ) ;
556+
557+ define_exception_fn ! ( fn new_buffer_error, buffer_error, BufferError ) ;
558+ define_exception_fn ! ( fn new_index_error, index_error, IndexError ) ;
559+ define_exception_fn ! (
560+ fn new_not_implemented_error,
561+ not_implemented_error,
562+ NotImplementedError
563+ ) ;
564+ define_exception_fn ! ( fn new_recursion_error, recursion_error, RecursionError ) ;
565+ define_exception_fn ! ( fn new_zero_division_error, zero_division_error, ZeroDivisionError ) ;
566+ define_exception_fn ! ( fn new_overflow_error, overflow_error, OverflowError ) ;
567+ define_exception_fn ! ( fn new_runtime_error, runtime_error, RuntimeError ) ;
568+ define_exception_fn ! ( fn new_memory_error, memory_error, MemoryError ) ;
614569}
0 commit comments