File tree 3 files changed +59
-0
lines changed
3 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ # KeyError
1
2
empty_exc = KeyError ()
2
3
assert str (empty_exc ) == ''
3
4
assert repr (empty_exc ) == 'KeyError()'
@@ -23,3 +24,22 @@ def __str__(self):
23
24
exc = KeyError (A ())
24
25
assert str (exc ) == 'repr'
25
26
assert repr (exc ) == 'KeyError(repr,)'
27
+
28
+ # ImportError / ModuleNotFoundError
29
+ exc = ImportError ()
30
+ assert exc .name is None
31
+ assert exc .path is None
32
+ assert exc .msg is None
33
+ assert exc .args == ()
34
+
35
+ exc = ImportError ('hello' )
36
+ assert exc .name is None
37
+ assert exc .path is None
38
+ assert exc .msg == 'hello'
39
+ assert exc .args == ('hello' ,)
40
+
41
+ exc = ImportError ('hello' , name = 'name' , path = 'path' )
42
+ assert exc .name == 'name'
43
+ assert exc .path == 'path'
44
+ assert exc .msg == 'hello'
45
+ assert exc .args == ('hello' ,)
Original file line number Diff line number Diff line change 25
25
except ImportError :
26
26
pass
27
27
28
+ try :
29
+ import mymodule
30
+ except ModuleNotFoundError as exc :
31
+ assert exc .name == 'mymodule'
32
+
28
33
29
34
test = __import__ ("import_target" )
30
35
assert test .X == import_target .X
Original file line number Diff line number Diff line change @@ -312,6 +312,35 @@ impl ExceptionZoo {
312
312
}
313
313
}
314
314
315
+ fn import_error_init ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
316
+ // TODO: call super().__init__(*args) instead
317
+ exception_init ( vm, args. clone ( ) ) ?;
318
+
319
+ let exc_self = args. args [ 0 ] . clone ( ) ;
320
+ vm. set_attr (
321
+ & exc_self,
322
+ "name" ,
323
+ args. kwargs
324
+ . get ( "name" )
325
+ . cloned ( )
326
+ . unwrap_or_else ( || vm. get_none ( ) ) ,
327
+ ) ?;
328
+ vm. set_attr (
329
+ & exc_self,
330
+ "path" ,
331
+ args. kwargs
332
+ . get ( "path" )
333
+ . cloned ( )
334
+ . unwrap_or_else ( || vm. get_none ( ) ) ,
335
+ ) ?;
336
+ vm. set_attr (
337
+ & exc_self,
338
+ "msg" ,
339
+ args. args . get ( 1 ) . cloned ( ) . unwrap_or_else ( || vm. get_none ( ) ) ,
340
+ ) ?;
341
+ Ok ( vm. get_none ( ) )
342
+ }
343
+
315
344
pub fn init ( context : & PyContext ) {
316
345
let base_exception_type = & context. exceptions . base_exception_type ;
317
346
extend_class ! ( context, base_exception_type, {
@@ -323,4 +352,9 @@ pub fn init(context: &PyContext) {
323
352
"__str__" => context. new_rustfunc( exception_str) ,
324
353
"__repr__" => context. new_rustfunc( exception_repr) ,
325
354
} ) ;
355
+
356
+ let import_error_type = & context. exceptions . import_error ;
357
+ extend_class ! ( context, import_error_type, {
358
+ "__init__" => context. new_rustfunc( import_error_init)
359
+ } ) ;
326
360
}
You can’t perform that action at this time.
0 commit comments