blueloveTH 2 yıl önce
ebeveyn
işleme
5ec1622887
3 değiştirilmiş dosya ile 22 ekleme ve 5 silme
  1. 2 2
      src/pocketpy.cpp
  2. 19 3
      tests/41_exception.py
  3. 1 0
      tests/70_collections.py

+ 2 - 2
src/pocketpy.cpp

@@ -1551,14 +1551,14 @@ void add_module_traceback(VM* vm){
     PyObject* mod = vm->new_module("traceback");
     vm->bind_func<0>(mod, "print_exc", [](VM* vm, ArgsView args) {
         if(vm->_last_exception==nullptr) vm->ValueError("no exception");
-        Exception& e = CAST(Exception&, vm->_last_exception);
+        Exception& e = _CAST(Exception&, vm->_last_exception);
         vm->stdout_write(e.summary());
         return vm->None;
     });
 
     vm->bind_func<0>(mod, "format_exc", [](VM* vm, ArgsView args) {
         if(vm->_last_exception==nullptr) vm->ValueError("no exception");
-        Exception& e = CAST(Exception&, vm->_last_exception);
+        Exception& e = _CAST(Exception&, vm->_last_exception);
         return VAR(e.summary());
     });
 }

+ 19 - 3
tests/41_exception.py

@@ -38,7 +38,7 @@ assert True
 def f():
     try:
         raise KeyError('foo')
-    except A:   # will fail to catch
+    except IndexError:   # will fail to catch
         exit(1)
     except:
         pass
@@ -52,9 +52,9 @@ def f1():
         try:
             a = {1: 2, 3: 4}
             x = a[0]
-        except A:
+        except RuntimeError:
             exit(1)
-    except B:
+    except IndexError:
         exit(1)
     exit(1)
 
@@ -94,8 +94,24 @@ try:
 except IndexError as e:
     exit(1)
 except Exception as e:
+    assert type(e) is KeyError
     assert str(e) == '2'
     assert repr(e).startswith('KeyError(')
 except:
     exit(1)
 
+
+class MyException(Exception):
+    pass
+
+class MyException2(MyException):
+    pass
+
+try:
+    raise MyException2
+except MyException as e:
+    ok = True
+except Exception:
+    exit(1)
+
+assert ok

+ 1 - 0
tests/70_collections.py

@@ -153,6 +153,7 @@ try:
 except TypeError:
     pass
 
+class ArithmeticError(Exception): pass
 
 class BadCompare:
     def __eq__(self, other):