From 627f7214dbe6d21487f1ce6800c6b433be92518e Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 11 Jan 2022 23:33:22 +0000 Subject: [PATCH] [Python] Add missing Python kwargs builtin support Accept keyword arguments accessing C++ static member functions when using -builtin and kwargs feature and Python class staticmethod syntax. The missing keyword argument support was only when using the class staticmethod syntax, not when using the flat static method syntax. --- CHANGES.current | 7 +++++++ Examples/test-suite/kwargs_feature.i | 1 + Examples/test-suite/python/kwargs_feature_runme.py | 3 +++ Source/Modules/python.cxx | 4 ++-- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 6632f0126..a5a7b8c34 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,13 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-01-11: wsfulton + [Python] Accept keyword arguments accessing static member functions when + using -builtin and kwargs feature and Python class staticmethod syntax. + The missing keyword argument support was only when using the + class staticmethod syntax, such as Klass.memberfunction, and not when + using the flat static method syntax, such as Klass_memberfunction. + 2022-01-04: juierror [Go] #2045 Add support for std::array in std_array.i. diff --git a/Examples/test-suite/kwargs_feature.i b/Examples/test-suite/kwargs_feature.i index dd5b2638d..6db4d407b 100644 --- a/Examples/test-suite/kwargs_feature.i +++ b/Examples/test-suite/kwargs_feature.i @@ -27,6 +27,7 @@ virtual int foo(int a = 1, int b = 0) {return a + b; } static int statfoo(int a = 1, int b = 0) {return a + b; } + static int statfoo_onearg(int x = 10) {return x + x; } static Foo *create(int a = 1, int b = 0) { diff --git a/Examples/test-suite/python/kwargs_feature_runme.py b/Examples/test-suite/python/kwargs_feature_runme.py index d07525ffc..677c9ebd8 100644 --- a/Examples/test-suite/python/kwargs_feature_runme.py +++ b/Examples/test-suite/python/kwargs_feature_runme.py @@ -18,6 +18,9 @@ if f.foo(b=1, a=2) != 3: if Foo.statfoo(b=2) != 3: raise RuntimeError +if Foo.statfoo_onearg(x=4) != 8: + raise RuntimeError + if f.efoo(b=2) != 3: raise RuntimeError diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 8682e6917..9046edcfb 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -4727,6 +4727,7 @@ public: Swig_restore(n); } + int kw = (check_kwargs(n) && !Getattr(n, "sym:overloaded")) ? 1 : 0; if (builtin && in_class) { if ((GetFlagAttr(n, "feature:extend") || checkAttribute(n, "access", "public")) && !Getattr(class_members, symname)) { @@ -4741,7 +4742,7 @@ public: else if (funpack && argcount == 1) Append(pyflags, "METH_O"); else - Append(pyflags, "METH_VARARGS"); + Append(pyflags, kw ? "METH_VARARGS|METH_KEYWORDS" : "METH_VARARGS"); // Cast via void(*)(void) to suppress GCC -Wcast-function-type warning. // Python should always call the function correctly, but the Python C // API requires us to store it in function pointer of a different type. @@ -4767,7 +4768,6 @@ public: String *staticfunc_name = NewString(fastproxy ? "_swig_new_static_method" : "staticmethod"); bool fast = (fastproxy && !have_addtofunc(n)) || Getattr(n, "feature:callback"); if (!fast || olddefs) { - int kw = (check_kwargs(n) && !Getattr(n, "sym:overloaded")) ? 1 : 0; String *parms = make_pyParmList(n, false, false, kw); String *callParms = make_pyParmList(n, false, true, kw); Printv(f_shadow, "\n", tab4, "@staticmethod", NIL);