jenkins-bot has submitted this change and it was merged.
Change subject: pywikibot: Store ImportError in imported variable ......................................................................
pywikibot: Store ImportError in imported variable
If a lazy ImportError is to be handled, the ImportError needs to be stored in a variable and then used when required. Earlier, the imported variable was set to `None` and the import error which was caught with `except` was used. But this import error may not exist at a later time as python guarantees it's existence only inside the `except` statement. Hence, this is changed so that the ImportError is saved in the variable that was being imported and checked later using `isinstance(<variable>, ImportError)`.
Bug: T134336 Change-Id: I80f82e7f0674bfca70688f28640620dacac1d80b --- M pywikibot/botirc.py M pywikibot/diff.py M scripts/flickrripper.py 3 files changed, 7 insertions(+), 12 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/botirc.py b/pywikibot/botirc.py index b509221..1d9f13a 100644 --- a/pywikibot/botirc.py +++ b/pywikibot/botirc.py @@ -25,13 +25,15 @@ try: from ircbot import SingleServerIRCBot except ImportError as e: + ircbot_import_error = e + class SingleServerIRCBot(object):
"""Fake SingleServerIRCBot."""
def __init__(*args, **kwargs): """Report import exception.""" - raise e + raise ircbot_import_error
_logger = "botirc" diff --git a/pywikibot/diff.py b/pywikibot/diff.py index f83033f..cc55fd9 100644 --- a/pywikibot/diff.py +++ b/pywikibot/diff.py @@ -20,11 +20,6 @@ else: from itertools import izip_longest as zip_longest
-try: - from bs4 import BeautifulSoup -except ImportError as bserror: - BeautifulSoup = False - import pywikibot from pywikibot.tools import chars
@@ -579,9 +574,7 @@ @return: deleted and added list of contexts @rtype: dict """ - # check if BeautifulSoup imported - if not BeautifulSoup: - raise bserror # should have been raised and stored earlier. + from bs4 import BeautifulSoup
comparands = {'deleted-context': [], 'added-context': []} soup = BeautifulSoup(compare_string) diff --git a/scripts/flickrripper.py b/scripts/flickrripper.py index 7d549aa..4dfa9b9 100755 --- a/scripts/flickrripper.py +++ b/scripts/flickrripper.py @@ -64,7 +64,7 @@ try: from pywikibot.userinterfaces.gui import Tkdialog except ImportError as _tk_error: - Tkdialog = None + Tkdialog = _tk_error
flickr_allowed_license = { @@ -304,7 +304,7 @@ override, addCategory, removeCategories) # pywikibot.output(photoDescription) - if Tkdialog is not None and not autonomous: + if not isinstance(Tkdialog, ImportError) and not autonomous: try: (newPhotoDescription, newFilename, skip) = Tkdialog( photoDescription, photo, filename).show_dialog() @@ -315,7 +315,7 @@ elif not autonomous: pywikibot.warning('Switching to autonomous mode because GUI ' 'interface cannot be used') - pywikibot.warning(_tk_error) + pywikibot.warning(Tkdialog) autonomous = True if autonomous: newPhotoDescription = photoDescription
pywikibot-commits@lists.wikimedia.org