Skip to content

Commit 9c91448

Browse files
committed
Re-add usercmodule sources & endorse new extra_coverage output
1 parent d9ed21e commit 9c91448

10 files changed

Lines changed: 167 additions & 20 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Include MicroPython API.
2+
#include "py/runtime.h"
3+
4+
// This is the function which will be called from Python as cexample.add_ints(a, b).
5+
STATIC mp_obj_t example_add_ints(mp_obj_t a_obj, mp_obj_t b_obj) {
6+
// Extract the ints from the micropython input objects.
7+
int a = mp_obj_get_int(a_obj);
8+
int b = mp_obj_get_int(b_obj);
9+
10+
// Calculate the addition and convert to MicroPython object.
11+
return mp_obj_new_int(a + b);
12+
}
13+
// Define a Python reference to the function above.
14+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(example_add_ints_obj, example_add_ints);
15+
16+
// Define all properties of the module.
17+
// Table entries are key/value pairs of the attribute name (a string)
18+
// and the MicroPython object reference.
19+
// All identifiers and strings are written as MP_QSTR_xxx and will be
20+
// optimized to word-sized integers by the build system (interned strings).
21+
STATIC const mp_rom_map_elem_t example_module_globals_table[] = {
22+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cexample) },
23+
{ MP_ROM_QSTR(MP_QSTR_add_ints), MP_ROM_PTR(&example_add_ints_obj) },
24+
};
25+
STATIC MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table);
26+
27+
// Define module object.
28+
const mp_obj_module_t example_user_cmodule = {
29+
.base = { &mp_type_module },
30+
.globals = (mp_obj_dict_t *)&example_module_globals,
31+
};
32+
33+
// Register the module to make it available in Python.
34+
MP_REGISTER_MODULE(MP_QSTR_cexample, example_user_cmodule);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Create an INTERFACE library for our C module.
2+
add_library(usermod_cexample INTERFACE)
3+
4+
# Add our source files to the lib
5+
target_sources(usermod_cexample INTERFACE
6+
${CMAKE_CURRENT_LIST_DIR}/examplemodule.c
7+
)
8+
9+
# Add the current directory as an include directory.
10+
target_include_directories(usermod_cexample INTERFACE
11+
${CMAKE_CURRENT_LIST_DIR}
12+
)
13+
14+
# Link our INTERFACE library to the usermod target.
15+
target_link_libraries(usermod INTERFACE usermod_cexample)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
EXAMPLE_MOD_DIR := $(USERMOD_DIR)
2+
3+
# Add all C files to SRC_USERMOD.
4+
SRC_USERMOD += $(EXAMPLE_MOD_DIR)/examplemodule.c
5+
6+
# We can add our module folder to include paths if needed
7+
# This is not actually needed in this example.
8+
CFLAGS_USERMOD += -I$(EXAMPLE_MOD_DIR)
9+
CEXAMPLE_MOD_DIR := $(USERMOD_DIR)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
extern "C" {
2+
#include <examplemodule.h>
3+
4+
// Here we implement the function using C++ code, but since it's
5+
// declaration has to be compatible with C everything goes in extern "C" scope.
6+
mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj) {
7+
// Prove we have (at least) C++11 features.
8+
const auto a = mp_obj_get_int(a_obj);
9+
const auto b = mp_obj_get_int(b_obj);
10+
const auto sum = [&]() {
11+
return mp_obj_new_int(a + b);
12+
} ();
13+
// Prove we're being scanned for QSTRs.
14+
mp_obj_t tup[] = {sum, MP_ROM_QSTR(MP_QSTR_hellocpp)};
15+
return mp_obj_new_tuple(2, tup);
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <examplemodule.h>
2+
3+
// Define a Python reference to the function we'll make available.
4+
// See example.cpp for the definition.
5+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(cppfunc_obj, cppfunc);
6+
7+
// Define all properties of the module.
8+
// Table entries are key/value pairs of the attribute name (a string)
9+
// and the MicroPython object reference.
10+
// All identifiers and strings are written as MP_QSTR_xxx and will be
11+
// optimized to word-sized integers by the build system (interned strings).
12+
STATIC const mp_rom_map_elem_t cppexample_module_globals_table[] = {
13+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cppexample) },
14+
{ MP_ROM_QSTR(MP_QSTR_cppfunc), MP_ROM_PTR(&cppfunc_obj) },
15+
};
16+
STATIC MP_DEFINE_CONST_DICT(cppexample_module_globals, cppexample_module_globals_table);
17+
18+
// Define module object.
19+
const mp_obj_module_t cppexample_user_cmodule = {
20+
.base = { &mp_type_module },
21+
.globals = (mp_obj_dict_t *)&cppexample_module_globals,
22+
};
23+
24+
// Register the module to make it available in Python.
25+
MP_REGISTER_MODULE(MP_QSTR_cppexample, cppexample_user_cmodule);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Include MicroPython API.
2+
#include "py/runtime.h"
3+
4+
// Declare the function we'll make available in Python as cppexample.cppfunc().
5+
extern mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Create an INTERFACE library for our CPP module.
2+
add_library(usermod_cppexample INTERFACE)
3+
4+
# Add our source files to the library.
5+
target_sources(usermod_cppexample INTERFACE
6+
${CMAKE_CURRENT_LIST_DIR}/example.cpp
7+
${CMAKE_CURRENT_LIST_DIR}/examplemodule.c
8+
)
9+
10+
# Add the current directory as an include directory.
11+
target_include_directories(usermod_cppexample INTERFACE
12+
${CMAKE_CURRENT_LIST_DIR}
13+
)
14+
15+
# Link our INTERFACE library to the usermod target.
16+
target_link_libraries(usermod INTERFACE usermod_cppexample)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CPPEXAMPLE_MOD_DIR := $(USERMOD_DIR)
2+
3+
# Add our source files to the respective variables.
4+
SRC_USERMOD += $(CPPEXAMPLE_MOD_DIR)/examplemodule.c
5+
SRC_USERMOD_CXX += $(CPPEXAMPLE_MOD_DIR)/example.cpp
6+
7+
# Add our module directory to the include path.
8+
CFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR)
9+
CXXFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR) -std=c++11
10+
11+
# We use C++ features so have to link against the standard library.
12+
LDFLAGS_USERMOD += -lstdc++
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This top-level micropython.cmake is responsible for listing
2+
# the individual modules we want to include.
3+
# Paths are absolute, and ${CMAKE_CURRENT_LIST_DIR} can be
4+
# used to prefix subdirectories.
5+
6+
# Add the C example.
7+
include(${CMAKE_CURRENT_LIST_DIR}/cexample/micropython.cmake)
8+
9+
# Add the CPP example.
10+
include(${CMAKE_CURRENT_LIST_DIR}/cppexample/micropython.cmake)

