John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/296411

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
(manually cherry picked from commit 65cabc8258aa807a754a62182ae51fbf7df45ddd)
---
M pywikibot/botirc.py
M pywikibot/diff.py
M scripts/flickrripper.py
3 files changed, 7 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/11/296411/1

diff --git a/pywikibot/botirc.py b/pywikibot/botirc.py
index 77d258a..3294d76 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 08f939e..ee41492 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
 
@@ -570,9 +565,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 38e97df..568b5f2 100755
--- a/scripts/flickrripper.py
+++ b/scripts/flickrripper.py
@@ -62,7 +62,7 @@
 try:
     from pywikibot.userinterfaces.gui import Tkdialog
 except ImportError as _tk_error:
-    Tkdialog = None
+    Tkdialog = _tk_error
 
 
 flickr_allowed_license = {
@@ -295,7 +295,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()
@@ -305,7 +305,7 @@
                     autonomous = True
             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

-- 
To view, visit https://gerrit.wikimedia.org/r/296411
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I80f82e7f0674bfca70688f28640620dacac1d80b
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: 2.0
Gerrit-Owner: John Vandenberg <[email protected]>
Gerrit-Reviewer: AbdealiJK <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to