From fd651ff4e2eca00e7c530b717b7a79e2a89440a6 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 31 Jul 2018 19:12:02 +0100 Subject: [PATCH] Overloaded C++ function Python wrappers now raise a TypeError instead of NotImplementedError Occurs when the types passed are incorrect. This change means there is now consistency with non-overloaded function wrappers which have always raised TypeError when the incorrect types are passed. See issue #1293 --- CHANGES.current | 21 +++++++++++++++++++ Doc/Manual/Python.html | 2 +- .../python/li_std_string_extra_runme.py | 12 +++++++---- .../python/primitive_types_runme.py | 7 +------ Examples/test-suite/python/varargs_runme.py | 2 -- Source/Modules/python.cxx | 2 +- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 46bb5a96f..64a2e65fd 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,27 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.0.0 (in progress) =========================== +2018-07-31: wsfulton + [Python] #1293 Overloaded C++ function wrappers now raise a TypeError instead + of NotImplementedError when the types passed are incorrect. This change means + there is now consistency with non-overloaded function wrappers which have always + raised TypeError when the incorrect types are passed. The error message remains + the same and is for example now: + + TypeError: Wrong number or type of arguments for overloaded function 'f'. + Possible C/C++ prototypes are: + f(int) + f(char const *) + + instead of: + + NotImplementedError: Wrong number or type of arguments for overloaded function 'f'. + Possible C/C++ prototypes are: + f(int) + f(char const *) + + *** POTENTIAL INCOMPATIBILITY *** + 2018-06-23: wsfulton [Python] #718 Fix pythonnondynamic feature for modern classes diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html index 1a2fd9e81..59690609a 100644 --- a/Doc/Manual/Python.html +++ b/Doc/Manual/Python.html @@ -4894,7 +4894,7 @@ If you don't you'll get an error message along the lines of: Traceback (most recent call last): File "runme.py", line 3, in >module< example.foo(["foo", "bar", "spam", "1"]) -NotImplementedError: Wrong number or type of arguments for overloaded function 'foo'. +TypeError: Wrong number or type of arguments for overloaded function 'foo'. Possible C/C++ prototypes are: foo(int, char **) foo() diff --git a/Examples/test-suite/python/li_std_string_extra_runme.py b/Examples/test-suite/python/li_std_string_extra_runme.py index 9835eaa81..53d2bdc75 100644 --- a/Examples/test-suite/python/li_std_string_extra_runme.py +++ b/Examples/test-suite/python/li_std_string_extra_runme.py @@ -95,14 +95,18 @@ if li_std_string_extra.test_value_basic_overload(123) != "int": try: li_std_string_extra.test_value_basic_overload([x]) - raise RuntimeError, "should throw NotImplementedError" -except NotImplementedError: + raise RuntimeError, "should throw TypeError" +except TypeError as e: + if str(e).find("Possible C/C++ prototypes are:") == -1: + raise RuntimeError("Incorrect error message text:\n{}".format(e)) pass try: li_std_string_extra.test_value_basic_overload([123]) - raise RuntimeError, "should throw NotImplementedError" -except NotImplementedError: + raise RuntimeError, "should throw TypeError" +except TypeError as e: + if str(e).find("Possible C/C++ prototypes are:") == -1: + raise RuntimeError("Incorrect error message text:\n{}".format(e)) pass # Global variables diff --git a/Examples/test-suite/python/primitive_types_runme.py b/Examples/test-suite/python/primitive_types_runme.py index b2060a028..c5009f640 100644 --- a/Examples/test-suite/python/primitive_types_runme.py +++ b/Examples/test-suite/python/primitive_types_runme.py @@ -549,14 +549,9 @@ def checkOverload(t, name, val, delta, prevval, limit): if t.ovr_str(val + delta) == name: raise RuntimeError, "bad " + name + " typemap" if val == limit: - # Should raise NotImplementedError here since this is the largest integral type - raise RuntimeError, "bad " + name + " typemap" - except NotImplementedError: - # NotImplementedError is expected only if this is the most extreme type - if val != limit: + # Should raise TypeError here since this is the largest integral type raise RuntimeError, "bad " + name + " typemap" except TypeError: - # TypeError is raised instead if swig is run with -O or -fastdispatch if val != limit: raise RuntimeError, "bad " + name + " typemap" diff --git a/Examples/test-suite/python/varargs_runme.py b/Examples/test-suite/python/varargs_runme.py index 7105ba8d7..277ea757a 100644 --- a/Examples/test-suite/python/varargs_runme.py +++ b/Examples/test-suite/python/varargs_runme.py @@ -30,7 +30,5 @@ if varargs.test_plenty("Hello", 1, 2) != "Hello": try: varargs.test_plenty("Hello", 1, 2, 3) raise RuntimeError -except NotImplementedError: - pass except TypeError: pass diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 89a9d2a8b..f7ec22a51 100755 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -2645,7 +2645,7 @@ public: Delete(fulldecl); } while ((sibl = Getattr(sibl, "sym:nextSibling"))); Append(f->code, "fail:\n"); - Printf(f->code, " SWIG_SetErrorMsg(PyExc_NotImplementedError," + Printf(f->code, " SWIG_SetErrorMsg(PyExc_TypeError," "\"Wrong number or type of arguments for overloaded function '%s'.\\n\"" "\n\" Possible C/C++ prototypes are:\\n\"%s);\n", symname, protoTypes); Printf(f->code, "return %s;\n", builtin_ctor ? "-1" : "0"); Delete(protoTypes);