jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/492466 )
Change subject: pwb.py: Simplify the import system ......................................................................
pwb.py: Simplify the import system
- Use `import pywikibot as pwb` instead of `pwb = pywikibot` in the else clause of the try statement. - There is no need to call tryimport_pwb in main() because it's already been imported earlier. - There is no need to call tryimport_pwb in run_python_file(), just make sure it is imported before calling it. - No need for remove_modules() function. AFAICT, none of the config-dependant modules can be loaded successfully when the config file is not present. Removing and reimporting others is of no use. - Remove the tryimport_pwb() as is it no longer used.
Change-Id: I49d728e08edd14edb5b4203d46349f39c1b75dc3 --- M pwb.py 1 file changed, 3 insertions(+), 30 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pwb.py b/pwb.py index 369e789..80c9aac 100755 --- a/pwb.py +++ b/pwb.py @@ -46,30 +46,6 @@ pwb = None
-def remove_modules(): - """Remove pywikibot modules.""" - for name in list(sys.modules): - if name.startswith('pywikibot'): - del sys.modules[name] - - -def tryimport_pwb(): - """Try to import pywikibot. - - If so, we need to patch pwb.argvu, too. - If pywikibot is not available, we create a mock object to remove the - need for if statements further on. - """ - global pwb - try: - import pywikibot - except RuntimeError: - remove_modules() - os.environ['PYWIKIBOT_NO_USER_CONFIG'] = '2' - import pywikibot - pwb = pywikibot - - # The following snippet was developed by Ned Batchelder (and others) # for coverage [1], with python 3 support [2] added later, # and is available under the BSD license (see [3]) @@ -87,8 +63,6 @@ `args` is the argument array to present as sys.argv, as unicode strings.
""" - tryimport_pwb() - # Create a module to serve as __main__ old_main_mod = sys.modules['__main__'] # it's explicitly using str() to bypass unicode_literals in Python 2 @@ -174,8 +148,10 @@ _pwb_dir = str(_pwb_dir) os.environ['PYWIKIBOT_DIR_PWB'] = _pwb_dir try: - import pywikibot + import pywikibot as pwb except RuntimeError: + os.environ['PYWIKIBOT_NO_USER_CONFIG'] = '2' + import pywikibot as pwb # user-config.py to be created if filename is not None and not (filename.startswith('generate_') or filename == 'version.py'): @@ -188,8 +164,6 @@ # 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.') sys.exit(1) -else: - pwb = pywikibot
def main(): @@ -197,7 +171,6 @@ global filename if filename: file_package = None - tryimport_pwb() argvu = pwb.argvu[1:] if not os.path.exists(filename): script_paths = ['scripts',
pywikibot-commits@lists.wikimedia.org