From 721c55dced099a797f0910839c7c4f9ac7599ed4 Mon Sep 17 00:00:00 2001 From: Rachel Dowdall Date: Sat, 22 Mar 2014 15:28:16 +0000 Subject: [PATCH] Added exception hierarchy except for OSError and UnicodeError (requires arguments). Comment out the errors that aren't needed if memory becomes an issue. --- py/obj.h | 48 ++++- py/objexcept.c | 87 +++++++-- py/qstrdefs.h | 49 ++++- py/runtime.c | 36 +++- tests/basics/exceptpoly.py | 380 +++++++++++++++++++++++++++++++++++++ 5 files changed, 574 insertions(+), 26 deletions(-) create mode 100644 tests/basics/exceptpoly.py diff --git a/py/obj.h b/py/obj.h index 8d40c3f2d..3217c4a4d 100644 --- a/py/obj.h +++ b/py/obj.h @@ -184,9 +184,53 @@ struct _mp_obj_type_t { typedef struct _mp_obj_type_t mp_obj_type_t; // Constant types, globally accessible - extern const mp_obj_type_t mp_type_type; + +// Exceptions extern const mp_obj_type_t mp_type_BaseException; +extern const mp_obj_type_t mp_type_ArithmeticError; +extern const mp_obj_type_t mp_type_AssertionError; +extern const mp_obj_type_t mp_type_AttributeError; +extern const mp_obj_type_t mp_type_BufferError; +extern const mp_obj_type_t mp_type_BytesWarning; +extern const mp_obj_type_t mp_type_DeprecationWarning; +extern const mp_obj_type_t mp_type_EOFError; +extern const mp_obj_type_t mp_type_EnvironmentError; +extern const mp_obj_type_t mp_type_Exception; +extern const mp_obj_type_t mp_type_FloatingPointError; +extern const mp_obj_type_t mp_type_FutureWarning; +extern const mp_obj_type_t mp_type_GeneratorExit; +extern const mp_obj_type_t mp_type_IOError; +extern const mp_obj_type_t mp_type_ImportError; +extern const mp_obj_type_t mp_type_ImportWarning; +extern const mp_obj_type_t mp_type_IndentationError; +extern const mp_obj_type_t mp_type_IndexError; +extern const mp_obj_type_t mp_type_KeyError; +extern const mp_obj_type_t mp_type_LookupError; +extern const mp_obj_type_t mp_type_MemoryError; +extern const mp_obj_type_t mp_type_NameError; +extern const mp_obj_type_t mp_type_NotImplementedError; +extern const mp_obj_type_t mp_type_OSError; +extern const mp_obj_type_t mp_type_OverflowError; +extern const mp_obj_type_t mp_type_PendingDeprecationWarning; +extern const mp_obj_type_t mp_type_ReferenceError; +extern const mp_obj_type_t mp_type_ResourceWarning; +extern const mp_obj_type_t mp_type_RuntimeError; +extern const mp_obj_type_t mp_type_RuntimeWarning; +extern const mp_obj_type_t mp_type_SyntaxError; +extern const mp_obj_type_t mp_type_SyntaxWarning; +extern const mp_obj_type_t mp_type_SystemError; +extern const mp_obj_type_t mp_type_SystemExit; +extern const mp_obj_type_t mp_type_TabError; +extern const mp_obj_type_t mp_type_TypeError; +extern const mp_obj_type_t mp_type_UnboundLocalError; +extern const mp_obj_type_t mp_type_UserWarning; +extern const mp_obj_type_t mp_type_ValueError; +extern const mp_obj_type_t mp_type_Warning; +extern const mp_obj_type_t mp_type_ZeroDivisionError; + +extern const mp_obj_type_t mp_type_StopIteration; +/*extern const mp_obj_type_t mp_type_BaseException; extern const mp_obj_type_t mp_type_AssertionError; extern const mp_obj_type_t mp_type_AttributeError; extern const mp_obj_type_t mp_type_ImportError; @@ -201,7 +245,7 @@ extern const mp_obj_type_t mp_type_OverflowError; extern const mp_obj_type_t mp_type_OSError; extern const mp_obj_type_t mp_type_NotImplementedError; extern const mp_obj_type_t mp_type_StopIteration; -extern const mp_obj_type_t mp_type_ZeroDivisionError; +extern const mp_obj_type_t mp_type_ZeroDivisionError;*/ // Constant objects, globally accessible diff --git a/py/objexcept.c b/py/objexcept.c index 3f636ffc6..7fd783b3a 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -77,23 +77,78 @@ const mp_obj_type_t mp_type_ ## exc_name = { \ .bases_tuple = (mp_obj_t)&mp_type_ ## base_name ## _base_tuple, \ }; +// List of all exceptions, arranged as in the table at: +// http://docs.python.org/3.3/library/exceptions.html MP_DEFINE_EXCEPTION_BASE(BaseException) - -MP_DEFINE_EXCEPTION(AssertionError, BaseException) -MP_DEFINE_EXCEPTION(AttributeError, BaseException) -MP_DEFINE_EXCEPTION(ImportError, BaseException) -MP_DEFINE_EXCEPTION(IndentationError, BaseException) -MP_DEFINE_EXCEPTION(IndexError, BaseException) -MP_DEFINE_EXCEPTION(KeyError, BaseException) -MP_DEFINE_EXCEPTION(NameError, BaseException) -MP_DEFINE_EXCEPTION(SyntaxError, BaseException) -MP_DEFINE_EXCEPTION(TypeError, BaseException) -MP_DEFINE_EXCEPTION(ValueError, BaseException) -MP_DEFINE_EXCEPTION(OverflowError, BaseException) -MP_DEFINE_EXCEPTION(OSError, BaseException) -MP_DEFINE_EXCEPTION(NotImplementedError, BaseException) -MP_DEFINE_EXCEPTION(StopIteration, BaseException) -MP_DEFINE_EXCEPTION(ZeroDivisionError, BaseException) +MP_DEFINE_EXCEPTION(SystemExit, BaseException) +//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException) +MP_DEFINE_EXCEPTION(GeneratorExit, BaseException) +MP_DEFINE_EXCEPTION(Exception, BaseException) + MP_DEFINE_EXCEPTION_BASE(Exception) + MP_DEFINE_EXCEPTION(StopIteration, Exception) + MP_DEFINE_EXCEPTION(ArithmeticError, Exception) + MP_DEFINE_EXCEPTION_BASE(ArithmeticError) + MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError) + MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError) + MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError) + MP_DEFINE_EXCEPTION(AssertionError, Exception) + MP_DEFINE_EXCEPTION(AttributeError, Exception) + MP_DEFINE_EXCEPTION(BufferError, Exception) + MP_DEFINE_EXCEPTION(EnvironmentError, Exception) + MP_DEFINE_EXCEPTION(EOFError, Exception) + MP_DEFINE_EXCEPTION(ImportError, Exception) + MP_DEFINE_EXCEPTION(IOError, Exception) + MP_DEFINE_EXCEPTION(LookupError, Exception) + MP_DEFINE_EXCEPTION_BASE(LookupError) + MP_DEFINE_EXCEPTION(IndexError, LookupError) + MP_DEFINE_EXCEPTION(KeyError, LookupError) + MP_DEFINE_EXCEPTION(MemoryError, Exception) + MP_DEFINE_EXCEPTION(NameError, Exception) + MP_DEFINE_EXCEPTION_BASE(NameError) + MP_DEFINE_EXCEPTION(UnboundLocalError, NameError) + MP_DEFINE_EXCEPTION(OSError, Exception) + // Probably don't need these + /*MP_DEFINE_EXCEPTION_BASE(OSError) + MP_DEFINE_EXCEPTION(BlockingIOError, OSError) + MP_DEFINE_EXCEPTION(ChildProcessError, OSError) + MP_DEFINE_EXCEPTION(ConnectionError, OSError) + MP_DEFINE_EXCEPTION(BrokenPipeError, ConnectionError) + MP_DEFINE_EXCEPTION(ConnectionAbortedError, ConnectionError) + MP_DEFINE_EXCEPTION(ConnectionRefusedError, ConnectionError) + MP_DEFINE_EXCEPTION(ConnectionResetError, ConnectionError) + MP_DEFINE_EXCEPTION(FileExistsError, OSError) + MP_DEFINE_EXCEPTION(FileNotFoundError, OSError) + MP_DEFINE_EXCEPTION(InterruptedError, OSError) + MP_DEFINE_EXCEPTION(IsADirectoryError, OSError) + MP_DEFINE_EXCEPTION(NotADirectoryError, OSError) + MP_DEFINE_EXCEPTION(PermissionError, OSError) + MP_DEFINE_EXCEPTION(ProcessLookupError, OSError) + MP_DEFINE_EXCEPTION(TimeoutError, OSError)*/ + MP_DEFINE_EXCEPTION(ReferenceError, Exception) + MP_DEFINE_EXCEPTION(RuntimeError, Exception) + MP_DEFINE_EXCEPTION_BASE(RuntimeError) + MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError) + MP_DEFINE_EXCEPTION(SyntaxError, Exception) + MP_DEFINE_EXCEPTION_BASE(SyntaxError) + MP_DEFINE_EXCEPTION(IndentationError, SyntaxError) + MP_DEFINE_EXCEPTION_BASE(IndentationError) + MP_DEFINE_EXCEPTION(TabError, IndentationError) + MP_DEFINE_EXCEPTION(SystemError, Exception) + MP_DEFINE_EXCEPTION(TypeError, Exception) + MP_DEFINE_EXCEPTION(ValueError, Exception) + //TODO: Implement UnicodeErrors which take arguments + MP_DEFINE_EXCEPTION(Warning, Exception) + MP_DEFINE_EXCEPTION_BASE(Warning) + MP_DEFINE_EXCEPTION(DeprecationWarning, Warning) + MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning) + MP_DEFINE_EXCEPTION(RuntimeWarning, Warning) + MP_DEFINE_EXCEPTION(SyntaxWarning, Warning) + MP_DEFINE_EXCEPTION(UserWarning, Warning) + MP_DEFINE_EXCEPTION(FutureWarning, Warning) + MP_DEFINE_EXCEPTION(ImportWarning, Warning) + MP_DEFINE_EXCEPTION(UnicodeWarning, Warning) + MP_DEFINE_EXCEPTION(BytesWarning, Warning) + MP_DEFINE_EXCEPTION(ResourceWarning, Warning) mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) { return mp_obj_new_exception_msg_varg(exc_type, NULL); diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 0e0479e9b..96d94d937 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -34,21 +34,64 @@ Q(Ellipsis) Q(StopIteration) Q(BaseException) +Q(ArithmeticError) Q(AssertionError) Q(AttributeError) +Q(BlockingIOError) +Q(BrokenPipeError) +Q(BufferError) +Q(BytesWarning) +Q(ChildProcessError) +Q(ConnectionAbortedError) +Q(ConnectionError) +Q(ConnectionRefusedError) +Q(ConnectionResetError) +Q(DeprecationWarning) +Q(EOFError) +Q(EnvironmentError) +Q(Exception) +Q(FileExistsError) +Q(FileNotFoundError) +Q(FloatingPointError) +Q(FutureWarning) +Q(GeneratorExit) +Q(IOError) Q(ImportError) +Q(ImportWarning) Q(IndentationError) Q(IndexError) +Q(InterruptedError) +Q(IsADirectoryError) Q(KeyError) +Q(LookupError) +Q(MemoryError) Q(NameError) +Q(NotADirectoryError) Q(NotImplementedError) Q(OSError) -Q(SyntaxError) -Q(TypeError) -Q(ValueError) Q(OverflowError) +Q(PendingDeprecationWarning) +Q(PermissionError) +Q(ProcessLookupError) +Q(ReferenceError) +Q(ResourceWarning) +Q(RuntimeError) +Q(RuntimeWarning) +Q(SyntaxError) +Q(SyntaxWarning) +Q(SystemError) +Q(SystemExit) +Q(TabError) +Q(TimeoutError) +Q(TypeError) +Q(UnboundLocalError) +Q(UnicodeWarning) +Q(UserWarning) +Q(ValueError) +Q(Warning) Q(ZeroDivisionError) + Q(NoneType) Q(abs) diff --git a/py/runtime.c b/py/runtime.c index 68edf5456..1cc1c2e6b 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -142,23 +142,49 @@ STATIC const mp_builtin_elem_t builtin_table[] = { // built-in exceptions { MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException }, + { MP_QSTR_ArithmeticError, (mp_obj_t)&mp_type_ArithmeticError }, { MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError }, { MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError }, + { MP_QSTR_BufferError, (mp_obj_t)&mp_type_BufferError }, + { MP_QSTR_BytesWarning, (mp_obj_t)&mp_type_BytesWarning }, + { MP_QSTR_DeprecationWarning, (mp_obj_t)&mp_type_DeprecationWarning }, + { MP_QSTR_EOFError, (mp_obj_t)&mp_type_EOFError }, + { MP_QSTR_EnvironmentError, (mp_obj_t)&mp_type_EnvironmentError }, + { MP_QSTR_Exception, (mp_obj_t)&mp_type_Exception }, + { MP_QSTR_FloatingPointError, (mp_obj_t)&mp_type_FloatingPointError }, + { MP_QSTR_FutureWarning, (mp_obj_t)&mp_type_FutureWarning }, + { MP_QSTR_GeneratorExit, (mp_obj_t)&mp_type_GeneratorExit }, + { MP_QSTR_IOError, (mp_obj_t)&mp_type_IOError }, { MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError }, + { MP_QSTR_ImportWarning, (mp_obj_t)&mp_type_ImportWarning }, { MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError }, { MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError }, { MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError }, + { MP_QSTR_LookupError, (mp_obj_t)&mp_type_LookupError }, + { MP_QSTR_MemoryError, (mp_obj_t)&mp_type_MemoryError }, { MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError }, + { MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError }, + { MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError }, + { MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError }, + { MP_QSTR_PendingDeprecationWarning, (mp_obj_t)&mp_type_PendingDeprecationWarning }, + { MP_QSTR_ReferenceError, (mp_obj_t)&mp_type_ReferenceError }, + { MP_QSTR_ResourceWarning, (mp_obj_t)&mp_type_ResourceWarning }, + { MP_QSTR_RuntimeError, (mp_obj_t)&mp_type_RuntimeError }, + { MP_QSTR_RuntimeWarning, (mp_obj_t)&mp_type_RuntimeWarning }, { MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError }, + { MP_QSTR_SyntaxWarning, (mp_obj_t)&mp_type_SyntaxWarning }, + { MP_QSTR_SystemError, (mp_obj_t)&mp_type_SystemError }, + { MP_QSTR_SystemExit, (mp_obj_t)&mp_type_SystemExit }, + { MP_QSTR_TabError, (mp_obj_t)&mp_type_TabError }, { MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError }, + { MP_QSTR_UnboundLocalError, (mp_obj_t)&mp_type_UnboundLocalError }, + { MP_QSTR_UserWarning, (mp_obj_t)&mp_type_UserWarning }, { MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError }, + { MP_QSTR_Warning, (mp_obj_t)&mp_type_Warning }, + { MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError }, + { MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration }, // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/ // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation - { MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError }, - { MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError }, - { MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError }, - { MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration }, - { MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError }, // Extra builtins as defined by a port MICROPY_EXTRA_BUILTINS diff --git a/tests/basics/exceptpoly.py b/tests/basics/exceptpoly.py new file mode 100644 index 000000000..0e5ac2d1a --- /dev/null +++ b/tests/basics/exceptpoly.py @@ -0,0 +1,380 @@ +try: + raise ArithmeticError +except Exception: + print("Caught ArithmeticError via Exception") + +try: + raise ArithmeticError +except ArithmeticError: + print("Caught ArithmeticError") + +try: + raise AssertionError +except Exception: + print("Caught AssertionError via Exception") + +try: + raise AssertionError +except AssertionError: + print("Caught AssertionError") + +try: + raise AttributeError +except Exception: + print("Caught AttributeError via Exception") + +try: + raise AttributeError +except AttributeError: + print("Caught AttributeError") + +try: + raise BufferError +except Exception: + print("Caught BufferError via Exception") + +try: + raise BufferError +except BufferError: + print("Caught BufferError") + +try: + raise BytesWarning +except Warning: + print("Caught BytesWarning via Warning") + +try: + raise BytesWarning +except BytesWarning: + print("Caught BytesWarning") + +try: + raise DeprecationWarning +except Warning: + print("Caught DeprecationWarning via Warning") + +try: + raise DeprecationWarning +except DeprecationWarning: + print("Caught DeprecationWarning") + +try: + raise EOFError +except Exception: + print("Caught EOFError via Exception") + +try: + raise EOFError +except EOFError: + print("Caught EOFError") + +try: + raise EnvironmentError +except Exception: + print("Caught EnvironmentError via Exception") + +try: + raise EnvironmentError +except EnvironmentError: + print("Caught EnvironmentError") + +try: + raise Exception +except BaseException: + print("Caught Exception via BaseException") + +try: + raise Exception +except Exception: + print("Caught Exception") + +try: + raise FloatingPointError +except ArithmeticError: + print("Caught FloatingPointError via ArithmeticError") + +try: + raise FloatingPointError +except FloatingPointError: + print("Caught FloatingPointError") + +try: + raise FutureWarning +except Warning: + print("Caught FutureWarning via Warning") + +try: + raise FutureWarning +except FutureWarning: + print("Caught FutureWarning") + +try: + raise IOError +except Exception: + print("Caught IOError via Exception") + +try: + raise IOError +except IOError: + print("Caught IOError") + +try: + raise ImportError +except Exception: + print("Caught ImportError via Exception") + +try: + raise ImportError +except ImportError: + print("Caught ImportError") + +try: + raise ImportWarning +except Warning: + print("Caught ImportWarning via Warning") + +try: + raise ImportWarning +except ImportWarning: + print("Caught ImportWarning") + +try: + raise IndentationError +except SyntaxError: + print("Caught IndentationError via SyntaxError") + +try: + raise IndentationError +except IndentationError: + print("Caught IndentationError") + +try: + raise IndexError +except LookupError: + print("Caught IndexError via LookupError") + +try: + raise IndexError +except IndexError: + print("Caught IndexError") + +try: + raise KeyError +except LookupError: + print("Caught KeyError via LookupError") + +try: + raise KeyError +except KeyError: + print("Caught KeyError") + +try: + raise LookupError +except Exception: + print("Caught LookupError via Exception") + +try: + raise LookupError +except LookupError: + print("Caught LookupError") + +try: + raise MemoryError +except Exception: + print("Caught MemoryError via Exception") + +try: + raise MemoryError +except MemoryError: + print("Caught MemoryError") + +try: + raise NameError +except Exception: + print("Caught NameError via Exception") + +try: + raise NameError +except NameError: + print("Caught NameError") + +try: + raise NotImplementedError +except RuntimeError: + print("Caught NotImplementedError via RuntimeError") + +try: + raise NotImplementedError +except NotImplementedError: + print("Caught NotImplementedError") + +try: + raise OSError +except Exception: + print("Caught OSError via Exception") + +try: + raise OSError +except OSError: + print("Caught OSError") + +try: + raise OverflowError +except ArithmeticError: + print("Caught OverflowError via ArithmeticError") + +try: + raise OverflowError +except OverflowError: + print("Caught OverflowError") + +try: + raise PendingDeprecationWarning +except Warning: + print("Caught PendingDeprecationWarning via Warning") + +try: + raise PendingDeprecationWarning +except PendingDeprecationWarning: + print("Caught PendingDeprecationWarning") + +try: + raise ReferenceError +except Exception: + print("Caught ReferenceError via Exception") + +try: + raise ReferenceError +except ReferenceError: + print("Caught ReferenceError") + +try: + raise ResourceWarning +except Warning: + print("Caught ResourceWarning via Warning") + +try: + raise ResourceWarning +except ResourceWarning: + print("Caught ResourceWarning") + +try: + raise RuntimeError +except Exception: + print("Caught RuntimeError via Exception") + +try: + raise RuntimeError +except RuntimeError: + print("Caught RuntimeError") + +try: + raise RuntimeWarning +except Warning: + print("Caught RuntimeWarning via Warning") + +try: + raise RuntimeWarning +except RuntimeWarning: + print("Caught RuntimeWarning") + +try: + raise SyntaxError +except Exception: + print("Caught SyntaxError via Exception") + +try: + raise SyntaxError +except SyntaxError: + print("Caught SyntaxError") + +try: + raise SyntaxWarning +except Warning: + print("Caught SyntaxWarning via Warning") + +try: + raise SyntaxWarning +except SyntaxWarning: + print("Caught SyntaxWarning") + +try: + raise SystemError +except Exception: + print("Caught SystemError via Exception") + +try: + raise SystemError +except SystemError: + print("Caught SystemError") + +try: + raise TabError +except IndentationError: + print("Caught TabError via IndentationError") + +try: + raise TabError +except TabError: + print("Caught TabError") + +try: + raise TypeError +except Exception: + print("Caught TypeError via Exception") + +try: + raise TypeError +except TypeError: + print("Caught TypeError") + +try: + raise UnboundLocalError +except NameError: + print("Caught UnboundLocalError via NameError") + +try: + raise UnboundLocalError +except UnboundLocalError: + print("Caught UnboundLocalError") + +try: + raise UserWarning +except Warning: + print("Caught UserWarning via Warning") + +try: + raise UserWarning +except UserWarning: + print("Caught UserWarning") + +try: + raise ValueError +except Exception: + print("Caught ValueError via Exception") + +try: + raise ValueError +except ValueError: + print("Caught ValueError") + +try: + raise Warning +except Exception: + print("Caught Warning via Exception") + +try: + raise Warning +except Warning: + print("Caught Warning") + +try: + raise ZeroDivisionError +except ArithmeticError: + print("Caught ZeroDivisionError via ArithmeticError") + +try: + raise ZeroDivisionError +except ZeroDivisionError: + print("Caught ZeroDivisionError") +