diff --git a/Examples/python/import_packages/Makefile b/Examples/python/import_packages/Makefile index 72b424a90..0d1a1848e 100644 --- a/Examples/python/import_packages/Makefile +++ b/Examples/python/import_packages/Makefile @@ -10,7 +10,8 @@ import_packages_subdirs = \ from_init3 \ relativeimport1 \ relativeimport2 \ - relativeimport3 + relativeimport3 \ + namespace_pkg check: build if test "x$(SRCDIR)" != x; then \ diff --git a/Examples/python/import_packages/README b/Examples/python/import_packages/README index 69fe3516e..187a5d64e 100644 --- a/Examples/python/import_packages/README +++ b/Examples/python/import_packages/README @@ -1,2 +1,4 @@ These are actually regression tests for SF bug #1297 (GH issue #7). -See individual READMEs in subdirectories. +The namespace_pkg is an example of python3's namespace packages. + +See individual READMEs in subdirectories. \ No newline at end of file diff --git a/Examples/python/import_packages/namespace_pkg/Makefile b/Examples/python/import_packages/namespace_pkg/Makefile new file mode 100644 index 000000000..d499fc969 --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/Makefile @@ -0,0 +1,20 @@ +TOP = ../../.. +PY3 = 1 +SWIGEXE = $(TOP)/../swig +SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib +SRCS = robin.c +TARGET = robin +INTERFACE = robin.i + +check: build + $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' PY3='$(PY3)' python_run + +build: + $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \ + PY3='$(PY3)' \ + SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python + +clean: + $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' python_clean + rm -rf path1 path2 path3 path4.zip diff --git a/Examples/python/import_packages/namespace_pkg/README b/Examples/python/import_packages/namespace_pkg/README new file mode 100644 index 000000000..8f7297a51 --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/README @@ -0,0 +1,25 @@ + This is an example (and test) of using swig generated modules in the context +of python3's namespace packages: + +https://www.python.org/dev/peps/pep-0420/ + + Consequently, this example requires python (3.3 or newer) to build and run. + + This example creates a simple swig module named robin. The robin.py module +has a companion C module named _robin.so. The robin module is tested in four +ways: + + 1) As a non-package module (tested by nonpkg.py) + + 2) With robin.py and _robin.so in the brave package under the path1 + subdirectory. (tested by normal.py) + + 3) With robin.py in path2/brave and _robin.so in path3/brave + (tested by split.py) + + 4) With robin.py contained in a zip file (path4.zip) as brave/robin.py and + _robin.so found on the filesystem under path3/brave (tested by zipsplit.py) + +Note: Using namespace packages with subpackages (such as brave.sir.robin) where + robin.py is located in a zipfile requires python-3.5.1 or newer as + python's zipimporter only worked with packages of depth 1 until then. \ No newline at end of file diff --git a/Examples/python/import_packages/namespace_pkg/nonpkg.py b/Examples/python/import_packages/namespace_pkg/nonpkg.py new file mode 100644 index 000000000..acf0aedbd --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/nonpkg.py @@ -0,0 +1,5 @@ +# import robin as a module in the global namespace + +import robin + +assert(robin.run() == "AWAY!") diff --git a/Examples/python/import_packages/namespace_pkg/normal.py b/Examples/python/import_packages/namespace_pkg/normal.py new file mode 100644 index 000000000..fc26c0216 --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/normal.py @@ -0,0 +1,7 @@ +import sys +# Package brave found under one path +sys.path.insert(0, 'path1') + +from brave import robin + +assert(robin.run() == "AWAY!") diff --git a/Examples/python/import_packages/namespace_pkg/robin.c b/Examples/python/import_packages/namespace_pkg/robin.c new file mode 100644 index 000000000..8b85a6213 --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/robin.c @@ -0,0 +1,3 @@ +const char *run(void) { + return "AWAY!"; +} diff --git a/Examples/python/import_packages/namespace_pkg/robin.i b/Examples/python/import_packages/namespace_pkg/robin.i new file mode 100644 index 000000000..2f581c686 --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/robin.i @@ -0,0 +1,7 @@ +%module robin + +%{ +extern const char *run(void); +%} + +const char *run(void); diff --git a/Examples/python/import_packages/namespace_pkg/runme.py b/Examples/python/import_packages/namespace_pkg/runme.py new file mode 100644 index 000000000..a8ae6413e --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/runme.py @@ -0,0 +1,9 @@ +import os, sys, setupPkg + +setupPkg.copyMods() + +# Run each test with a seperate interpreter +os.system(sys.executable + " nonpkg.py") +os.system(sys.executable + " normal.py") +os.system(sys.executable + " split.py") +os.system(sys.executable + " zipsplit.py") diff --git a/Examples/python/import_packages/namespace_pkg/setupPkg.py b/Examples/python/import_packages/namespace_pkg/setupPkg.py new file mode 100644 index 000000000..1ebb9399d --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/setupPkg.py @@ -0,0 +1,34 @@ +#--------------------------------- +# setup the namespace package dirs +#--------------------------------- +import os, shutil, zipfile + + +def copyMods(): + dirs = ['path1', 'path2', 'path3'] + + # Clean out any old package paths + for d in dirs: + if os.path.isdir(d): + shutil.rmtree(d) + + for d in dirs: + os.mkdir(d) + os.mkdir(os.path.join(d, 'brave')) + + shutil.copy('robin.py', os.path.join('path1', 'brave')) + shutil.copy('_robin.so', os.path.join('path1', 'brave')) + + shutil.copy('robin.py', os.path.join('path2', 'brave')) + shutil.copy('_robin.so', os.path.join('path3', 'brave')) + + mkzip() + +def mkzip(): + zf = zipfile.ZipFile("path4.zip", "w") + zf.writestr("brave/", b'') + zf.write('robin.py', 'brave/robin.py') + zf.close() + +if __name__ == "__main__": + copyMods() diff --git a/Examples/python/import_packages/namespace_pkg/split.py b/Examples/python/import_packages/namespace_pkg/split.py new file mode 100644 index 000000000..1b66c2d49 --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/split.py @@ -0,0 +1,9 @@ +import sys +# Package brave split into two paths. +# path2/brave/robin.py and path3/brave/_robin.so +sys.path.insert(0, 'path2') +sys.path.insert(0, 'path3') + +from brave import robin + +assert(robin.run() == "AWAY!") diff --git a/Examples/python/import_packages/namespace_pkg/zipsplit.py b/Examples/python/import_packages/namespace_pkg/zipsplit.py new file mode 100644 index 000000000..9e35559e3 --- /dev/null +++ b/Examples/python/import_packages/namespace_pkg/zipsplit.py @@ -0,0 +1,9 @@ +import sys +# Package brave split into two paths. +# brave/robin.py (in path4.zip) and path3/brave/_robin.so +sys.path.insert(0, 'path4.zip') +sys.path.insert(0, 'path3') + +from brave import robin + +assert(robin.run() == "AWAY!")