jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/611895 )

Change subject: [bugfix] import distutils later in version.py
......................................................................

[bugfix] import distutils later in version.py

- with setuptools 49.2.0 it is discouraged to import distutils before
  Setuptools. Therefore import distutils when needed
- also remove Python 2 related code in version.py

Bug: T257772
Change-Id: I4d0a0cc7a68d7e7d80069d09b8f03b4990e2b8ad
---
M pywikibot/version.py
1 file changed, 22 insertions(+), 33 deletions(-)

Approvals:
  Matěj Suchánek: Looks good to me, but someone else must approve
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/version.py b/pywikibot/version.py
index 0c6423e..2b50f15 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -5,25 +5,27 @@
 #
 # Distributed under the terms of the MIT license.
 #
-from __future__ import absolute_import, division, unicode_literals
-
 import codecs
 import datetime
-from importlib import import_module
 import json
 import os
+import pathlib
 import socket
 import subprocess
 import sys
 import time
 import xml.dom.minidom

-from contextlib import closing
-from distutils import log
-from distutils.sysconfig import get_python_lib
+from contextlib import closing, suppress
+from importlib import import_module
 from io import BytesIO
 from warnings import warn

+import pywikibot
+
+from pywikibot import config2 as config
+from pywikibot.tools import deprecated
+
 try:
     from setuptools import svn_utils
 except ImportError:
@@ -32,17 +34,6 @@
     except ImportError as e:
         svn_utils = e

-try:
-    import pathlib
-except ImportError:
-    import pathlib2 as pathlib  # Python 2
-
-
-import pywikibot
-
-from pywikibot import config2 as config
-from pywikibot.tools import deprecated, PY2, UnicodeType
-
 cache = None
 _logger = 'version'

@@ -79,17 +70,15 @@
     data['cmp_ver'] = 'n/a'

     if online:
-        try:
+        with suppress(Exception):
             hsh3 = getversion_onlinerepo('tags/stable')
             hsh2 = getversion_onlinerepo()
             hsh1 = data['hsh']
             data['cmp_ver'] = 'UNKNOWN' if not hsh1 else (
                 'OUTDATED' if hsh1 not in (hsh2, hsh3) else 'ok')
-        except Exception:
-            pass

     data['hsh'] = data['hsh'][:7]  # make short hash from full hash
-    return '%(tag)s (%(hsh)s, %(rev)s, %(date)s, %(cmp_ver)s)' % data
+    return '{tag} ({hsh}, {rev}, {date}, {cmp_ver})'.format(**data)


 def getversiondict():
@@ -135,7 +124,7 @@
         pywikibot.debug('version algorithm exceptions:\n%r'
                         % exceptions, _logger)

-    if isinstance(date, UnicodeType):
+    if isinstance(date, str):
         datestring = date
     elif isinstance(date, time.struct_time):
         datestring = time.strftime('%Y/%m/%d, %H:%M:%S', date)
@@ -166,14 +155,14 @@
         with open(filename) as entries:
             version = entries.readline().strip()
             if version != '12':
-                for i in range(3):
+                for _ in range(3):
                     entries.readline()
                 tag = entries.readline().strip()
                 t = tag.split('://', 1)
                 t[1] = t[1].replace('svn.wikimedia.org/svnroot/pywikipedia/',
                                     '')
                 tag = '[{0}] {1}'.format(*t)
-                for i in range(4):
+                for _ in range(4):
                     entries.readline()
                 date = time.strptime(entries.readline()[:19],
                                      '%Y-%m-%dT%H:%M:%S')
@@ -235,6 +224,8 @@
     """
     if isinstance(svn_utils, Exception):
         raise svn_utils
+
+    from distutils import log  # T257772
     tag = 'pywikibot-core'
     _program_dir = path or _get_program_dir()
     svninfo = svn_utils.SvnInfo(_program_dir)
@@ -417,10 +408,8 @@
         with codecs.open(fn, 'r', 'utf-8') as f:
             for line in f.readlines():
                 if line.find('__version__') == 0:
-                    try:
+                    with suppress(Exception):
                         exec(line)
-                    except Exception:
-                        pass
                     break
         stat = os.stat(fn)
         mtime = datetime.datetime.fromtimestamp(stat.st_mtime).isoformat(' ')
@@ -442,6 +431,7 @@
     """
     if hasattr(module, '__version__'):
         return module.__version__[5:-1]
+    return None


 def get_module_filename(module):
@@ -459,13 +449,12 @@
     """
     if hasattr(module, '__file__') and os.path.exists(module.__file__):
         filename = module.__file__
-        if PY2:
-            filename = os.path.abspath(filename)
         if filename[-4:-1] == '.py' and os.path.exists(filename[:-1]):
             filename = filename[:-1]
         program_dir = _get_program_dir()
         if filename[:len(program_dir)] == program_dir:
             return filename
+    return None


 def get_module_mtime(module):
@@ -480,6 +469,7 @@
     filename = get_module_filename(module)
     if filename:
         return datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
+    return None


 def package_versions(modules=None, builtins=False, standard_lib=None):
@@ -495,6 +485,8 @@
     @param standard_lib: Include standard library packages
     @type standard_lib: Boolean, or None for automatic selection
     """
+    from distutils.sysconfig import get_python_lib  # T257772
+
     if not modules:
         modules = sys.modules.keys()

@@ -508,7 +500,7 @@

     # Improve performance by removing builtins from the list if possible.
     if builtins is False:
-        root_packages = list(root_packages - builtin_packages)
+        root_packages = root_packages - builtin_packages

     std_lib_packages = []

@@ -541,9 +533,6 @@
             if '__init__.py' in path:
                 path = path[0:path.index('__init__.py')]

-            if PY2:
-                path = path.decode(sys.getfilesystemencoding())
-
             info['path'] = path
             assert path not in paths, 'Path of the package is in defined paths'
             paths[path] = name

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/611895
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I4d0a0cc7a68d7e7d80069d09b8f03b4990e2b8ad
Gerrit-Change-Number: 611895
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: Zhuyifei1999 <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: JJMC89 <[email protected]>
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to