Merge branch 'mromberg-impfix'

* mromberg-impfix:
  Examples readme file tweak
  remove builtin tests as they are already covered.
  Apply patch to cleanup whitespace from wsfulton
  and... remove all the .py files that were copied over from the source tree.
  remove -py3 related tests.  Move py3 specific code to it's own module
  copy everything with a .py extension into the build tree (creating dirs if needed)
  debug
  avoid the shell checks involving __init__.py
  Something optimizes out empty files and breaks the tests.  Put something in.
  fix for new split module test cases.
  split_wrapper -> split_modules to avoid conflict with clean rule
  Add cases for tests without the -py3 flag even when PY3 is set.
  Tests/Examples for the use case of splitting the wrapper. Half of the wrapper in a package and 1/2 is global.
This commit is contained in:
William S Fulton 2016-06-05 21:03:40 +01:00
commit ad98b58a3b
15 changed files with 213 additions and 60 deletions

View file

@ -10,11 +10,15 @@ import_packages_subdirs = \
from_init3 \
relativeimport1 \
relativeimport2 \
relativeimport3
relativeimport3 \
split_modules \
namespace_pkg
check: build
if test "x$(SRCDIR)" != x; then \
for file in `cd $(SRCDIR) && find . -type f -name __init__.py`; do \
for file in `cd $(SRCDIR) && find . -type f -name "*.py"`; do \
mkdir -p `dirname $$file`; \
cp "${SRCDIR}$$file" "$$file" || exit 1; \
done; \
fi; \
@ -35,7 +39,7 @@ static:
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' python_clean
if test "x$(SRCDIR)" != x; then \
for file in `cd $(SRCDIR) && find . -type f -name __init__.py`; do \
for file in `cd $(SRCDIR) && find . -type f -name "*.py"`; do \
rm -f "$$file" || exit 1; \
done; \
fi; \

View file

