Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jepler/596da0c2c488f51d19d547c6cc8116aa to your computer and use it in GitHub Desktop.
Save jepler/596da0c2c488f51d19d547c6cc8116aa to your computer and use it in GitHub Desktop.
From 6e720a37e7b7a2f0523eede0d17c886061624c57 Mon Sep 17 00:00:00 2001
From: Jeff Epler <[email protected]>
Date: Sun, 24 Sep 2023 08:42:05 -0500
Subject: [PATCH 1/2] Add support for enums where the 0-enumerant is translated
to None
---
py/enum.h | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/py/enum.h b/py/enum.h
index b83257e00f..671f494a7c 100644
--- a/py/enum.h
+++ b/py/enum.h
@@ -64,3 +64,20 @@ typedef struct {
mp_obj_t cp_enum_find(const mp_obj_type_t *type, int value);
int cp_enum_value(const mp_obj_type_t *type, mp_obj_t obj, qstr arg_name);
void cp_enum_obj_print_helper(uint16_t module, const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
+
+// The "0"-suffixed routines are for enumerated types where the 0-enumerant
+// should be translated to None in Python. In this case, there should be no enum
+// value or enum map entry for the 0-enumerant.
+static MP_INLINE mp_obj_t cp_enum_find0(const mp_obj_type_t *type, int value) {
+ if (value == 0) {
+ return mp_const_none;
+ }
+ return cp_enum_find(type, value);
+}
+
+static MP_INLINE int cp_enum_value0(const mp_obj_type_t *type, mp_obj_t obj, qstr arg_name) {
+ if (!mp_obj_is_true(obj)) {
+ return 0;
+ }
+ return cp_enum_value0(type, obj, arg_name);
+}
--
2.39.2
From 57a07f48e1d8e197cd116e8d714abb4801a3198c Mon Sep 17 00:00:00 2001
From: Jeff Epler <[email protected]>
Date: Sun, 24 Sep 2023 08:42:31 -0500
Subject: [PATCH 2/2] DO NOT MERGE show using None for SAFE_MODE_NONE
this is an incompatible change, so don't do it.
---
shared-bindings/supervisor/Runtime.c | 2 +-
shared-bindings/supervisor/SafeModeReason.c | 6 ------
2 files changed, 1 insertion(+), 7 deletions(-)
diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c
index 792f18637a..1a38091d4c 100644
--- a/shared-bindings/supervisor/Runtime.c
+++ b/shared-bindings/supervisor/Runtime.c
@@ -123,7 +123,7 @@ MP_PROPERTY_GETTER(supervisor_runtime_run_reason_obj,
//| """
STATIC mp_obj_t supervisor_runtime_get_safe_mode_reason(mp_obj_t self) {
#if CIRCUITPY_SAFEMODE_PY
- return cp_enum_find(&supervisor_safe_mode_reason_type, get_safe_mode());
+ return cp_enum_find0(&supervisor_safe_mode_reason_type, get_safe_mode());
#else
mp_raise_NotImplementedError(NULL);
#endif
diff --git a/shared-bindings/supervisor/SafeModeReason.c b/shared-bindings/supervisor/SafeModeReason.c
index 2da03bdced..1d4cfee00a 100644
--- a/shared-bindings/supervisor/SafeModeReason.c
+++ b/shared-bindings/supervisor/SafeModeReason.c
@@ -31,7 +31,6 @@
// Reuse the non-Python safe_mode_t enum
#include "supervisor/shared/safe_mode.h"
-MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, NONE, SAFE_MODE_NONE);
MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, BROWNOUT, SAFE_MODE_BROWNOUT);
// alphabetical from here down
MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, FLASH_WRITE_FAIL, SAFE_MODE_FLASH_WRITE_FAIL);
@@ -59,11 +58,6 @@ MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, WATCHDOG, SA
//|
MAKE_ENUM_MAP(supervisor_safe_mode_reason) {
-//| NONE: object
-//| """CircuitPython is not in safe mode."""
-//|
- MAKE_ENUM_MAP_ENTRY(safe_mode_reason, NONE),
-
//| BROWNOUT: object
//| """The microcontroller voltage dropped too low."""
//|
--
2.39.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment