Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions binding/emit_c_cpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,29 @@ def emit_c(ctx):
.sq_ass_item = lvpy_struct_array_ass_item,
};

/* dir() on a struct instance: what object.__dir__ finds, plus the fields and
* methods the struct's getattro resolves by name (lvgl-bindings#40). */
static inline PyObject *lvpy_struct_dir(PyObject *self, const char *const *fields,
PyMethodDef *methods)
{
PyObject *names = PyObject_CallMethod((PyObject *)&PyBaseObject_Type, "__dir__", "O", self);
if (names == NULL) return NULL;
PyObject *list = PySequence_List(names);
Py_DECREF(names);
if (list == NULL) return NULL;
for (; *fields != NULL; fields++) {
PyObject *s = PyUnicode_FromString(*fields);
if (s == NULL || PyList_Append(list, s) < 0) { Py_XDECREF(s); Py_DECREF(list); return NULL; }
Py_DECREF(s);
}
for (; methods->ml_name != NULL; methods++) {
PyObject *s = PyUnicode_FromString(methods->ml_name);
if (s == NULL || PyList_Append(list, s) < 0) { Py_XDECREF(s); Py_DECREF(list); return NULL; }
Py_DECREF(s);
}
return list;
}

static PyTypeObject lvpy_struct_array_type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "lvgl.StructArray",
Expand Down
21 changes: 21 additions & 0 deletions binding/emit_cpython_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function

import re
import collections
import copy
from contextvars import ContextVar
Expand Down Expand Up @@ -475,6 +476,19 @@ def try_generate_struct_cpython(struct_name, struct):
return PyObject_GenericGetAttr((PyObject *)self, name);
}}

static const char *const py_{san}_field_names[] = {{{field_names}NULL}};

static PyObject *py_{san}_dir(PyObject *self, PyObject *unused)
{{
(void)unused;
return lvpy_struct_dir(self, py_{san}_field_names, py_{san}_methods);
}}

static PyMethodDef py_{san}_type_methods[] = {{
{{"__dir__", py_{san}_dir, METH_NOARGS, NULL}},
{{NULL}}
}};

static int py_{san}_setattro(PyObject *self, PyObject *name, PyObject *value)
{{
if (value == NULL) {{
Expand Down Expand Up @@ -520,6 +534,7 @@ def try_generate_struct_cpython(struct_name, struct):
.tp_getattro = (getter)py_{san}_getattro,
.tp_setattro = (setter)py_{san}_setattro,
.tp_new = py_{san}_new,
.tp_methods = py_{san}_type_methods,
.tp_base = &py_lv_base_struct_type,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
}};
Expand Down Expand Up @@ -549,6 +564,12 @@ def try_generate_struct_cpython(struct_name, struct):
san=sanitized_struct_name,
struct_tag=struct_tag,
read_cases="\n ".join(read_cases) if read_cases else "(void)attr;",
field_names="".join(
'"%s", ' % name
for name in dict.fromkeys(
re.findall(r'strcmp\(attr, "([^"]+)"\)', "".join(read_cases))
)
),
write_cases="\n ".join(write_cases) if write_cases else "(void)value;",
elem_size=elem_size_expr,
# A conversion that fails has already stored its placeholder, so
Expand Down
Loading
Loading