Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
4a4490ff
Commit
4a4490ff
authored
7 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
py/modio: resource_stream: Implement "package" param handling.
parent
c1e0eb7a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/modio.c
+31
-4
31 additions, 4 deletions
py/modio.c
tests/io/resource_stream.py
+2
-4
2 additions, 4 deletions
tests/io/resource_stream.py
tests/io/resource_stream.py.exp
+1
-0
1 addition, 0 deletions
tests/io/resource_stream.py.exp
with
34 additions
and
8 deletions
py/modio.c
+
31
−
4
View file @
4a4490ff
...
...
@@ -133,13 +133,39 @@ STATIC const mp_obj_type_t bufwriter_type = {
#if MICROPY_MODULE_FROZEN_STR
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
)
{
mp_not_implemented
(
""
);
mp_obj_t
args
[
5
];
args
[
0
]
=
package_in
;
args
[
1
]
=
mp_const_none
;
// TODO should be globals
args
[
2
]
=
mp_const_none
;
// TODO should be locals
args
[
3
]
=
mp_const_true
;
// Pass sentinel "non empty" value to force returning of leaf module
args
[
4
]
=
MP_OBJ_NEW_SMALL_INT
(
0
);
// TODO lookup __import__ and call that instead of going straight to builtin implementation
mp_obj_t
pkg
=
mp_builtin___import__
(
5
,
args
);
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
,
'/'
);
}
size_t
len
;
const
char
*
path
=
mp_obj_str_get_data
(
path_in
,
&
len
);
const
char
*
data
=
mp_find_frozen_str
(
path
,
&
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
;
...
...
@@ -150,7 +176,8 @@ STATIC mp_obj_t resource_stream(mp_obj_t package_in, mp_obj_t path_in) {
return
MP_OBJ_FROM_PTR
(
o
);
}
return
mp_builtin_open
(
1
,
&
path_in
,
(
mp_map_t
*
)
&
mp_const_empty_map
);
mp_obj_t
path_out
=
mp_obj_new_str
(
path_buf
.
buf
,
path_buf
.
len
,
false
);
return
mp_builtin_open
(
1
,
&
path_out
,
(
mp_map_t
*
)
&
mp_const_empty_map
);
}
MP_DEFINE_CONST_FUN_OBJ_2
(
resource_stream_obj
,
resource_stream
);
#endif
...
...
This diff is collapsed.
Click to expand it.
tests/io/resource_stream.py
+
2
−
4
View file @
4a4490ff
...
...
@@ -7,10 +7,8 @@ except AttributeError:
print
(
'
SKIP
'
)
sys
.
exit
()
try
:
buf
=
uio
.
resource_stream
(
"
data
"
,
"
file2
"
)
except
NotImplementedError
:
pass
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
,
sys
.
path
[
0
]
+
"
/data/file2
"
)
...
...
This diff is collapsed.
Click to expand it.
tests/io/resource_stream.py.exp
+
1
−
0
View file @
4a4490ff
1234
1234
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment