comparison contrib/check-py3-compat.py @ 30094:f701fffd21d8

py3: make check-py3-compat.py load modules in standard manner Otherwise no code transformation would be applied to the modules which are imported only by imp.load_module(). This change means modules are imported from PYTHONPATH, not from the paths given by command arguments. This isn't always correct, but seems acceptable.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 08 Oct 2016 17:22:07 +0200
parents 68010ed1636d
children e8aeeb28e35e
comparison
equal deleted inserted replaced
30093:68010ed1636d 30094:f701fffd21d8
8 # GNU General Public License version 2 or any later version. 8 # GNU General Public License version 2 or any later version.
9 9
10 from __future__ import absolute_import, print_function 10 from __future__ import absolute_import, print_function
11 11
12 import ast 12 import ast
13 import imp 13 import importlib
14 import os 14 import os
15 import sys 15 import sys
16 import traceback 16 import traceback
17 17
18 def check_compat_py2(f): 18 def check_compat_py2(f):
54 # For now we only support mercurial.* and hgext.* modules because figuring 54 # For now we only support mercurial.* and hgext.* modules because figuring
55 # out module paths for things not in a package can be confusing. 55 # out module paths for things not in a package can be confusing.
56 if f.startswith(('hgext/', 'mercurial/')) and not f.endswith('__init__.py'): 56 if f.startswith(('hgext/', 'mercurial/')) and not f.endswith('__init__.py'):
57 assert f.endswith('.py') 57 assert f.endswith('.py')
58 name = f.replace('/', '.')[:-3].replace('.pure.', '.') 58 name = f.replace('/', '.')[:-3].replace('.pure.', '.')
59 with open(f, 'r') as fh: 59 if True:
60 try: 60 try:
61 imp.load_module(name, fh, f, ('py', 'r', imp.PY_SOURCE)) 61 importlib.import_module(name)
62 except Exception as e: 62 except Exception as e:
63 exc_type, exc_value, tb = sys.exc_info() 63 exc_type, exc_value, tb = sys.exc_info()
64 # We walk the stack and ignore frames from our custom importer, 64 # We walk the stack and ignore frames from our custom importer,
65 # import mechanisms, and stdlib modules. This kinda/sorta 65 # import mechanisms, and stdlib modules. This kinda/sorta
66 # emulates CPython behavior in import.c while also attempting 66 # emulates CPython behavior in import.c while also attempting