Merge branch 'mromberg-relative'
* mromberg-relative: Python: Do not import all of sys when using -relativeimport Add missing print statements to the Python import_packages tests Update -relative import documentation to reflect runtime check. > to >= Make the check for python3 -relative does python runtime check.
This commit is contained in:
commit
98e2132c0b
5 changed files with 42 additions and 22 deletions
|
|
@ -5680,8 +5680,9 @@ class M2(pkg2.mod3.M3): pass
|
|||
<p>By default, SWIG would generate <tt>mod2.py</tt> proxy file with
|
||||
<tt>import</tt> directive as in point 1. This can be changed with the
|
||||
<tt>-relativeimport</tt> command line option. The <tt>-relativeimport</tt> instructs
|
||||
SWIG to organize imports as in point 2 (for Python 2.x) or as in point 4 (for
|
||||
Python 3, that is when the -py3 command line option is enabled). In short, if you have
|
||||
SWIG to organize imports as in point 2 (for Python < 2.7.0) or as in point 4
|
||||
for Python 2.7.0 and newer. This is a check done at the time the module is
|
||||
imported. In short, if you have
|
||||
<tt>mod2.i</tt> and <tt>mod3.i</tt> as above, then without
|
||||
<tt>-relativeimport</tt> SWIG will write</p>
|
||||
|
||||
|
|
@ -5695,22 +5696,17 @@ import pkg1.pkg2.mod3
|
|||
write</p>
|
||||
|
||||
<div class="targetlang">
|
||||
<pre>
|
||||
import pkg2.mod3
|
||||
<pre>
|
||||
from sys import version_info
|
||||
if version_info >= (2, 7, 0):
|
||||
from . import pkg2
|
||||
import pkg1.pkg2.mod3
|
||||
else:
|
||||
import pkg2.mod3
|
||||
del version_info
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>if <tt>-py3</tt> is not used, or</p>
|
||||
|
||||
<div class="targetlang">
|
||||
<pre>
|
||||
from . import pkg2
|
||||
import pkg1.pkg2.mod3
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>when <tt>-py3</tt> is used.</p>
|
||||
|
||||
<p>You should avoid using relative imports and use absolute ones whenever
|
||||
possible. There are some cases, however, when relative imports may be
|
||||
necessary. The first example is, when some (legacy) Python code refers entities
|
||||
|
|
|
|||
|
|
@ -1,8 +1,17 @@
|
|||
# These examples rely on namespace packages. Don't
|
||||
# run them for old python interpreters.
|
||||
import sys
|
||||
import os.path
|
||||
|
||||
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
|
||||
print "Testing " + testname + " - namespace packages"
|
||||
|
||||
if sys.version_info < (3, 3, 0):
|
||||
print " Not importing nstest as Python version is < 3.3"
|
||||
sys.exit(0)
|
||||
|
||||
import nstest
|
||||
|
||||
print " Finished importing nstest"
|
||||
|
||||
nstest.main()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,10 @@
|
|||
import os.path
|
||||
|
||||
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
|
||||
print "Testing " + testname + " - split modules"
|
||||
|
||||
import pkg1.foo
|
||||
|
||||
print " Finished importing pkg1.foo"
|
||||
|
||||
assert(pkg1.foo.count() == 3)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,10 @@
|
|||
import os.path
|
||||
|
||||
testname = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
|
||||
print "Testing " + testname + " - split modules"
|
||||
|
||||
import pkg1.foo
|
||||
|
||||
print " Finished importing pkg1.foo"
|
||||
|
||||
assert(pkg1.foo.count() == 3)
|
||||
|
|
|
|||
|
|
@ -1251,13 +1251,14 @@ public:
|
|||
Printf(out, "import %s%s%s%s\n", apkg, *Char(apkg) ? "." : "", pfx, mod);
|
||||
Delete(apkg);
|
||||
} else {
|
||||
if (py3) {
|
||||
if (py3_rlen1)
|
||||
Printf(out, "from . import %.*s\n", py3_rlen1, rpkg);
|
||||
Printf(out, "from .%s import %s%s\n", rpkg, pfx, mod);
|
||||
} else {
|
||||
Printf(out, "import %s%s%s%s\n", rpkg, *Char(rpkg) ? "." : "", pfx, mod);
|
||||
}
|
||||
Printf(out, "from sys import version_info\n");
|
||||
Printf(out, "if version_info >= (2, 7, 0):\n");
|
||||
if (py3_rlen1)
|
||||
Printf(out, tab4 "from . import %.*s\n", py3_rlen1, rpkg);
|
||||
Printf(out, tab4 "from .%s import %s%s\n", rpkg, pfx, mod);
|
||||
Printf(out, "else:\n");
|
||||
Printf(out, tab4 "import %s%s%s%s\n", rpkg, *Char(rpkg) ? "." : "", pfx, mod);
|
||||
Printf(out, "del version_info\n");
|
||||
Delete(rpkg);
|
||||
}
|
||||
return out;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue