Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uPython-mirror
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
TASTE
uPython-mirror
Commits
6e7819ee
Commit
6e7819ee
authored
Feb 20, 2018
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
py/objmodule: Factor common code for calling __init__ on builtin module.
parent
27fa9881
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
24 deletions
+27
-24
py/builtinimport.c
py/builtinimport.c
+1
-13
py/objmodule.c
py/objmodule.c
+17
-11
py/objmodule.h
py/objmodule.h
+9
-0
No files found.
py/builtinimport.c
View file @
6e7819ee
...
...
@@ -389,19 +389,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
}
// found weak linked module
module_obj
=
el
->
value
;
if
(
MICROPY_MODULE_BUILTIN_INIT
)
{
// look for __init__ and call it if it exists
// Note: this code doesn't work fully correctly because it allows the
// __init__ function to be called twice if the module is imported by its
// non-weak-link name. Also, this code is duplicated in objmodule.c.
mp_obj_t
dest
[
2
];
mp_load_method_maybe
(
el
->
value
,
MP_QSTR___init__
,
dest
);
if
(
dest
[
0
]
!=
MP_OBJ_NULL
)
{
mp_call_method_n_kw
(
0
,
0
,
dest
);
// register module so __init__ is not called again
mp_module_register
(
mod_name
,
el
->
value
);
}
}
mp_module_call_init
(
mod_name
,
module_obj
);
}
else
{
no_exist:
#else
...
...
py/objmodule.c
View file @
6e7819ee
...
...
@@ -247,17 +247,7 @@ mp_obj_t mp_module_get(qstr module_name) {
if
(
el
==
NULL
)
{
return
MP_OBJ_NULL
;
}
if
(
MICROPY_MODULE_BUILTIN_INIT
)
{
// look for __init__ and call it if it exists
mp_obj_t
dest
[
2
];
mp_load_method_maybe
(
el
->
value
,
MP_QSTR___init__
,
dest
);
if
(
dest
[
0
]
!=
MP_OBJ_NULL
)
{
mp_call_method_n_kw
(
0
,
0
,
dest
);
// register module so __init__ is not called again
mp_module_register
(
module_name
,
el
->
value
);
}
}
mp_module_call_init
(
module_name
,
el
->
value
);
}
// module found, return it
...
...
@@ -268,3 +258,19 @@ void mp_module_register(qstr qst, mp_obj_t module) {
mp_map_t
*
mp_loaded_modules_map
=
&
MP_STATE_VM
(
mp_loaded_modules_dict
).
map
;
mp_map_lookup
(
mp_loaded_modules_map
,
MP_OBJ_NEW_QSTR
(
qst
),
MP_MAP_LOOKUP_ADD_IF_NOT_FOUND
)
->
value
=
module
;
}
#if MICROPY_MODULE_BUILTIN_INIT
void
mp_module_call_init
(
qstr
module_name
,
mp_obj_t
module_obj
)
{
// Look for __init__ and call it if it exists
mp_obj_t
dest
[
2
];
mp_load_method_maybe
(
module_obj
,
MP_QSTR___init__
,
dest
);
if
(
dest
[
0
]
!=
MP_OBJ_NULL
)
{
mp_call_method_n_kw
(
0
,
0
,
dest
);
// Register module so __init__ is not called again.
// If a module can be referenced by more than one name (eg due to weak links)
// then __init__ will still be called for each distinct import, and it's then
// up to the particular module to make sure it's __init__ code only runs once.
mp_module_register
(
module_name
,
module_obj
);
}
}
#endif
py/objmodule.h
View file @
6e7819ee
...
...
@@ -34,4 +34,13 @@ extern const mp_map_t mp_builtin_module_weak_links_map;
mp_obj_t
mp_module_get
(
qstr
module_name
);
void
mp_module_register
(
qstr
qstr
,
mp_obj_t
module
);
#if MICROPY_MODULE_BUILTIN_INIT
void
mp_module_call_init
(
qstr
module_name
,
mp_obj_t
module_obj
);
#else
static
inline
void
mp_module_call_init
(
qstr
module_name
,
mp_obj_t
module_obj
)
{
(
void
)
module_name
;
(
void
)
module_obj
;
}
#endif
#endif // MICROPY_INCLUDED_PY_OBJMODULE_H
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment