From 077812b2abe3f5e5325194f9694dad7eb38186dd Mon Sep 17 00:00:00 2001 From: Daniel Campora Date: Mon, 29 Jun 2015 22:45:39 +0200 Subject: [PATCH] py: Add TimeoutError exception subclassed from OSError. The TimeoutError is useful for some modules, specially the the socket module. TimeoutError can then be alised to socket.timeout and then Python code can differentiate between socket.error and socket.timeout. --- py/mpconfig.h | 5 +++++ py/obj.h | 1 + py/objexcept.c | 6 ++++-- py/qstrdefs.h | 3 +++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index 9f541ef70..e92e4c45d 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -462,6 +462,11 @@ typedef double mp_float_t; #define MICROPY_PY_BUILTINS_RANGE_ATTRS (1) #endif +// Whether to support timeout exceptions (like socket.timeout) +#ifndef MICROPY_PY_BUILTINS_TIMEOUTERROR +#define MICROPY_PY_BUILTINS_TIMEOUTERROR (0) +#endif + // Whether to support complete set of special methods // for user classes, otherwise only the most used #ifndef MICROPY_PY_ALL_SPECIAL_METHODS diff --git a/py/obj.h b/py/obj.h index 7275886c7..10a678e83 100644 --- a/py/obj.h +++ b/py/obj.h @@ -426,6 +426,7 @@ 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_TimeoutError; extern const mp_obj_type_t mp_type_OverflowError; extern const mp_obj_type_t mp_type_RuntimeError; extern const mp_obj_type_t mp_type_StopIteration; diff --git a/py/objexcept.c b/py/objexcept.c index 6c108d99c..75369bac7 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -221,8 +221,11 @@ MP_DEFINE_EXCEPTION(Exception, BaseException) MP_DEFINE_EXCEPTION(UnboundLocalError, NameError) */ MP_DEFINE_EXCEPTION(OSError, Exception) - /* +#if MICROPY_PY_BUILTINS_TIMEOUTERROR MP_DEFINE_EXCEPTION_BASE(OSError) + MP_DEFINE_EXCEPTION(TimeoutError, OSError) +#endif + /* MP_DEFINE_EXCEPTION(BlockingIOError, OSError) MP_DEFINE_EXCEPTION(ChildProcessError, OSError) MP_DEFINE_EXCEPTION(ConnectionError, OSError) @@ -235,7 +238,6 @@ MP_DEFINE_EXCEPTION(Exception, BaseException) MP_DEFINE_EXCEPTION(NotADirectoryError, OSError) MP_DEFINE_EXCEPTION(PermissionError, OSError) MP_DEFINE_EXCEPTION(ProcessLookupError, OSError) - MP_DEFINE_EXCEPTION(TimeoutError, OSError) MP_DEFINE_EXCEPTION(FileExistsError, OSError) MP_DEFINE_EXCEPTION(FileNotFoundError, OSError) MP_DEFINE_EXCEPTION(ReferenceError, Exception) diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 2f51d470d..367aea17b 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -140,6 +140,9 @@ Q(MemoryError) Q(NameError) Q(NotImplementedError) Q(OSError) +#if MICROPY_PY_BUILTINS_TIMEOUTERROR +Q(TimeoutError) +#endif Q(OverflowError) Q(RuntimeError) Q(SyntaxError)