diff tests/test-extension.t @ 19932:e3a5922e18c3

demandimport: support "absolute_import" for external libraries (issue4029) Before this patch, demandimport of Mercurial may fail to load external libraries using "from __future__ import absolute_import": for example, importing "foo" in "bar.baz" module will load "bar.foo" if it exists, even though "absolute_import" is enabled in "bar.baz" module. So, extensions for Mercurial can't use such external libraries. This patch saves "level" of import request for on-demand module loading in the future: default value of level is -1, and level is 0 when "absolute_import" is enabled. "level" value is passed to built-in import function in "_demandmod._load()" and it should load target module correctly. This patch changes only one "_demandmod" construction case other than cases below: - construction in "_demandmod._load()" this code path should be used only in relative sub-module loading case - constructions other than patched one in"_demandimport()" these code paths shouldn't be used in "level != -1" case
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Sat, 05 Oct 2013 01:02:22 +0900
parents a194a33f8cb2
children 621a26eb3a99
line wrap: on
line diff
--- a/tests/test-extension.t	Sat Oct 05 01:02:22 2013 +0900
+++ b/tests/test-extension.t	Sat Oct 05 01:02:22 2013 +0900
@@ -129,6 +129,45 @@
   $ echo 'foo = !' >> $HGRCPATH
   $ echo 'bar = !' >> $HGRCPATH
 
+Check "from __future__ import absolute_import" support for external libraries
+
+  $ mkdir $TESTTMP/libroot
+  $ echo "s = 'libroot/ambig.py'" > $TESTTMP/libroot/ambig.py
+  $ mkdir $TESTTMP/libroot/mod
+  $ touch $TESTTMP/libroot/mod/__init__.py
+  $ echo "s = 'libroot/mod/ambig.py'" > $TESTTMP/libroot/mod/ambig.py
+
+#if absimport
+  $ cat > $TESTTMP/libroot/mod/ambigabs.py <<EOF
+  > from __future__ import absolute_import
+  > import ambig # should load "libroot/ambig.py"
+  > s = ambig.s
+  > EOF
+  $ cat > loadabs.py <<EOF
+  > import mod.ambigabs as ambigabs
+  > def extsetup():
+  >     print 'ambigabs.s=%s' % ambigabs.s
+  > EOF
+  $ (PYTHONPATH=$PYTHONPATH:$TESTTMP/libroot; hg --config extensions.loadabs=loadabs.py root)
+  ambigabs.s=libroot/ambig.py
+  $TESTTMP/a
+#endif
+
+#if no-py3k
+  $ cat > $TESTTMP/libroot/mod/ambigrel.py <<EOF
+  > import ambig # should load "libroot/mod/ambig.py"
+  > s = ambig.s
+  > EOF
+  $ cat > loadrel.py <<EOF
+  > import mod.ambigrel as ambigrel
+  > def extsetup():
+  >     print 'ambigrel.s=%s' % ambigrel.s
+  > EOF
+  $ (PYTHONPATH=$PYTHONPATH:$TESTTMP/libroot; hg --config extensions.loadrel=loadrel.py root)
+  ambigrel.s=libroot/mod/ambig.py
+  $TESTTMP/a
+#endif
+
   $ cd ..
 
 hide outer repo