jenkins-bot merged this change.

View Change

Approvals: Dalba: Looks good to me, approved jenkins-bot: Verified
[IMPR] Use importlib.import_module instead of __import__

importlib.import_module was backported to Python 2.7
After dropping Python 2.6 __import__ is not needed here.

Change-Id: I94b66307636de31874111aa7a2d3de3aeaa783c8
---
M pwb.py
M pywikibot/bot.py
M pywikibot/tools/__init__.py
M pywikibot/version.py
M scripts/maintenance/make_i18n_dict.py
M tests/tools_tests.py
6 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/pwb.py b/pwb.py
index d6211ef..b8db45b 100755
--- a/pwb.py
+++ b/pwb.py
@@ -16,6 +16,7 @@
from __future__ import (absolute_import, division,
print_function, unicode_literals)

+from importlib import import_module
import os
import sys
import types
@@ -242,7 +243,7 @@

if file_package and file_package not in sys.modules:
try:
- __import__(file_package)
+ import_module(file_package)
except ImportError as e:
warn('Parent module %s not found: %s'
% (file_package, e), ImportWarning)
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index ae1e335..6b11443 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -81,6 +81,7 @@

import codecs
import datetime
+from importlib import import_module
import json
import logging
import logging.handlers
@@ -1035,7 +1036,7 @@

global_help = _GLOBAL_HELP % module_name
try:
- module = __import__('%s' % module_name)
+ module = import_module('%s' % module_name)
help_text = module.__doc__
if PY2 and isinstance(help_text, bytes):
help_text = help_text.decode('utf-8')
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 0b9095d..80b7e07 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -10,6 +10,7 @@
import collections
import gzip
import hashlib
+from importlib import import_module
import inspect
import itertools
import os
@@ -374,7 +375,7 @@
def has_module(module):
"""Check whether a module can be imported."""
try:
- __import__(module)
+ import_module(module)
except ImportError:
return False
else:
@@ -1974,7 +1975,7 @@
elif '.' in self._deprecated[attr][0]:
try:
package_name = self._deprecated[attr][0].split('.', 1)[0]
- module = __import__(package_name)
+ module = import_module(package_name)
context = {package_name: module}
replacement = eval(self._deprecated[attr][0], context)
self._deprecated[attr] = (
diff --git a/pywikibot/version.py b/pywikibot/version.py
index b910310..3ec659d 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -3,7 +3,7 @@
#
# (C) Merlijn 'valhallasw' van Deen, 2007-2014
# (C) xqt, 2010-2018
-# (C) Pywikibot team, 2007-2018
+# (C) Pywikibot team, 2007-2019
#
# Distributed under the terms of the MIT license.
#
@@ -11,6 +11,7 @@

import codecs
import datetime
+from importlib import import_module
import json
import os
import socket
@@ -515,8 +516,8 @@

for name in root_packages:
try:
- package = __import__(name, level=0)
- except Exception as e:
+ package = import_module(name)
+ except ImportError as e:
data[name] = {'name': name, 'err': e}
continue

diff --git a/scripts/maintenance/make_i18n_dict.py b/scripts/maintenance/make_i18n_dict.py
index 49194ed..22087d2 100755
--- a/scripts/maintenance/make_i18n_dict.py
+++ b/scripts/maintenance/make_i18n_dict.py
@@ -25,8 +25,8 @@
>>> bot.to_json()
"""
#
-# (C) xqt, 2013-2018
-# (C) Pywikibot team, 2013-2018
+# (C) xqt, 2013-2019
+# (C) Pywikibot team, 2013-2019
#
# Distributed under the terms of the MIT license.
#
@@ -34,6 +34,7 @@
print_function, unicode_literals)

import codecs
+from importlib import import_module
import json
import os

@@ -48,7 +49,7 @@
"""Initializer."""
modules = script.split('.')
self.scriptname = modules[0]
- self.script = __import__('scripts.' + self.scriptname)
+ self.script = import_module('scripts.' + self.scriptname)
for m in modules:
self.script = getattr(self.script, m)
self.messages = {}
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index ce8722b..c474bd2 100644
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
"""Test tools package alone which don't fit into other tests."""
#
-# (C) Pywikibot team, 2015-2018
+# (C) Pywikibot team, 2015-2019
#
# Distributed under the terms of the MIT license.
from __future__ import absolute_import, division, unicode_literals
@@ -13,6 +13,7 @@
from collections import Mapping
from collections import OrderedDict
import decimal
+from importlib import import_module
import inspect
import os.path
import subprocess
@@ -125,7 +126,7 @@
"""Test open_archive when bz2file library."""
old_bz2 = tools.bz2
try:
- tools.bz2 = __import__('bz2file')
+ tools.bz2 = import_module('bz2file')
self.assertEqual(self._get_content(self.base_file + '.bz2'),
self.original_content)
self.assertEqual(self._get_content(self.base_file + '.bz2',

To view, visit change 491216. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I94b66307636de31874111aa7a2d3de3aeaa783c8
Gerrit-Change-Number: 491216
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: jenkins-bot (75)