Skip to content

Bugfix/list dict etc #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
43 changes: 39 additions & 4 deletions src/ustubby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,57 @@ def string_handle(*args, **kwargs):
float: "\tmp_float_t ret_val;",
bool: "\tbool ret_val;",
str: "",
bytes: "",
tuple: "",
list: "",
dict: "",
object: "",
# tuple: string_template(
# "\tmp_obj_t *{0} = NULL;\n\tsize_t {0}_len = 0;\n\tmp_obj_get_array({0}_arg, &{0}_len, &{0});"),
# list: string_template(
# "\tmp_obj_t *{0} = NULL;\n\tsize_t {0}_len = 0;\n\tmp_obj_get_array({0}_arg, &{0}_len, &{0});"),
# set: string_template(
# "\tmp_obj_t *{0} = NULL;\n\tsize_t {0}_len = 0;\n\tmp_obj_get_array({0}_arg, &{0}_len, &{0});"),
inspect._empty: "",
None: ""
}

return_handler = {
int: "\treturn mp_obj_new_int(ret_val);",
float: "\treturn mp_obj_new_float(ret_val);",
bool: "\treturn mp_obj_new_bool(ret_val);",
str: "\treturn mp_obj_new_str(<ret_val_ptr>, <ret_val_len>);",
tuple: '''
// signature: mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);

str: """\t// signature: mp_obj_t mp_obj_new_bytes(const char* data, size_t len);
return mp_obj_new_str("hello", 5);""",

tuple: '''\t// signature: mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
mp_obj_t ret_val[] = {
mp_obj_new_int(123),
mp_obj_new_float(456.789),
mp_obj_new_str("hello", 5),
};
return mp_obj_new_tuple(3, ret_val);''',

bytes:'''\t// signature: mp_obj_t mp_obj_new_bytes(const byte* data, size_t len);
return mp_obj_new_bytes("hello", 5);''',

list: '''\t// signature: mp_obj_t mp_obj_new_list(size_t n, const mp_obj_t *items);
mp_obj_t ret_val[] = {
mp_obj_new_int(123),
mp_obj_new_float(456.789),
mp_obj_new_str("hello", 5),
};
return mp_obj_new_list(3, ret_val);''',

dict: '''\tmp_obj_t ret_val = mp_obj_dict(0);
mp_obj_dict_store(ret_val, mp_obj_new_str("element1", 8), mp_obj_new_int(123));
mp_obj_dict_store(ret_val, mp_obj_new_str("element2", 8), mp_obj_new_float(456.789));
mp_obj_dict_store(ret_val, mp_obj_new_str("element3", 8), mp_obj_new_str("hello", 5));
return ret_val;''',

object: '''\treturn mp_const_none; // TODO''',

inspect._empty: "\treturn mp_const_none;",
None: "\treturn mp_const_none;"
}

Expand Down Expand Up @@ -474,7 +502,14 @@ def headers():
return '''// Include required definitions first.
#include "py/obj.h"
#include "py/runtime.h"
#include "py/builtin.h"'''
#include "py/builtin.h"

/*
// Example exception for any generated function
if (some_val == 0) {
mp_raise_ValueError("'some_val' can't be zero!");
}
*/'''


def function_comments(f):
Expand Down
4 changes: 3 additions & 1 deletion test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def readfrom_mem(addr: int = 0, memaddr: int = 0, arg: object = None, *, addrsiz
assert func.to_c_func_def() == "STATIC mp_obj_t example_readfrom_mem"
assert func.to_c_return_val_init() is None
assert func.to_c_code_body() == "//Your code here"
assert func.to_c_return_value() == "return mp_obj_new_str(<ret_val_ptr>, <ret_val_len>);"
# assert func.to_c_return_value() == "return mp_obj_new_str(<ret_val_ptr>, <ret_val_len>);"
assert func.to_c_return_value() == """\t// signature: mp_obj_t mp_obj_new_bytes(const char* data, size_t len);\n
return mp_obj_new_str("hello", 5);"""
assert func.to_c_define() == "MP_DEFINE_CONST_FUN_OBJ_KW(example_readfrom_mem_obj, 1, example_readfrom_mem);"
assert func.to_c_arg_array_def() == "STATIC const mp_arg_t example_readfrom_mem_allowed_args[]"

Expand Down