Skip to content

Commit e5a3759

Browse files
committed
py: Factor out mp_obj_is_package() function.
1 parent 8becca7 commit e5a3759

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

py/builtinimport.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555

5656
#define PATH_SEP_CHAR '/'
5757

58+
bool mp_obj_is_package(mp_obj_t module) {
59+
mp_obj_t dest[2];
60+
mp_load_method_maybe(module, MP_QSTR___path__, dest);
61+
return dest[0] != MP_OBJ_NULL;
62+
}
63+
5864
STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) {
5965
//printf("stat %s\n", vstr_str(path));
6066
mp_import_stat_t stat = mp_import_stat(vstr_str(path));

py/obj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,8 @@ typedef struct _mp_obj_module_t {
555555
mp_obj_dict_t *globals;
556556
} mp_obj_module_t;
557557
mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in);
558+
// check if given module object is a package
559+
bool mp_obj_is_package(mp_obj_t module);
558560

559561
// staticmethod and classmethod types; defined here so we can make const versions
560562
// this structure is used for instances of both staticmethod and classmethod

py/runtime.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,7 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
10981098
}
10991099

11001100
// See if it's a package, then can try FS import
1101-
mp_load_method_maybe(module, MP_QSTR___path__, dest);
1102-
if (dest[0] == MP_OBJ_NULL) {
1101+
if (!mp_obj_is_package(module)) {
11031102
goto import_error;
11041103
}
11051104

0 commit comments

Comments
 (0)