Patchwork [3,of,4] demandimport: drop Py3 workarounds from Py2 implementation

login
register
mail settings
Submitter Yuya Nishihara
Date July 16, 2017, 2:41 p.m.
Message ID <f74cef39afa518f5b7a3.1500216110@mimosa>
Download mbox | patch
Permalink /patch/22440/
State Accepted
Headers show

Comments

Yuya Nishihara - July 16, 2017, 2:41 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1500193162 -32400
#      Sun Jul 16 17:19:22 2017 +0900
# Node ID f74cef39afa518f5b7a3e5770b9988d24f99fc24
# Parent  10093dbe4b0785678511fc4e7d3088746019742c
demandimport: drop Py3 workarounds from Py2 implementation

Patch

diff --git a/hgdemandimport/demandimportpy2.py b/hgdemandimport/demandimportpy2.py
--- a/hgdemandimport/demandimportpy2.py
+++ b/hgdemandimport/demandimportpy2.py
@@ -26,27 +26,17 @@  These imports will not be delayed:
 
 from __future__ import absolute_import
 
+import __builtin__ as builtins
 import contextlib
 import os
 import sys
 
-# __builtin__ in Python 2, builtins in Python 3.
-try:
-    import __builtin__ as builtins
-except ImportError:
-    import builtins
-
 contextmanager = contextlib.contextmanager
 
 _origimport = __import__
 
 nothing = object()
 
-# Python 3 doesn't have relative imports nor level -1.
-level = -1
-if sys.version_info[0] >= 3:
-    level = 0
-
 def _hgextimport(importfunc, name, globals, *args, **kwargs):
     try:
         return importfunc(name, globals, *args, **kwargs)
@@ -172,7 +162,7 @@  class _demandmod(object):
 
 _pypy = '__pypy__' in sys.builtin_module_names
 
-def _demandimport(name, globals=None, locals=None, fromlist=None, level=level):
+def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
     if locals is None or name in ignore or fromlist == ('*',):
         # these cases we can't really delay
         return _hgextimport(_origimport, name, globals, locals, fromlist, level)
diff --git a/tests/test-check-py3-compat.t b/tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t
+++ b/tests/test-check-py3-compat.t
@@ -3,8 +3,9 @@ 
   $ . "$TESTDIR/helpers-testrepo.sh"
   $ cd "$TESTDIR"/..
 
-  $ testrepohg files 'set:(**.py)' | sed 's|\\|/|g' \
-  > | xargs $PYTHON contrib/check-py3-compat.py
+  $ testrepohg files 'set:(**.py)' \
+  > -X hgdemandimport/demandimportpy2.py \
+  > | sed 's|\\|/|g' | xargs $PYTHON contrib/check-py3-compat.py
   contrib/python-zstandard/setup.py not using absolute_import
   contrib/python-zstandard/setup_zstd.py not using absolute_import
   contrib/python-zstandard/tests/common.py not using absolute_import
@@ -24,6 +25,7 @@ 
 
 #if py3exe
   $ testrepohg files 'set:(**.py) - grep(pygments)' \
+  > -X hgdemandimport/demandimportpy2.py \
   > -X hgext/fsmonitor/pywatchman \
   > | sed 's|\\|/|g' | xargs $PYTHON3 contrib/check-py3-compat.py \
   > | sed 's/[0-9][0-9]*)$/*)/'