Skip to content

Commit ea1a5e4

Browse files
jimmodpgeorge
authored andcommitted
examples/natmod/deflate: Add deflate as a dynamic native module.
This replaces the previous zlib version. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <[email protected]>
1 parent b804443 commit ea1a5e4

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

examples/natmod/deflate/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Location of top-level MicroPython directory
2+
MPY_DIR = ../../..
3+
4+
# Name of module (different to built-in uzlib so it can coexist)
5+
MOD = deflate_$(ARCH)
6+
7+
# Source files (.c or .py)
8+
SRC = deflate.c
9+
10+
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
11+
ARCH = x64
12+
13+
include $(MPY_DIR)/py/dynruntime.mk

examples/natmod/deflate/deflate.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#define MICROPY_PY_DEFLATE (1)
2+
#define MICROPY_PY_DEFLATE_COMPRESS (1)
3+
4+
#include "py/dynruntime.h"
5+
6+
#if !defined(__linux__)
7+
void *memcpy(void *dst, const void *src, size_t n) {
8+
return mp_fun_table.memmove_(dst, src, n);
9+
}
10+
void *memset(void *s, int c, size_t n) {
11+
return mp_fun_table.memset_(s, c, n);
12+
}
13+
#endif
14+
15+
mp_obj_full_type_t deflateio_type;
16+
17+
#include "extmod/moddeflate.c"
18+
19+
// Re-implemented from py/stream.c, not yet available in dynruntime.h.
20+
mp_obj_t mp_stream_close(mp_obj_t stream) {
21+
const mp_stream_p_t *stream_p = mp_get_stream(stream);
22+
int error;
23+
mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
24+
if (res == MP_STREAM_ERROR) {
25+
mp_raise_OSError(error);
26+
}
27+
return mp_const_none;
28+
}
29+
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_close_obj, mp_stream_close);
30+
31+
// Re-implemented from py/stream.c, not yet available in dynruntime.h.
32+
STATIC mp_obj_t mp_stream___exit__(size_t n_args, const mp_obj_t *args) {
33+
(void)n_args;
34+
return mp_stream_close(args[0]);
35+
}
36+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream___exit___obj, 4, 4, mp_stream___exit__);
37+
38+
// Re-implemented from obj.c, not yet available in dynruntime.h.
39+
mp_obj_t mp_identity(mp_obj_t self) {
40+
return self;
41+
}
42+
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
43+
44+
mp_map_elem_t deflateio_locals_dict_table[7];
45+
STATIC MP_DEFINE_CONST_DICT(deflateio_locals_dict, deflateio_locals_dict_table);
46+
47+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
48+
MP_DYNRUNTIME_INIT_ENTRY
49+
50+
deflateio_type.base.type = mp_fun_table.type_type;
51+
deflateio_type.name = MP_QSTR_DeflateIO;
52+
MP_OBJ_TYPE_SET_SLOT(&deflateio_type, make_new, &deflateio_make_new, 0);
53+
MP_OBJ_TYPE_SET_SLOT(&deflateio_type, protocol, &deflateio_stream_p, 1);
54+
deflateio_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_OBJ_FROM_PTR(&mp_stream_read_obj) };
55+
deflateio_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_OBJ_FROM_PTR(&mp_stream_readinto_obj) };
56+
deflateio_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_readline), MP_OBJ_FROM_PTR(&mp_stream_unbuffered_readline_obj) };
57+
deflateio_locals_dict_table[3] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_OBJ_FROM_PTR(&mp_stream_write_obj) };
58+
deflateio_locals_dict_table[4] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_close), MP_OBJ_FROM_PTR(&mp_stream_close_obj) };
59+
deflateio_locals_dict_table[5] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), MP_OBJ_FROM_PTR(&mp_identity_obj) };
60+
deflateio_locals_dict_table[6] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR___exit__), MP_OBJ_FROM_PTR(&mp_stream___exit___obj) };
61+
MP_OBJ_TYPE_SET_SLOT(&deflateio_type, locals_dict, (void*)&deflateio_locals_dict, 2);
62+
63+
mp_store_global(MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR_deflate));
64+
mp_store_global(MP_QSTR_DeflateIO, MP_OBJ_FROM_PTR(&deflateio_type));
65+
mp_store_global(MP_QSTR_RAW, MP_OBJ_NEW_SMALL_INT(DEFLATEIO_FORMAT_RAW));
66+
mp_store_global(MP_QSTR_ZLIB, MP_OBJ_NEW_SMALL_INT(DEFLATEIO_FORMAT_ZLIB));
67+
mp_store_global(MP_QSTR_GZIP, MP_OBJ_NEW_SMALL_INT(DEFLATEIO_FORMAT_GZIP));
68+
69+
MP_DYNRUNTIME_INIT_EXIT
70+
}

tests/run-natmodtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# Supported tests and their corresponding mpy module
2222
TEST_MAPPINGS = {
2323
"btree": "btree/btree_$(ARCH).mpy",
24+
"deflate": "deflate/deflate_$(ARCH).mpy",
2425
"framebuf": "framebuf/framebuf_$(ARCH).mpy",
2526
"heapq": "heapq/heapq_$(ARCH).mpy",
2627
"random": "random/random_$(ARCH).mpy",

tools/ci.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ function ci_native_mpy_modules_build {
426426
make -C examples/natmod/features2 ARCH=$arch
427427
make -C examples/natmod/features3 ARCH=$arch
428428
make -C examples/natmod/btree ARCH=$arch
429+
make -C examples/natmod/deflate ARCH=$arch
429430
make -C examples/natmod/framebuf ARCH=$arch
430431
make -C examples/natmod/heapq ARCH=$arch
431432
make -C examples/natmod/random ARCH=$arch
@@ -495,7 +496,7 @@ function ci_unix_coverage_run_mpy_merge_tests {
495496

496497
function ci_unix_coverage_run_native_mpy_tests {
497498
MICROPYPATH=examples/natmod/features2 ./ports/unix/build-coverage/micropython -m features2
498-
(cd tests && ./run-natmodtests.py "$@" extmod/{btree*,framebuf*,heapq*,random*,re*}.py)
499+
(cd tests && ./run-natmodtests.py "$@" extmod/{btree*,deflate*,framebuf*,heapq*,random*,re*}.py)
499500
}
500501

501502
function ci_unix_32bit_setup {

0 commit comments

Comments
 (0)