AbdealiJK has uploaded a new change for review.
https://gerrit.wikimedia.org/r/277207
Change subject: pywikibot.tools: Raise error if bz2 not found
......................................................................
pywikibot.tools: Raise error if bz2 not found
bz2 is required in pywikibot to open compressed files. But,
sometimes python is not compiled with bz2. This causes pywikibot
to fail while it is being imported.
This commit shows a warning in such cases and tries to use
bz2file as an alternative. If both are not found, it raises an
error lazily (i.e. only when bz2 is needed to open a compressed
file).
Bug T123092
Change-Id: Iace28a5b356cff19118983fd089770c75a1b4dff
---
M pywikibot/tools/__init__.py
1 file changed, 11 insertions(+), 1 deletion(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/07/277207/1
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index c2c5bba..5f62a92 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -8,7 +8,6 @@
from __future__ import absolute_import, print_function, unicode_literals
__version__ = '$Id$'
-import bz2
import collections
import gzip
import inspect
@@ -38,6 +37,15 @@
from pywikibot.logging import debug
+try:
+ import bz2
+except ImportError as bz2_import_error:
+ try:
+ import bz2file as bz2
+ warn('package bz2 was not found; using bz2file', ImportWarning)
+ except ImportError:
+ warn('package bz2 and bz2file were not found', ImportWarning)
+ bz2 = False
if PYTHON_VERSION < (3, 5):
# although deprecated in 3 completely no message was emitted until 3.5
@@ -988,6 +996,8 @@
extension = ''
if extension == 'bz2':
+ if not bz2:
+ raise bz2_import_error
return wrap(bz2.BZ2File(filename, mode), 1)
elif extension == 'gz':
return wrap(gzip.open(filename, mode), 0)
--
To view, visit https://gerrit.wikimedia.org/r/277207
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iace28a5b356cff19118983fd089770c75a1b4dff
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: AbdealiJK <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits