Skip to content

Commit 077812b

Browse files
author
Daniel Campora
committed
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.
1 parent bdf958d commit 077812b

4 files changed

Lines changed: 13 additions & 2 deletions

File tree

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ typedef double mp_float_t;
462462
#define MICROPY_PY_BUILTINS_RANGE_ATTRS (1)
463463
#endif
464464

465+
// Whether to support timeout exceptions (like socket.timeout)
466+
#ifndef MICROPY_PY_BUILTINS_TIMEOUTERROR
467+
#define MICROPY_PY_BUILTINS_TIMEOUTERROR (0)
468+
#endif
469+
465470
// Whether to support complete set of special methods
466471
// for user classes, otherwise only the most used
467472
#ifndef MICROPY_PY_ALL_SPECIAL_METHODS

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ extern const mp_obj_type_t mp_type_MemoryError;
426426
extern const mp_obj_type_t mp_type_NameError;
427427
extern const mp_obj_type_t mp_type_NotImplementedError;
428428
extern const mp_obj_type_t mp_type_OSError;
429+
extern const mp_obj_type_t mp_type_TimeoutError;
429430
extern const mp_obj_type_t mp_type_OverflowError;
430431
extern const mp_obj_type_t mp_type_RuntimeError;
431432
extern const mp_obj_type_t mp_type_StopIteration;

py/objexcept.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
221221
MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
222222
*/
223223
MP_DEFINE_EXCEPTION(OSError, Exception)
224-
/*
224+
#if MICROPY_PY_BUILTINS_TIMEOUTERROR
225225
MP_DEFINE_EXCEPTION_BASE(OSError)
226+
MP_DEFINE_EXCEPTION(TimeoutError, OSError)
227+
#endif
228+
/*
226229
MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
227230
MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
228231
MP_DEFINE_EXCEPTION(ConnectionError, OSError)
@@ -235,7 +238,6 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
235238
MP_DEFINE_EXCEPTION(NotADirectoryError, OSError)
236239
MP_DEFINE_EXCEPTION(PermissionError, OSError)
237240
MP_DEFINE_EXCEPTION(ProcessLookupError, OSError)
238-
MP_DEFINE_EXCEPTION(TimeoutError, OSError)
239241
MP_DEFINE_EXCEPTION(FileExistsError, OSError)
240242
MP_DEFINE_EXCEPTION(FileNotFoundError, OSError)
241243
MP_DEFINE_EXCEPTION(ReferenceError, Exception)

py/qstrdefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ Q(MemoryError)
140140
Q(NameError)
141141
Q(NotImplementedError)
142142
Q(OSError)
143+
#if MICROPY_PY_BUILTINS_TIMEOUTERROR
144+
Q(TimeoutError)
145+
#endif
143146
Q(OverflowError)
144147
Q(RuntimeError)
145148
Q(SyntaxError)

0 commit comments

Comments
 (0)