From 31926ad905013ecd2dcb2fb97fbdcb2ec5a070d9 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Wed, 2 Jul 2008 03:17:46 +0000 Subject: [PATCH] [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 --- CHANGES.current | 6 ++++++ Source/Modules/python.cxx | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGES.current b/CHANGES.current index ef3fcca54..89f48ce93 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -1,3 +1,9 @@ Version 1.3.36 (in progress) ============================= +2008-07-02: olly + [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. + diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index f0e335c37..d497b57b3 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -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);