[Python] Import the C extension differently for Python 2.6 and

later so that an implicit relative import doesn't produce a
deprecation warning for 2.6 and a failure for 2.7 and later.
Patch from Richard Boulton in SF#2008229.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10624 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Olly Betts 2008-07-02 03:17:46 +00:00
commit 31926ad905
2 changed files with 25 additions and 1 deletions

View file

@ -689,7 +689,25 @@ public:
mod_docstring = NULL;
}
Printf(f_shadow, "\nimport %s\n", module);
Printv(f_shadow, "\nfrom sys import version_info\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), 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.
*/
Printv(f_shadow, "if version_info >= (2,6,0):\n", NULL);
Printf(f_shadow, tab4 "from . import %s\n", module);
Printv(f_shadow, "else:\n", NULL);
Printf(f_shadow, tab4 "import %s\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);
Printv(f_shadow, "import new\n", NULL);
Printv(f_shadow, "new_instancemethod = new.instancemethod\n", NULL);