From 6d103b65482d8dc1d688fcfd59b3400ebfbe0f0f Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Mon, 25 Apr 2016 19:28:12 +0300
Subject: [PATCH] py: Move call_function_*_protected() functions to py/ for
 reuse.

They almost certainly needed by any C code which calls Python callbacks.
---
 esp8266/Makefile                      |  1 -
 esp8266/modesp.c                      |  1 -
 esp8266/modmachine.c                  |  3 +--
 esp8266/modpybpin.c                   |  3 +--
 esp8266/utils.h                       | 29 ---------------------------
 py/py.mk                              |  1 +
 py/runtime.h                          |  3 +++
 esp8266/utils.c => py/runtime_utils.c |  4 ++--
 8 files changed, 8 insertions(+), 37 deletions(-)
 delete mode 100644 esp8266/utils.h
 rename esp8266/utils.c => py/runtime_utils.c (92%)

diff --git a/esp8266/Makefile b/esp8266/Makefile
index c49fdd2c2..6db8003ed 100644
--- a/esp8266/Makefile
+++ b/esp8266/Makefile
@@ -76,7 +76,6 @@ SRC_C = \
 	moduos.c \
 	modmachine.c \
 	modonewire.c \
-	utils.c \
 	ets_alt_task.c \
 	$(BUILD)/frozen.c \
 	fatfs_port.c \
diff --git a/esp8266/modesp.c b/esp8266/modesp.c
index fd54ae070..a6038ab39 100644
--- a/esp8266/modesp.c
+++ b/esp8266/modesp.c
@@ -40,7 +40,6 @@
 #include "user_interface.h"
 #include "espconn.h"
 #include "spi_flash.h"
-#include "utils.h"
 #include "espneopixel.h"
 #include "modpyb.h"
 
diff --git a/esp8266/modmachine.c b/esp8266/modmachine.c
index c75f6e528..c3c9494ac 100644
--- a/esp8266/modmachine.c
+++ b/esp8266/modmachine.c
@@ -31,7 +31,6 @@
 #include "py/runtime.h"
 #include "extmod/machine_mem.h"
 #include "extmod/machine_i2c.h"
-#include "utils.h"
 #include "modpyb.h"
 #include "modpybrtc.h"
 
@@ -137,7 +136,7 @@ STATIC mp_obj_t esp_timer_make_new(const mp_obj_type_t *type, mp_uint_t n_args,
 
 STATIC void esp_timer_cb(void *arg) {
     esp_timer_obj_t *self = arg;
-    call_function_1_protected(self->callback, self);
+    mp_call_function_1_protected(self->callback, self);
 }
 
 STATIC mp_obj_t esp_timer_init_helper(esp_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
diff --git a/esp8266/modpybpin.c b/esp8266/modpybpin.c
index a65911cbd..ca2b87d93 100644
--- a/esp8266/modpybpin.c
+++ b/esp8266/modpybpin.c
@@ -37,7 +37,6 @@
 #include "py/runtime.h"
 #include "py/gc.h"
 #include "modpyb.h"
-#include "utils.h"
 
 #define GET_TRIGGER(phys_port) \
     GPIO_PIN_INT_TYPE_GET(GPIO_REG_READ(GPIO_PIN_ADDR(phys_port)))
@@ -105,7 +104,7 @@ void pin_intr_handler(uint32_t status) {
         if (status & 1) {
             mp_obj_t handler = MP_STATE_PORT(pin_irq_handler)[p];
             if (handler != MP_OBJ_NULL) {
-                call_function_1_protected(handler, MP_OBJ_FROM_PTR(&pyb_pin_obj[p]));
+                mp_call_function_1_protected(handler, MP_OBJ_FROM_PTR(&pyb_pin_obj[p]));
             }
         }
     }
diff --git a/esp8266/utils.h b/esp8266/utils.h
deleted file mode 100644
index c6a4f1f3e..000000000
--- a/esp8266/utils.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * This file is part of the Micro Python project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 Josef Gajdusek
- * Copyright (c) 2015 Paul Sokolovsky
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-void call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
-void call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
diff --git a/py/py.mk b/py/py.mk
index c6360599f..74aaf4510 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -100,6 +100,7 @@ PY_O_BASENAME = \
 	parsenum.o \
 	emitglue.o \
 	runtime.o \
+	runtime_utils.o \
 	nativeglue.o \
 	stackctrl.o \
 	argcheck.o \
diff --git a/py/runtime.h b/py/runtime.h
index 1b58f4728..3e325a31b 100644
--- a/py/runtime.h
+++ b/py/runtime.h
@@ -95,6 +95,9 @@ mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
 mp_obj_t mp_call_function_n_kw(mp_obj_t fun, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
 mp_obj_t mp_call_method_n_kw(mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
 mp_obj_t mp_call_method_n_kw_var(bool have_self, mp_uint_t n_args_n_kw, const mp_obj_t *args);
+// Call function and catch/dump exception - for Python callbacks from C code
+void mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
+void mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
 
 typedef struct _mp_call_args_t {
     mp_obj_t fun;
diff --git a/esp8266/utils.c b/py/runtime_utils.c
similarity index 92%
rename from esp8266/utils.c
rename to py/runtime_utils.c
index b2bdcffbe..0b0aa4e50 100644
--- a/esp8266/utils.c
+++ b/py/runtime_utils.c
@@ -29,7 +29,7 @@
 #include "py/obj.h"
 #include "py/nlr.h"
 
-void call_function_1_protected(mp_obj_t fun, mp_obj_t arg) {
+void mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg) {
     nlr_buf_t nlr;
     if (nlr_push(&nlr) == 0) {
         mp_call_function_1(fun, arg);
@@ -39,7 +39,7 @@ void call_function_1_protected(mp_obj_t fun, mp_obj_t arg) {
     }
 }
 
-void call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
+void mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
     nlr_buf_t nlr;
     if (nlr_push(&nlr) == 0) {
         mp_call_function_2(fun, arg1, arg2);
-- 
GitLab