From 9ce4b58df41c061d62a3b35a4c57cc33c954ee6d Mon Sep 17 00:00:00 2001 From: Mike Romberg Date: Mon, 6 Jun 2016 01:28:40 -0600 Subject: [PATCH 1/5] Make the check for python3 -relative does python runtime check. --- Source/Modules/python.cxx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 22703bb72..92dd35c5f 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -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, "import sys\n"); + Printf(out, "if sys.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); Delete(rpkg); } return out; From d55151ac705ac0c44fff326f32b22db999638321 Mon Sep 17 00:00:00 2001 From: Mike Romberg Date: Mon, 6 Jun 2016 13:26:55 -0600 Subject: [PATCH 2/5] > to >= --- Source/Modules/python.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 92dd35c5f..be7af9171 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -1252,7 +1252,7 @@ public: Delete(apkg); } else { Printf(out, "import sys\n"); - Printf(out, "if sys.version_info > (2, 7, 0):\n"); + Printf(out, "if sys.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); From ac7157dfc620471cee3376cfc3f8312dff48d32c Mon Sep 17 00:00:00 2001 From: Mike Romberg Date: Mon, 6 Jun 2016 13:43:58 -0600 Subject: [PATCH 3/5] Update -relative import documentation to reflect runtime check. --- Doc/Manual/Python.html | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html index 34870a285..e6c23bc8c 100644 --- a/Doc/Manual/Python.html +++ b/Doc/Manual/Python.html @@ -5680,8 +5680,9 @@ class M2(pkg2.mod3.M3): pass

By default, SWIG would generate mod2.py proxy file with import directive as in point 1. This can be changed with the -relativeimport command line option. The -relativeimport 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 mod2.i and mod3.i as above, then without -relativeimport SWIG will write

@@ -5695,22 +5696,16 @@ import pkg1.pkg2.mod3 write

-
-import pkg2.mod3
+  
+import sys
+if sys.version_info >= (2, 7, 0):
+    from . import pkg2
+    import pkg1.pkg2.mod3
+else:
+    import pkg2.mod3
 
-

if -py3 is not used, or

- -
-
-from . import pkg2
-import pkg1.pkg2.mod3
-
-
- -

when -py3 is used.

-

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 From dd40a25349dadc4011040474334c1fb7be3abecf Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 6 Jun 2016 22:12:56 +0100 Subject: [PATCH 4/5] Add missing print statements to the Python import_packages tests --- Examples/python/import_packages/namespace_pkg/runme.py | 9 +++++++++ .../import_packages/split_modules/vanilla/runme.py | 7 +++++++ .../import_packages/split_modules/vanilla_split/runme.py | 7 +++++++ 3 files changed, 23 insertions(+) diff --git a/Examples/python/import_packages/namespace_pkg/runme.py b/Examples/python/import_packages/namespace_pkg/runme.py index 4d0aa755c..9c22d36fb 100644 --- a/Examples/python/import_packages/namespace_pkg/runme.py +++ b/Examples/python/import_packages/namespace_pkg/runme.py @@ -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() diff --git a/Examples/python/import_packages/split_modules/vanilla/runme.py b/Examples/python/import_packages/split_modules/vanilla/runme.py index 8a6055bf1..a188364f1 100644 --- a/Examples/python/import_packages/split_modules/vanilla/runme.py +++ b/Examples/python/import_packages/split_modules/vanilla/runme.py @@ -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) diff --git a/Examples/python/import_packages/split_modules/vanilla_split/runme.py b/Examples/python/import_packages/split_modules/vanilla_split/runme.py index 8a6055bf1..a188364f1 100644 --- a/Examples/python/import_packages/split_modules/vanilla_split/runme.py +++ b/Examples/python/import_packages/split_modules/vanilla_split/runme.py @@ -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) From 81adedd7dd3433d600a584337dcff2335dc12e9b Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 6 Jun 2016 22:20:07 +0100 Subject: [PATCH 5/5] Python: Do not import all of sys when using -relativeimport --- Doc/Manual/Python.html | 5 +++-- Source/Modules/python.cxx | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html index e6c23bc8c..83baa1a47 100644 --- a/Doc/Manual/Python.html +++ b/Doc/Manual/Python.html @@ -5697,12 +5697,13 @@ write

-import sys
-if sys.version_info >= (2, 7, 0):
+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
 
diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index be7af9171..a7e76cb0f 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -1251,14 +1251,14 @@ public: Printf(out, "import %s%s%s%s\n", apkg, *Char(apkg) ? "." : "", pfx, mod); Delete(apkg); } else { - Printf(out, "import sys\n"); - Printf(out, "if sys.version_info >= (2, 7, 0):\n"); + 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 . 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, tab4 "import %s%s%s%s\n", rpkg, *Char(rpkg) ? "." : "", pfx, mod); + Printf(out, "del version_info\n"); Delete(rpkg); } return out;