[PATCH] tests: fix import-checker relative test on python 2.6

Durham Goode durham at fb.com
Tue Jun 21 06:45:35 UTC 2016


# HG changeset patch
# User Durham Goode <durham at fb.com>
# Date 1466491380 25200
#      Mon Jun 20 23:43:00 2016 -0700
# Node ID e735bd6f9b4b801538b5958c3cb298bb4934790f
# Parent  4e6e280e238fa885df9e0e600dc5915a71c8a37a
tests: fix import-checker relative test on python 2.6

Commit 1f88c0f6ff5a introduced a None check in fromlocal to detect relative
imports. It assumed the ast returned None for a relative import. This is true
for Python 2.7, but Python 2.6 actually returns an empty string instead. This
went unnoticed until cba8bc11ed, which changed the logic around late importers
and started the import checker test failing in Python 2.6.

The fix is to just check for an empty string as well. I verified this fix by
running the module import test and verifying that every invocation of
`fromlocal` that was None in 2.7 was '' in 2.6.

diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -127,6 +127,8 @@ def fromlocalfunc(modulename, localmods)
     False
     >>> fromlocal(None, 1)
     ('foo', 'foo.__init__', True)
+    >>> fromlocal('', 1)
+    ('foo', 'foo.__init__', True)
     >>> fromlocal('foo1', 1)
     ('foo.foo1', 'foo.foo1', False)
     >>> fromlocal2 = fromlocalfunc('foo.xxx.yyy', localmods)
@@ -141,8 +143,8 @@ def fromlocalfunc(modulename, localmods)
     if prefix:
         prefix += '.'
     def fromlocal(name, level=0):
-        # name is None when relative imports are used.
-        if name is None:
+        # name is None or empty when relative imports are used.
+        if name is None or name == '':
             # If relative imports are used, level must not be absolute.
             assert level > 0
             candidates = ['.'.join(modulename.split('.')[:-level])]


More information about the Mercurial-devel mailing list