@@ -13,6 +13,22 @@ use crate::{
13
13
vm:: VirtualMachine ,
14
14
} ;
15
15
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
+
16
32
/// Collection of object creation helpers
17
33
impl VirtualMachine {
18
34
/// Create a new python object
@@ -125,16 +141,6 @@ impl VirtualMachine {
125
141
)
126
142
}
127
143
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
-
138
144
pub fn new_no_attribute_error ( & self , obj : PyObjectRef , name : PyStrRef ) -> PyBaseExceptionRef {
139
145
let msg = format ! (
140
146
"'{}' object has no attribute '{}'" ,
@@ -149,11 +155,6 @@ impl VirtualMachine {
149
155
attribute_error
150
156
}
151
157
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
-
157
158
pub fn new_name_error ( & self , msg : String , name : PyStrRef ) -> PyBaseExceptionRef {
158
159
let name_error_type = self . ctx . exceptions . name_error . to_owned ( ) ;
159
160
let name_error = self . new_exception_msg ( name_error_type, msg) ;
@@ -199,11 +200,6 @@ impl VirtualMachine {
199
200
) )
200
201
}
201
202
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
-
207
203
pub fn new_errno_error ( & self , errno : i32 , msg : String ) -> PyBaseExceptionRef {
208
204
let vm = self ;
209
205
let exc_type =
@@ -213,17 +209,6 @@ impl VirtualMachine {
213
209
vm. new_exception ( exc_type. to_owned ( ) , vec ! [ errno_obj, vm. new_pyobj( msg) ] )
214
210
}
215
211
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
-
227
212
pub fn new_unicode_decode_error_real (
228
213
& self ,
229
214
encoding : PyStrRef ,
@@ -254,12 +239,6 @@ impl VirtualMachine {
254
239
exc
255
240
}
256
241
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
-
263
242
pub fn new_unicode_encode_error_real (
264
243
& self ,
265
244
encoding : PyStrRef ,
@@ -290,49 +269,12 @@ impl VirtualMachine {
290
269
exc
291
270
}
292
271
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
-
305
272
// TODO: don't take ownership should make the success path faster
306
273
pub fn new_key_error ( & self , obj : PyObjectRef ) -> PyBaseExceptionRef {
307
274
let key_error = self . ctx . exceptions . key_error . to_owned ( ) ;
308
275
self . new_exception ( key_error, vec ! [ obj] )
309
276
}
310
277
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
-
336
278
#[ cfg( any( feature = "parser" , feature = "compiler" ) ) ]
337
279
pub fn new_syntax_error_maybe_incomplete (
338
280
& self ,
@@ -531,16 +473,6 @@ impl VirtualMachine {
531
473
exc
532
474
}
533
475
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
-
544
476
pub fn new_stop_iteration ( & self , value : Option < PyObjectRef > ) -> PyBaseExceptionRef {
545
477
let dict = self . ctx . new_dict ( ) ;
546
478
let args = if let Some ( value) = value {
@@ -607,8 +539,31 @@ impl VirtualMachine {
607
539
)
608
540
}
609
541
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 ) ;
614
569
}
0 commit comments