diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2016-01-23 23:24:34 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2016-01-23 23:24:34 +0000 |
commit | 55f303d2868a6448bf6f17291530f95c0c106a95 (patch) | |
tree | 7378248ad9de4bbc70627e9222aff633f37b26b6 | |
parent | 584b328239360bcb45ad76fd62c7d45968b0a22e (diff) | |
parent | 2a5bbb601851184ba5e1c97cd35e4dd0848022e0 (diff) | |
download | swig-55f303d2868a6448bf6f17291530f95c0c106a95.tar.gz |
Merge pull request #584 from benmwebb/master
(Python) Qualify use of "__builtin__.Exception" class.
-rw-r--r-- | Examples/test-suite/common.mk | 1 | ||||
-rw-r--r-- | Examples/test-suite/exception_classname.i | 10 | ||||
-rw-r--r-- | Examples/test-suite/python/exception_classname_runme.py | 4 | ||||
-rw-r--r-- | Source/Modules/python.cxx | 17 |
4 files changed, 26 insertions, 6 deletions
diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 7b114fe7b..02bde2caa 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -218,6 +218,7 @@ CPP_TEST_CASES += \ evil_diamond \ evil_diamond_ns \ evil_diamond_prop \ + exception_classname \ exception_order \ extend \ extend_constructor_destructor \ diff --git a/Examples/test-suite/exception_classname.i b/Examples/test-suite/exception_classname.i new file mode 100644 index 000000000..a5a76e24d --- /dev/null +++ b/Examples/test-suite/exception_classname.i @@ -0,0 +1,10 @@ +%module exception_classname + +%warnfilter(SWIGWARN_RUBY_WRONG_NAME); + +%inline %{ +class Exception { +public: + int testfunc() { return 42; } +}; +%} diff --git a/Examples/test-suite/python/exception_classname_runme.py b/Examples/test-suite/python/exception_classname_runme.py new file mode 100644 index 000000000..c78f4e68b --- /dev/null +++ b/Examples/test-suite/python/exception_classname_runme.py @@ -0,0 +1,4 @@ +import exception_classname + +a = exception_classname.Exception() +assert a.testfunc() == 42 diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 3ed784c05..43c296306 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -859,6 +859,11 @@ public: 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); } + + /* Need builtins to qualify names like Exception that might also be + defined in this module (try both Python 3 and Python 2 names) */ + Printv(f_shadow, "try:\n", tab4, "import builtins as __builtin__\n", "except ImportError:\n", tab4, "import __builtin__\n", NULL); + /* if (!modern) */ /* always needed, a class can be forced to be no-modern, such as an exception */ { @@ -905,7 +910,7 @@ public: Printv(f_shadow, "\n", "def _swig_repr(self):\n", tab4, "try:\n", tab8, "strthis = \"proxy of \" + self.this.__repr__()\n", - tab4, "except Exception:\n", tab8, "strthis = \"\"\n", tab4, "return \"<%s.%s; %s >\" % (self.__class__.__module__, self.__class__.__name__, strthis,)\n\n", NIL); + tab4, "except __builtin__.Exception:\n", tab8, "strthis = \"\"\n", tab4, "return \"<%s.%s; %s >\" % (self.__class__.__module__, self.__class__.__name__, strthis,)\n\n", NIL); if (!classic) { /* Usage of types.ObjectType is deprecated. @@ -935,7 +940,7 @@ public: if (directorsEnabled()) { // Try loading weakref.proxy, which is only available in Python 2.1 and higher Printv(f_shadow, - "try:\n", tab4, "import weakref\n", tab4, "weakref_proxy = weakref.proxy\n", "except Exception:\n", tab4, "weakref_proxy = lambda x: x\n", "\n\n", NIL); + "try:\n", tab4, "import weakref\n", tab4, "weakref_proxy = weakref.proxy\n", "except __builtin__.Exception:\n", tab4, "weakref_proxy = lambda x: x\n", "\n\n", NIL); } } // Include some information in the code @@ -4491,11 +4496,11 @@ public: if (!modern) { Printv(f_shadow_file, tab8, "try:\n", tab8, tab4, "self.this.append(this)\n", - tab8, "except Exception:\n", tab8, tab4, "self.this = this\n", tab8, "self.this.own(0)\n", tab8, "self.__class__ = ", class_name, "\n\n", NIL); + tab8, "except __builtin__.Exception:\n", tab8, tab4, "self.this = this\n", tab8, "self.this.own(0)\n", tab8, "self.__class__ = ", class_name, "\n\n", NIL); } else { Printv(f_shadow_file, tab8, "try:\n", tab8, tab4, "self.this.append(this)\n", - tab8, "except Exception:\n", tab8, tab4, "self.this = this\n", tab8, "self.this.own(0)\n", tab8, "self.__class__ = ", class_name, "\n\n", NIL); + tab8, "except __builtin__.Exception:\n", tab8, tab4, "self.this = this\n", tab8, "self.this.own(0)\n", tab8, "self.__class__ = ", class_name, "\n\n", NIL); } } @@ -4837,7 +4842,7 @@ public: } else { Printv(f_shadow, tab8, "this = ", funcCall(Swig_name_construct(NSPACE_TODO, symname), callParms), "\n", - tab8, "try:\n", tab8, tab4, "self.this.append(this)\n", tab8, "except Exception:\n", tab8, tab4, "self.this = this\n", NIL); + tab8, "try:\n", tab8, tab4, "self.this.append(this)\n", tab8, "except __builtin__.Exception:\n", tab8, tab4, "self.this = this\n", NIL); } if (have_pythonappend(n)) Printv(f_shadow, indent_pythoncode(pythonappend(n), tab8, Getfile(n), Getline(n), "%pythonappend or %feature(\"pythonappend\")"), "\n\n", NIL); @@ -4935,7 +4940,7 @@ public: #ifdef USE_THISOWN Printv(f_shadow, tab8, "try:\n", NIL); Printv(f_shadow, tab8, tab4, "if self.thisown:", module, ".", Swig_name_destroy(NSPACE_TODO, symname), "(self)\n", NIL); - Printv(f_shadow, tab8, "except Exception: pass\n", NIL); + Printv(f_shadow, tab8, "except __builtin__.Exception: pass\n", NIL); #else #endif if (have_pythonappend(n)) |