py/modio: Remove io.resource_stream function.
This feature is not enabled on any port, it's not in CPython's io module, and functionality is better suited to the micropython-lib implementation of pkg_resources.
This commit is contained in:
parent
d6dc4cb65a
commit
cc23e99f32
@ -51,7 +51,6 @@
|
|||||||
#define MICROPY_PY_MATH_FACTORIAL (1)
|
#define MICROPY_PY_MATH_FACTORIAL (1)
|
||||||
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
|
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
|
||||||
#define MICROPY_PY_IO_BUFFEREDWRITER (1)
|
#define MICROPY_PY_IO_BUFFEREDWRITER (1)
|
||||||
#define MICROPY_PY_IO_RESOURCE_STREAM (1)
|
|
||||||
#define MICROPY_PY_UASYNCIO (1)
|
#define MICROPY_PY_UASYNCIO (1)
|
||||||
#define MICROPY_PY_URE_DEBUG (1)
|
#define MICROPY_PY_URE_DEBUG (1)
|
||||||
#define MICROPY_PY_URE_MATCH_GROUPS (1)
|
#define MICROPY_PY_URE_MATCH_GROUPS (1)
|
||||||
|
47
py/modio.c
47
py/modio.c
@ -204,50 +204,6 @@ STATIC const mp_obj_type_t mp_type_bufwriter = {
|
|||||||
};
|
};
|
||||||
#endif // MICROPY_PY_IO_BUFFEREDWRITER
|
#endif // MICROPY_PY_IO_BUFFEREDWRITER
|
||||||
|
|
||||||
#if MICROPY_PY_IO_RESOURCE_STREAM
|
|
||||||
STATIC mp_obj_t resource_stream(mp_obj_t package_in, mp_obj_t path_in) {
|
|
||||||
VSTR_FIXED(path_buf, MICROPY_ALLOC_PATH_MAX);
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
// As an extension to pkg_resources.resource_stream(), we support
|
|
||||||
// package parameter being None, the path_in is interpreted as a
|
|
||||||
// raw path.
|
|
||||||
if (package_in != mp_const_none) {
|
|
||||||
// Pass "True" as sentinel value in fromlist to force returning of leaf module
|
|
||||||
mp_obj_t pkg = mp_import_name(mp_obj_str_get_qstr(package_in), mp_const_true, MP_OBJ_NEW_SMALL_INT(0));
|
|
||||||
|
|
||||||
mp_obj_t dest[2];
|
|
||||||
mp_load_method_maybe(pkg, MP_QSTR___path__, dest);
|
|
||||||
if (dest[0] == MP_OBJ_NULL) {
|
|
||||||
mp_raise_TypeError(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *path = mp_obj_str_get_data(dest[0], &len);
|
|
||||||
vstr_add_strn(&path_buf, path, len);
|
|
||||||
vstr_add_byte(&path_buf, '/');
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *path = mp_obj_str_get_data(path_in, &len);
|
|
||||||
vstr_add_strn(&path_buf, path, len);
|
|
||||||
|
|
||||||
len = path_buf.len;
|
|
||||||
const char *data = mp_find_frozen_str(path_buf.buf, &len);
|
|
||||||
if (data != NULL) {
|
|
||||||
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
|
|
||||||
o->base.type = &mp_type_bytesio;
|
|
||||||
o->vstr = m_new_obj(vstr_t);
|
|
||||||
vstr_init_fixed_buf(o->vstr, len + 1, (char *)data);
|
|
||||||
o->vstr->len = len;
|
|
||||||
o->pos = 0;
|
|
||||||
return MP_OBJ_FROM_PTR(o);
|
|
||||||
}
|
|
||||||
|
|
||||||
mp_obj_t path_out = mp_obj_new_str(path_buf.buf, path_buf.len);
|
|
||||||
return mp_builtin_open(1, &path_out, (mp_map_t *)&mp_const_empty_map);
|
|
||||||
}
|
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(resource_stream_obj, resource_stream);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
|
STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
|
||||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uio) },
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uio) },
|
||||||
// Note: mp_builtin_open_obj should be defined by port, it's not
|
// Note: mp_builtin_open_obj should be defined by port, it's not
|
||||||
@ -256,9 +212,6 @@ STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
|
|||||||
#if MICROPY_PY_IO_IOBASE
|
#if MICROPY_PY_IO_IOBASE
|
||||||
{ MP_ROM_QSTR(MP_QSTR_IOBase), MP_ROM_PTR(&mp_type_iobase) },
|
{ MP_ROM_QSTR(MP_QSTR_IOBase), MP_ROM_PTR(&mp_type_iobase) },
|
||||||
#endif
|
#endif
|
||||||
#if MICROPY_PY_IO_RESOURCE_STREAM
|
|
||||||
{ MP_ROM_QSTR(MP_QSTR_resource_stream), MP_ROM_PTR(&resource_stream_obj) },
|
|
||||||
#endif
|
|
||||||
#if MICROPY_PY_IO_FILEIO
|
#if MICROPY_PY_IO_FILEIO
|
||||||
{ MP_ROM_QSTR(MP_QSTR_FileIO), MP_ROM_PTR(&mp_type_fileio) },
|
{ MP_ROM_QSTR(MP_QSTR_FileIO), MP_ROM_PTR(&mp_type_fileio) },
|
||||||
#if MICROPY_CPYTHON_COMPAT
|
#if MICROPY_CPYTHON_COMPAT
|
||||||
|
@ -1281,17 +1281,6 @@ typedef double mp_float_t;
|
|||||||
#define MICROPY_PY_IO_IOBASE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
|
#define MICROPY_PY_IO_IOBASE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Whether to provide "uio.resource_stream()" function with
|
|
||||||
// the semantics of CPython's pkg_resources.resource_stream()
|
|
||||||
// (allows to access binary resources in frozen source packages).
|
|
||||||
// Note that the same functionality can be achieved in "pure
|
|
||||||
// Python" by prepocessing binary resources into Python source
|
|
||||||
// and bytecode-freezing it (with a simple helper module available
|
|
||||||
// e.g. in micropython-lib).
|
|
||||||
#ifndef MICROPY_PY_IO_RESOURCE_STREAM
|
|
||||||
#define MICROPY_PY_IO_RESOURCE_STREAM (0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Whether to provide "io.FileIO" class
|
// Whether to provide "io.FileIO" class
|
||||||
#ifndef MICROPY_PY_IO_FILEIO
|
#ifndef MICROPY_PY_IO_FILEIO
|
||||||
#define MICROPY_PY_IO_FILEIO (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
|
#define MICROPY_PY_IO_FILEIO (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
import uio
|
|
||||||
import usys
|
|
||||||
|
|
||||||
try:
|
|
||||||
uio.resource_stream
|
|
||||||
except AttributeError:
|
|
||||||
print("SKIP")
|
|
||||||
raise SystemExit
|
|
||||||
|
|
||||||
buf = uio.resource_stream("data", "file2")
|
|
||||||
print(buf.read())
|
|
||||||
|
|
||||||
# resource_stream(None, ...) look ups from current dir, hence sys.path[0] hack
|
|
||||||
buf = uio.resource_stream(None, usys.path[0] + "/data/file2")
|
|
||||||
print(buf.read())
|
|
@ -1,2 +0,0 @@
|
|||||||
1234
|
|
||||||
1234
|
|
@ -89,12 +89,6 @@ try:
|
|||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
print("ZeroDivisionError")
|
print("ZeroDivisionError")
|
||||||
|
|
||||||
# test loading a resource from a frozen string
|
|
||||||
import uio
|
|
||||||
|
|
||||||
buf = uio.resource_stream("frzstr_pkg2", "mod.py")
|
|
||||||
print(buf.read(21))
|
|
||||||
|
|
||||||
# test for MP_QSTR_NULL regression
|
# test for MP_QSTR_NULL regression
|
||||||
from frzqstr import returns_NULL
|
from frzqstr import returns_NULL
|
||||||
|
|
||||||
|
@ -174,5 +174,4 @@ frzstr_pkg2.mod
|
|||||||
frzmpy_pkg2.mod
|
frzmpy_pkg2.mod
|
||||||
1
|
1
|
||||||
ZeroDivisionError
|
ZeroDivisionError
|
||||||
b'# test frozen package'
|
|
||||||
NULL
|
NULL
|
||||||
|
Loading…
x
Reference in New Issue
Block a user