diff --git a/Modules/main.c b/Modules/main.c --- a/Modules/main.c +++ b/Modules/main.c @@ -679,18 +679,21 @@ } if (command != NULL) { - /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ - _PyOS_optind--; - argv[_PyOS_optind] = L"-c"; - } - - if (module != NULL) { - /* Backup _PyOS_optind and force sys.argv[0] = '-m'*/ - _PyOS_optind--; - argv[_PyOS_optind] = L"-m"; - } - - PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); + // Build sys.argv: ['-c', 'command', 'other', 'args', ...] + // _PyOS_optind: index of the command argument in argv, +1. + // argv[_PyOS_optind-2] is '-c'. Starting from there, + // transfer all remaining arguments to sys.argv. + PySys_SetArgv(argc-_PyOS_optind+2, argv+_PyOS_optind-2); + } else if (module != NULL) { + // Build sys.argv: ['-m', 'other', 'args', ...] + // argv[_PyOS_optind-1] is 'module'. Replace with '-m' and, + // starting from there, transfer all remaining arguments to + // sys.argv. '-m' is about to be replaced with module info + // later on, by the runpy module. + argv[_PyOS_optind-1] = L"-m"; + PySys_SetArgv(argc-_PyOS_optind+1, argv+_PyOS_optind-1); + } else + PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) && isatty(fileno(stdin))) {