Skip to content

Commit 4a4490f

Browse files
committed
py/modio: resource_stream: Implement "package" param handling.
1 parent c1e0eb7 commit 4a4490f

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

py/modio.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,39 @@ STATIC const mp_obj_type_t bufwriter_type = {
133133

134134
#if MICROPY_MODULE_FROZEN_STR
135135
STATIC mp_obj_t resource_stream(mp_obj_t package_in, mp_obj_t path_in) {
136+
VSTR_FIXED(path_buf, MICROPY_ALLOC_PATH_MAX);
137+
size_t len;
138+
139+
// As an extension to pkg_resources.resource_stream(), we support
140+
// package parameter being None, the path_in is interpreted as a
141+
// raw path.
136142
if (package_in != mp_const_none) {
137-
mp_not_implemented("");
143+
mp_obj_t args[5];
144+
args[0] = package_in;
145+
args[1] = mp_const_none; // TODO should be globals
146+
args[2] = mp_const_none; // TODO should be locals
147+
args[3] = mp_const_true; // Pass sentinel "non empty" value to force returning of leaf module
148+
args[4] = MP_OBJ_NEW_SMALL_INT(0);
149+
150+
// TODO lookup __import__ and call that instead of going straight to builtin implementation
151+
mp_obj_t pkg = mp_builtin___import__(5, args);
152+
153+
mp_obj_t dest[2];
154+
mp_load_method_maybe(pkg, MP_QSTR___path__, dest);
155+
if (dest[0] == MP_OBJ_NULL) {
156+
mp_raise_TypeError(NULL);
157+
}
158+
159+
const char *path = mp_obj_str_get_data(dest[0], &len);
160+
vstr_add_strn(&path_buf, path, len);
161+
vstr_add_byte(&path_buf, '/');
138162
}
139163

140-
size_t len;
141164
const char *path = mp_obj_str_get_data(path_in, &len);
142-
const char *data = mp_find_frozen_str(path, &len);
165+
vstr_add_strn(&path_buf, path, len);
166+
167+
len = path_buf.len;
168+
const char *data = mp_find_frozen_str(path_buf.buf, &len);
143169
if (data != NULL) {
144170
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
145171
o->base.type = &mp_type_bytesio;
@@ -150,7 +176,8 @@ STATIC mp_obj_t resource_stream(mp_obj_t package_in, mp_obj_t path_in) {
150176
return MP_OBJ_FROM_PTR(o);
151177
}
152178

153-
return mp_builtin_open(1, &path_in, (mp_map_t*)&mp_const_empty_map);
179+
mp_obj_t path_out = mp_obj_new_str(path_buf.buf, path_buf.len, false);
180+
return mp_builtin_open(1, &path_out, (mp_map_t*)&mp_const_empty_map);
154181
}
155182
MP_DEFINE_CONST_FUN_OBJ_2(resource_stream_obj, resource_stream);
156183
#endif

tests/io/resource_stream.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
print('SKIP')
88
sys.exit()
99

10-
try:
11-
buf = uio.resource_stream("data", "file2")
12-
except NotImplementedError:
13-
pass
10+
buf = uio.resource_stream("data", "file2")
11+
print(buf.read())
1412

1513
# resource_stream(None, ...) look ups from current dir, hence sys.path[0] hack
1614
buf = uio.resource_stream(None, sys.path[0] + "/data/file2")

tests/io/resource_stream.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
1234
2+
1234

0 commit comments

Comments
 (0)