jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/602298 )
Change subject: [bugfix] Use the scripts args executing a script with pwb.py ......................................................................
[bugfix] Use the scripts args executing a script with pwb.py
- pwb.py is able to handle global args. These must not be passed to the script as arguments. Therefore use the result of handle_args function instead of argvu list to be passed to run_python_file - call run_python_file with only 3 parameters: filename, script_args and file_package - combine these 3 parameters to fill sys.argv and pwb.argvu inside run_python_file function - update generate_user_files call - tests were added already. Check also the argv content.
Bug: T254435 Change-Id: Ic201599583d6527047050846c12a898f0d2f0b2c --- M pwb.py M tests/pwb_tests.py 2 files changed, 20 insertions(+), 18 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pwb.py b/pwb.py index 8c5da9f..1f6741d 100755 --- a/pwb.py +++ b/pwb.py @@ -102,12 +102,16 @@ # https://bitbucket.org/ned/coveragepy/src/2c5fb3a8b81c/setup.py?at=default#cl...
-def run_python_file(filename, argv, argvu, package=None): +def run_python_file(filename, args, package=None): """Run a python file as if it were the main program on the command line.
- `filename` is the path to the file to execute, it need not be a .py file. - `args` is the argument array to present as sys.argv, as unicode strings. - + :param filename: The path to the file to execute, it need not be a + .py file. + :type filename: str + :param args: is the argument list to present as sys.argv, as strings. + :type args: List[str] + :param package: The package of the script. Used for checks. + :type package: Optional[module] """ # Create a module to serve as __main__ old_main_mod = sys.modules['__main__'] @@ -123,8 +127,8 @@ old_argv = sys.argv old_argvu = pwb.argvu
- sys.argv = argv - pwb.argvu = argvu + sys.argv = [filename] + args + pwb.argvu = [Path(filename).stem] + args sys.path.insert(0, os.path.dirname(filename))
try: @@ -281,9 +285,7 @@ or filename == 'version.py'): print("NOTE: 'user-config.py' was not found!") print('Please follow the prompts to create it:') - run_python_file(os.path.join(_pwb_dir, 'generate_user_files.py'), - ['generate_user_files.py'], - ['generate_user_files.py']) + run_python_file(os.path.join(_pwb_dir, 'generate_user_files.py'), []) # because we have loaded pywikibot without user-config.py loaded, # we need to re-start the entire process. Ask the user to do so. print('Now, you have to re-execute the command to start your script.') @@ -403,7 +405,6 @@ return False
file_package = None - argvu = pwb.argvu[1:]
if not os.path.exists(filename): filename = find_filename(filename) @@ -440,10 +441,7 @@ help_option = any(arg.startswith('-help:') or arg == '-help' for arg in script_args) if site_package or check_modules(filename) or help_option: - run_python_file(filename, - [filename] + script_args, - [Path(filename).stem] + argvu[1:], - module) + run_python_file(filename, script_args, module) return True
diff --git a/tests/pwb_tests.py b/tests/pwb_tests.py index 66d4298..2d59c6b 100644 --- a/tests/pwb_tests.py +++ b/tests/pwb_tests.py @@ -75,18 +75,22 @@ self.assertEqual('Häuser', vpwb['stdout'].strip()) self.assertEqual('Häuser', vpwb['stderr'].strip())
- @unittest.expectedFailure # T254435 def test_argv(self): """Test argv of pywikibot.
Make sure that argv passed to the script is not contaminated by global options given to pwb.py wrapper. """ - script_path = join_pwb_tests_path('print_argv.py') - without_global_args = execute_pwb([script_path, '-help']) - with_no_global_args = execute_pwb(['-maxlag:5', script_path, '-help']) + script_name = 'print_argv' + script_path = join_pwb_tests_path(script_name + '.py') + script_opts = ['-help'] + command = [script_path] + script_opts + without_global_args = execute_pwb(command) + with_no_global_args = execute_pwb(['-maxlag:5'] + command) self.assertEqual(without_global_args['stdout'], with_no_global_args['stdout']) + self.assertEqual(without_global_args['stdout'].rstrip(), + str([script_name] + script_opts))
def test_script_found(self): """Test pwb.py script call which is found."""
pywikibot-commits@lists.wikimedia.org