Skip to content

Commit 2fe841d

Browse files
committed
Try not to cause a MemoryError when raising an exception during nterrupt handling.
Step 1 fixes adafruit#732
1 parent 8993fb6 commit 2fe841d

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

py/gc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ void gc_unlock(void) {
172172
gc_lock_depth--;
173173
}
174174

175+
bool gc_is_locked(void) {
176+
return gc_lock_depth != 0;
177+
}
178+
175179
#define VERIFY_PTR(ptr) ( \
176180
(ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
177181
&& ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \

py/gc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void gc_init(void *start, void *end);
3030
// They can be used to prevent the GC from allocating/freeing.
3131
void gc_lock(void);
3232
void gc_unlock(void);
33+
bool gc_is_locked(void);
3334

3435
// A given port must implement gc_collect by using the other collect functions.
3536
void gc_collect(void);

py/objexcept.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "objtype.h"
3838
#include "runtime.h"
3939
#include "runtime0.h"
40+
#include "gc.h"
4041

4142
typedef struct _mp_obj_exception_t {
4243
mp_obj_base_t base;
@@ -335,6 +336,10 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
335336
}
336337

337338
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
339+
if (gc_is_locked()) {
340+
// We can't allocate memory, so don't bother to try
341+
return;
342+
}
338343
GET_NATIVE_EXCEPTION(self, self_in);
339344

340345
// for traceback, we are just using the list object for convenience, it's not really a list of Python objects

0 commit comments

Comments
 (0)