File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -429,8 +429,13 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
429429
430430 // if args[3] (fromtuple) has magic value False, set up
431431 // this module for command-line "-m" option (set module's
432- // name to __main__ instead of real name).
433- if (i == mod_len && fromtuple == mp_const_false ) {
432+ // name to __main__ instead of real name). Do this only
433+ // for *modules* however - packages never have their names
434+ // replaced, instead they're -m'ed using a special __main__
435+ // submodule in them. (This all apparently is done to not
436+ // touch package name itself, which is important for future
437+ // imports).
438+ if (i == mod_len && fromtuple == mp_const_false && stat != MP_IMPORT_STAT_DIR ) {
434439 mp_obj_module_t * o = MP_OBJ_TO_PTR (module_obj );
435440 mp_obj_dict_store (MP_OBJ_FROM_PTR (o -> globals ), MP_OBJ_NEW_QSTR (MP_QSTR___name__ ), MP_OBJ_NEW_QSTR (MP_QSTR___main__ ));
436441 #if MICROPY_CPYTHON_COMPAT
Original file line number Diff line number Diff line change @@ -553,6 +553,9 @@ MP_NOINLINE int main_(int argc, char **argv) {
553553
554554 mp_obj_t mod ;
555555 nlr_buf_t nlr ;
556+ bool subpkg_tried = false;
557+
558+ reimport :
556559 if (nlr_push (& nlr ) == 0 ) {
557560 mod = mp_builtin___import__ (MP_ARRAY_SIZE (import_args ), import_args );
558561 nlr_pop ();
@@ -561,11 +564,17 @@ MP_NOINLINE int main_(int argc, char **argv) {
561564 return handle_uncaught_exception (nlr .ret_val ) & 0xff ;
562565 }
563566
564- if (mp_obj_is_package (mod )) {
565- // TODO
566- mp_printf (& mp_stderr_print , "%s: -m for packages not yet implemented\n" , argv [0 ]);
567- exit (1 );
567+ if (mp_obj_is_package (mod ) && !subpkg_tried ) {
568+ subpkg_tried = true;
569+ vstr_t vstr ;
570+ int len = strlen (argv [a + 1 ]);
571+ vstr_init (& vstr , len + sizeof (".__main__" ));
572+ vstr_add_strn (& vstr , argv [a + 1 ], len );
573+ vstr_add_strn (& vstr , ".__main__" , sizeof (".__main__" ) - 1 );
574+ import_args [0 ] = mp_obj_new_str_from_vstr (& mp_type_str , & vstr );
575+ goto reimport ;
568576 }
577+
569578 ret = 0 ;
570579 break ;
571580 } else if (strcmp (argv [a ], "-X" ) == 0 ) {
You can’t perform that action at this time.
0 commit comments