lvgl / lv_binding_micropython

LVGL binding for MicroPython
MIT License
237 stars 156 forks source link

[NameError: name 'items' isn't defined] in c modules #241

Open TsXor opened 1 year ago

TsXor commented 1 year ago

error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/myfont/myfont.py", line 61, in readchr
  File "/lib/myfont/myfont.py", line 31, in mapaddr
NameError: name 'items' isn't defined

that line:

coffset = bisect_left(zhmapper, cord)

bisect.c

#include "py/dynruntime.h"

STATIC mp_int_t bisect_left_impl(mp_obj_t a, mp_int_t x) {
    mp_int_t lo = 0; mp_int_t hi = mp_obj_get_int(mp_obj_len(a));
    mp_int_t mid; mp_obj_t curptr; 
    while (lo < hi) {
        mid = (lo + hi) >> 1; // 右移一位等价于地板除2
        curptr = mp_obj_subscr(a, mp_obj_new_int(mid), MP_OBJ_SENTINEL);
        if (mp_obj_get_int(curptr) < x) {
            lo = mid + 1;
        } else {
            hi = mid;
        }
    }
    return lo;
}

STATIC mp_obj_t bisect_left(mp_obj_t a_obj, mp_obj_t x_obj) {
    mp_obj_t a = a_obj;
    mp_int_t x = mp_obj_get_int(x_obj);

    mp_int_t result = bisect_left_impl(a, x);

    return mp_obj_new_int(result);
}

STATIC MP_DEFINE_CONST_FUN_OBJ_2(bisect_left_obj, bisect_left);

mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
    MP_DYNRUNTIME_INIT_ENTRY

    mp_store_global(MP_QSTR_bisect_left, MP_OBJ_FROM_PTR(&bisect_left_obj));

    MP_DYNRUNTIME_INIT_EXIT
}
TsXor commented 1 year ago

Just compiled like regular mpy, don't know how it is different in lvgl. This module runs as expected in build without lvgl.

amirgon commented 1 year ago

Hi @TsXor !

It's a bit hard for me to say what is the problem by reading your code.
I don't know what you are trying to do, or how you configured / changed / built the project etc. If I could reproduce your problem on my side maybe I could help.

Please provide:

TsXor commented 1 year ago

Hi @TsXor !

It's a bit hard for me to say what is the problem by reading your code. I don't know what you are trying to do, or how you configured / changed / built the project etc. If I could reproduce your problem on my side maybe I could help.

Please provide:

* [**Minimal, Reproducible Example**](https://stackoverflow.com/help/minimal-reproducible-example) for your problem. Please try to remove any line of code that is irrelevant to the problem.

* All the details for how you configure and build the project

* Step by step all the commands you run until your see this problem

The code I posted is almost reproducible. You just have to compile it as a dynamic module with a proper Makefile like any of the ones in examples/natmod/

Here is the full thing. Just clone this folder and run make inside it (you may need to compile mpy-cross first)

After building it, you just put it in the /lib folder and import it like:

from bisect import bisect_left
TsXor commented 1 year ago

I can almost say that it is mp_obj_subscr that go wrong.