tests/unix/extra_coverage.py.exp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abc
1818
0x0
1919
0x0
2020
# tracked allocation
21-
m_tracked_head = 0
21+
m_tracked_head = 0x0
2222
0 1
2323
1 1
2424
2 1
@@ -35,7 +35,7 @@ m_tracked_head = 0
3535
5 1
3636
6 1
3737
7 1
38-
m_tracked_head = 0
38+
m_tracked_head = 0x0
3939
# vstr
4040
tests
4141
sts
@@ -48,30 +48,29 @@ RuntimeError:
4848
ame__
4949
mport
5050

51-
builtins micropython __future__ _asyncio
52-
_thread _uasyncio aesio array
53-
audiocore audiomixer binascii bitmaptools
54-
btree cexample cmath collections
55-
cppexample displayio errno ffi
56-
framebuf gc hashlib json
57-
math qrio rainbowio re
58-
struct synthio sys termios
59-
traceback ubinascii uctypes uerrno
60-
uheapq uio ujson ulab
61-
ulab.numpy ulab.numpy.fft ulab.numpy.linalg
62-
ulab.scipy ulab.scipy.linalg
63-
ulab.scipy.optimize ulab.scipy.signal
64-
ulab.scipy.special ulab.utils uos
65-
urandom ure uselect utime
66-
utimeq uzlib zlib
51+
builtins micropython __future__ _thread
52+
_uasyncio aesio array audiocore
53+
audiomixer bitmaptools cexample cmath
54+
collections cppexample displayio gc
55+
math qrio rainbowio struct
56+
synthio termios traceback ubinascii
57+
uctypes uerrno uhashlib uheapq
58+
uio ujson ulab ulab.numpy
59+
ulab.numpy.fft ulab.numpy.linalg ulab.scipy
60+
ulab.scipy.linalg ulab.scipy.optimize
61+
ulab.scipy.signal ulab.scipy.special
62+
ulab.utils uos urandom ure
63+
uselect usys utime utimeq
64+
uzlib zlib
6765
ime
6866

6967
utime utimeq
7068

7169
argv atexit byteorder exc_info
7270
exit getsizeof implementation maxsize
73-
modules path platform stderr
74-
stdin stdout version version_info
71+
modules path platform print_exception
72+
ps1 ps2 stderr stdin
73+
stdout tracebacklimit version version_info
7574
ementation
7675
# attrtuple
7776
(start=1, stop=2, step=3)
@@ -199,4 +198,9 @@ frzstr_pkg2.mod
199198
frzmpy_pkg2.mod
200199
1
201200
ZeroDivisionError
201+
\
202+
203+
X
204+
'\x1b'
205+
b'\x00\xff'
202206
NULL

0 commit comments

Comments
 (0)