Skip to content

Commit 5fc5804

Browse files
committed
Merge branch 'dhylands-preserve-except'
2 parents aa47f39 + f0b2972 commit 5fc5804

3 files changed

Lines changed: 13 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: 8 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,13 @@ 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 MICROPY_ENABLE_GC
340+
if (gc_is_locked()) {
341+
// We can't allocate memory, so don't bother to try
342+
return;
343+
}
344+
#endif
345+
338346
GET_NATIVE_EXCEPTION(self, self_in);
339347

340348
// 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)