MicroPython 是一种适用于微控制器和其他受限环境的 Python 编程语言的实现。它旨在提供与 Python 3 语言的紧密兼容,同时考虑到内存和计算资源的限制。MicroPython 库是这门语言的核心组成部分,提供了一系列的模块和函数,使得开发者能够在硬件上执行各种任务。
下面将通过系列文章,逐一解读microPython,以便让读者了解掌握microPython的整个核心逻辑.,以便读者可以透过这个Python的最小内核,掌握Python解析器的核心实现逻辑,学习世界上最优秀的源码设计之一.
#include <string.h>
#include "py/obj.h"
#include "py/runtime.h"
// 定义一个结构体,用于表示绑定方法
typedef struct _mp_obj_bound_meth_t {
mp_obj_base_t base; // 继承自基础对象
mp_obj_t meth; // 指向方法对象的指针
mp_obj_t self; // 指向绑定对象的指针
} mp_obj_bound_meth_t;
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
// 打印绑定方法对象的函数
static void bound_meth_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_bound_meth_t *o = MP_OBJ_TO_PTR(o_in); // 将对象转换为结构体指针
mp_printf(print, "<bound_method %p ", o); // 打印绑定方法的地址
mp_obj_print_helper(print, o->self, PRINT_REPR); // 打印绑定对象
mp_print_str(print, ".");
mp_obj_print_helper(print, o->meth, PRINT_REPR); // 打印方法对象
mp_print_str(print, ">");
}
#endif
// 调用绑定方法的函数
mp_obj_t mp_call_method_self_n_kw(mp_obj_t meth, mp_obj_t self, size_t n_args, size_t n_kw, const mp_obj_t *args) {
// 将self插入到参数列表的开始,然后调用方法
size_t n_total = n_args + 2 * n_kw; // 计算总参数数量
mp_obj_t *args2 = NULL; // 临时参数数组
#if MICROPY_ENABLE_PYSTACK
// 使用pystack分配内存
args2 = mp_pystack_alloc(sizeof(mp_obj_t) * (1 + n_total));
#else
mp_obj_t *free_args2 = NULL;
if (n_total > 4) {
// 尝试使用堆分配临时参数数组
args2 = m_new_maybe(mp_obj_t, 1 + n_total);
free_args2 = args2;
}
if (args2 == NULL) {
// 使用栈分配临时参数数组
args2 = alloca(sizeof(mp_obj_t) * (1 + n_total));
}
#endif
args2[0] = self; // 将self作为第一个参数
memcpy(args2 + 1, args, n_total * sizeof(mp_obj_t)); // 复制其他参数
mp_obj_t res = mp_call_function_n_kw(meth, n_args + 1, n_kw, args2); // 调用方法
#if MICROPY_ENABLE_PYSTACK
// 释放pystack分配的内存
mp_pystack_free(args2);
#else
if (free_args2 != NULL) {
// 释放堆分配的内存
m_del(mp_obj_t, free_args2, 1 + n_total);
}
#endif
return res; // 返回调用结果
}
// 绑定方法的调用函数
static mp_obj_t bound_meth_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_obj_bound_meth_t *self = MP_OBJ_TO_PTR(self_in); // 将对象转换为结构体指针
return mp_call_method_self_n_kw(