@ -0,0 +1,45 @@
import sys
import os
import shutil
import 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'))
os.system('cp _robin.* ' + os.path.join('path1', 'brave'))
shutil.copy('robin.py', os.path.join('path2', 'brave'))
os.system('cp _robin.* ' + 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()
def main():
copyMods()
# Run each test with a separate 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")
if __name__ == "__main__":
main()

View file

@ -4,48 +4,5 @@ import sys
if sys.version_info < (3, 3, 0):
sys.exit(0)
import os
import shutil
import 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'))
os.system('cp _robin.* ' + os.path.join('path1', 'brave'))
shutil.copy('robin.py', os.path.join('path2', 'brave'))
os.system('cp _robin.* ' + 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()
def main():
copyMods()
# Run each test with a separate 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")
if __name__ == "__main__":
main()
import nstest
nstest.main()

View file

@ -0,0 +1,26 @@
TOP = ../../..
LIBS =
subdirs = vanilla vanilla_split
check: build
for s in $(subdirs); do \
(cd $$s && $(MAKE) check); \
done
build:
for s in $(subdirs); do \
(cd $$s && $(MAKE) SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' build); \
done
static:
for s in $(subdirs); do \
(cd $$s && $(MAKE) SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' static); \
done
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' python_clean
for s in $(subdirs); do \
(cd $$s && $(MAKE) clean); \
done

View file

@ -0,0 +1,7 @@
These examples/tests are for when the SWIG generated wrapper module is split
between two packages. Specifically the pure python part is part of a package
and the C/C++ part is not in any package at all. Historically SWIG has
supported this sort of thing.
vanilla # "plane Jane" module both halves in pkg1
vanilla_split # python 1/2 in pkg1 C 1/2 in global namespace

View file

@ -0,0 +1,18 @@
TOP = ../../../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SWIGOPT =
LIBS =
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' python_run
build:
cd pkg1 && $(MAKE) build
static:
cd pkg1 && $(MAKE) static
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='foo' python_clean
cd pkg1 && $(MAKE) -f $(TOP)/../Makefile SRCDIR='$(SRCDIR)' TARGET='foo' python_clean

View file

@ -0,0 +1,18 @@
TOP = ../../../../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SWIGOPT =
LIBS =
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' TARGET='foo' INTERFACE='foo.i' python_cpp
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' TARGET='foo' INTERFACE='foo.i' python_cpp_static
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='foo' python_clean

View file

@ -0,0 +1 @@
# killroy was here

View file

@ -0,0 +1,10 @@
%module(package="pkg1") foo
%{
static unsigned count(void)
{
return 3;
}
%}
unsigned count(void);

View file

@ -0,0 +1,3 @@
import pkg1.foo
assert(pkg1.foo.count() == 3)

View file

@ -0,0 +1,22 @@
TOP = ../../../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SWIGOPT = -outdir pkg1
LIBS =
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' python_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' TARGET='foo' INTERFACE='foo.i' python_cpp
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' TARGET='foo' INTERFACE='foo.i' python_cpp_static
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='foo' python_clean
cd pkg1 && $(MAKE) -f $(TOP)/../Makefile SRCDIR='$(SRCDIR)' TARGET='foo' python_clean

View file

@ -0,0 +1,10 @@
%module(package="pkg1") foo
%{
static unsigned count(void)
{
return 3;
}
%}
unsigned count(void);

View file

@ -0,0 +1 @@
# killroy was here

View file

@ -0,0 +1,3 @@
import pkg1.foo
assert(pkg1.foo.count() == 3)

View file

@ -827,6 +827,14 @@ public:
* in 2.6, and fail in 2.7 onwards), but the relative import syntax
* isn't available in python 2.4 or earlier, so we have to write some
* code conditional on the python version.
*
* For python 2.7.0 and newer, first determine the shadow wrappers package
* based on the __name__ it was given by the importer that loaded it.
* Then construct a name for the module based on the package name and the
* module name (we know the module name). Use importlib to try and load
* it. If an attempt to load the module with importlib fails with an
* ImportError then fallback and try and load just the module name from
* the global namespace.
*/
Printv(f_shadow, "if version_info >= (2, 7, 0):\n", NULL);
Printv(f_shadow, tab4, "def swig_import_helper():\n", NULL);
@ -834,7 +842,12 @@ public:
Printv(f_shadow, tab8, "pkg = __name__.rpartition('.')[0]\n", NULL);
Printf(f_shadow, tab8 "mname = '.'.join((pkg, '%s')).lstrip('.')\n",
module);
Printv(f_shadow, tab8, "return importlib.import_module(mname)\n", NULL);
Printv(f_shadow, tab8, "try:\n", NULL);
Printv(f_shadow, tab8, tab4, "return importlib.import_module(mname)\n",
NULL);
Printv(f_shadow, tab8, "except ImportError:\n", NULL);
Printf(f_shadow, tab8 tab4 "return importlib.import_module('%s')\n",
module);
Printf(f_shadow, tab4 "%s = swig_import_helper()\n", module);
Printv(f_shadow, tab4, "del swig_import_helper\n", NULL);
Printv(f_shadow, "elif version_info >= (2, 6, 0):\n", NULL);
@ -859,21 +872,36 @@ public:
Printv(f_shadow, "else:\n", NULL);
Printf(f_shadow, tab4 "import %s\n", module);
if (builtin) {
/*
* Pull in all the attributes from the C module.
*
* An alternative approach to doing this if/else chain was
* proposed by Michael Thon. Someone braver than I may try it out.
* I fear some current swig user may depend on some side effect
* of from _foo import *
*
* for attr in _foo.__all__:
* globals()[attr] = getattr(_foo, attr)
*
*/
Printf(f_shadow, "# pull in all the attributes from %s\n", module);
Printv(f_shadow, "if __name__.rpartition('.')[0] != '':\n", NULL);
Printv(f_shadow, tab4, "if version_info >= (2, 7, 0):\n", NULL);
Printv(f_shadow, tab8, "try:\n", NULL);
Printf(f_shadow, tab8 tab4 "from .%s import *\n", module);
Printv(f_shadow, tab8 "except ImportError:\n", NULL);
Printf(f_shadow, tab8 tab4 "from %s import *\n", module);
Printv(f_shadow, tab4, "else:\n", NULL);
Printf(f_shadow, tab8 "from %s import *\n", module);
Printv(f_shadow, "else:\n", NULL);
Printf(f_shadow, tab4 "from %s import *\n", module);
}
/* Delete the version_info symbol since we don't use it elsewhere in the
* module. */
Printv(f_shadow, "del version_info\n", NULL);
if (builtin) {
/*
* Python3 removes relative imports. So 'from _foo import *'
* will only work for non-package modules.
*/
Printv(f_shadow, "if __name__.rpartition('.')[0] != '':\n", NULL);
Printf(f_shadow, tab4 "from %s%s import *\n", (py3 ? "." : ""),
module);
Printv(f_shadow, "else:\n", NULL);
Printf(f_shadow, tab4 "from %s import *\n", module);
}
if (modern || !classic) {
Printv(f_shadow, "try:\n", tab4, "_swig_property = property\n", "except NameError:\n", tab4, "pass # Python < 2.2 doesn't have 'property'.\n\n", NULL);
}