diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html index 3def2080e..9c43fb3fe 100644 --- a/Doc/Manual/Python.html +++ b/Doc/Manual/Python.html @@ -149,9 +149,9 @@
This chapter describes SWIG's support of Python. SWIG is compatible -with most recent Python versions (Python 2.6 and later, including Python 3). -If you still need to generate bindings which work with older versions of -Python, you'll have to use SWIG 3.0.x. +with most recent Python versions (Python 2.7 and Python 3.x). If you still +need to generate bindings which work with older versions of Python, you'll have +to use SWIG 3.0.x.
diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index 3004e0538..a19ab54cf 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -7,8 +7,8 @@ * * ----------------------------------------------------------------------------- */ -#if PY_VERSION_HEX < 0x02060000 /* 2.6.0 */ -# error "This version of SWIG only supports Python >= 2.6" +#if PY_VERSION_HEX < 0x02070000 /* 2.7.0 */ +# error "This version of SWIG only supports Python >= 2.7" #endif /* Common SWIG API */ @@ -1385,8 +1385,8 @@ SWIG_Python_GetModule(void *SWIGUNUSEDPARM(clientdata)) { # ifdef SWIGPY_USE_CAPSULE type_pointer = PyCapsule_Import(SWIGPY_CAPSULE_NAME, 0); # else - type_pointer = PyCObject_Import((char *)"swig_runtime_data" SWIG_RUNTIME_VERSION, - (char *)"type_pointer" SWIG_TYPE_TABLE_NAME); + type_pointer = PyCObject_Import("swig_runtime_data" SWIG_RUNTIME_VERSION, + "type_pointer" SWIG_TYPE_TABLE_NAME); # endif if (PyErr_Occurred()) { PyErr_Clear(); diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index ce7addc5b..d1144522d 100755 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -830,58 +830,39 @@ public: if (!builtin && fastproxy) { Printv(default_import_code, "if _swig_python_version_info >= (3, 0, 0):\n", NULL); Printf(default_import_code, tab4 "new_instancemethod = lambda func, inst, cls: %s.SWIG_PyInstanceMethod_New(func)\n", module); - Printv(default_import_code, "else:\n", NULL); + Printv(default_import_code, "elif _swig_python_version_info >= (2, 7, 0):\n", NULL); Printv(default_import_code, tab4, "from new import instancemethod as new_instancemethod\n", NULL); + Printv(default_import_code, "else:\n", NULL); + Printv(default_import_code, tab4, "raise RuntimeError('Python 2.7 or later required')\n", NULL); + } else { + Printv(default_import_code, "if _swig_python_version_info < (2, 7, 0):\n", NULL); + Printv(default_import_code, tab4, "raise RuntimeError('Python 2.7 or later required')\n", NULL); } /* Import the C-extension module. This should be a relative import, * since the shadow module may also have been imported by a relative * import, and there is thus no guarantee that the C-extension is on * sys.path. Relative imports must be explicitly specified from 2.6.0 - * onwards (implicit relative imports will raise a DeprecationWarning - * in 2.6, and fail in 2.7 onwards). + * onwards (implicit relative imports raised a DeprecationWarning in 2.6, + * and fail in 2.7 onwards). * - * 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. + * 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(default_import_code, "if _swig_python_version_info >= (2, 7, 0):\n", NULL); - Printv(default_import_code, tab4, "def swig_import_helper():\n", NULL); - Printv(default_import_code, tab8, "import importlib\n", NULL); - Printv(default_import_code, tab8, "pkg = __name__.rpartition('.')[0]\n", NULL); - Printf(default_import_code, tab8 "mname = '.'.join((pkg, '%s')).lstrip('.')\n", module); - Printv(default_import_code, tab8, "try:\n", NULL); - Printv(default_import_code, tab8, tab4, "return importlib.import_module(mname)\n", NULL); - Printv(default_import_code, tab8, "except ImportError:\n", NULL); - Printf(default_import_code, tab8 tab4 "return importlib.import_module('%s')\n", module); - Printf(default_import_code, tab4 "%s = swig_import_helper()\n", module); - Printv(default_import_code, tab4, "del swig_import_helper\n", NULL); - Printv(default_import_code, "elif _swig_python_version_info >= (2, 6, 0):\n", NULL); - Printv(default_import_code, tab4, "def swig_import_helper():\n", NULL); - Printv(default_import_code, tab8, "from os.path import dirname\n", NULL); - Printv(default_import_code, tab8, "import imp\n", NULL); - Printv(default_import_code, tab8, "fp = None\n", NULL); - Printv(default_import_code, tab8, "try:\n", NULL); - Printf(default_import_code, tab4 tab8 "fp, pathname, description = imp.find_module('%s', [dirname(__file__)])\n", module); - Printf(default_import_code, tab8 "except ImportError:\n"); - /* At here, the module may already loaded, so simply import it. */ - Printf(default_import_code, tab4 tab8 "import %s\n", module); - Printf(default_import_code, tab4 tab8 "return %s\n", module); - Printv(default_import_code, tab8 "try:\n", NULL); - /* imp.load_module() handles fp being None. */ - Printf(default_import_code, tab4 tab8 "_mod = imp.load_module('%s', fp, pathname, description)\n", module); - Printv(default_import_code, tab8, "finally:\n", NULL); - Printv(default_import_code, tab4 tab8 "if fp is not None:\n", NULL); - Printv(default_import_code, tab8 tab8, "fp.close()\n", NULL); - Printv(default_import_code, tab8, "return _mod\n", NULL); - Printf(default_import_code, tab4 "%s = swig_import_helper()\n", module); - Printv(default_import_code, tab4, "del swig_import_helper\n", NULL); - Printv(default_import_code, "else:\n", NULL); - Printv(default_import_code, tab4, "raise RuntimeError('Python 2.6 or later required')\n", NULL); + Printv(default_import_code, "def swig_import_helper():\n", NULL); + Printv(default_import_code, tab4, "import importlib\n", NULL); + Printv(default_import_code, tab4, "pkg = __name__.rpartition('.')[0]\n", NULL); + Printf(default_import_code, tab4 "mname = '.'.join((pkg, '%s')).lstrip('.')\n", module); + Printv(default_import_code, tab4, "try:\n", NULL); + Printv(default_import_code, tab8, "return importlib.import_module(mname)\n", NULL); + Printv(default_import_code, tab4, "except ImportError:\n", NULL); + Printf(default_import_code, tab8 "return importlib.import_module('%s')\n", module); + Printf(default_import_code, "%s = swig_import_helper()\n", module); + Printv(default_import_code, "del swig_import_helper\n", NULL); if (builtin) { /*