From c21e2423a02939d2a4bc02bdfaa5c3bed3be95b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tomulik?= Date: Sat, 25 Oct 2014 22:56:41 +0200 Subject: [PATCH 1/3] make %constant directive to work with structs/classes --- .../python/constant_directive_runme.py | 21 ++++++++++++ Source/Modules/python.cxx | 33 ++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 Examples/test-suite/python/constant_directive_runme.py diff --git a/Examples/test-suite/python/constant_directive_runme.py b/Examples/test-suite/python/constant_directive_runme.py new file mode 100644 index 000000000..b52b5e231 --- /dev/null +++ b/Examples/test-suite/python/constant_directive_runme.py @@ -0,0 +1,21 @@ +import constant_directive + +if type(constant_directive.TYPE1_CONSTANT1) != constant_directive.Type1: + print("TYPE1_CONSTANT1 type: {}".format(type(constant_directive.TYPE1_CONSTANT1))) + raise RuntimeError("fail"); +if type(constant_directive.getType1Instance()) != constant_directive.Type1: + print("getType1Instance() type: {}".format(type(constant_directive.getType1Instance()))) + raise RuntimeError("fail"); + +if constant_directive.TYPE1_CONSTANT1.val != 1: + print "constant_directive.TYPE1_CONSTANT1.val != 1" + print "constant_directive.TYPE1_CONSTANT1.val is %r" % constant_directive.TYPE1_CONSTANT1.val + raise RuntimeError("fail") +if constant_directive.TYPE1_CONSTANT2.val != 2: + print "constant_directive.TYPE1_CONSTANT2.val != 2" + print "constant_directive.TYPE1_CONSTANT2.val is %r" % constant_directive.TYPE1_CONSTANT2.val + raise RuntimeError("fail") +if constant_directive.TYPE1_CONSTANT3.val != 3: + print "constant_directive.TYPE1_CONSTANT3.val != 3" + print "constant_directive.TYPE1_CONSTANT3.val is %r" % constant_directive.TYPE1_CONSTANT3.val + raise RuntimeError("fail") diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 6a0b286ca..c52d40b53 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -3169,7 +3169,32 @@ public: Replaceall(tm, "$source", value); Replaceall(tm, "$target", name); Replaceall(tm, "$value", value); - Printf(f_init, "%s\n", tm); + if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER)) && (!in_class || !Getattr(n, "feature:python:callback"))) { + // Generate method which registers the new constant + Printf(f_wrappers, "SWIGINTERN PyObject* %s_swigregister(PyObject* SWIGUNUSEDPARM(self), PyObject* args) {\n", iname); + Printf(f_wrappers, tab2 "PyObject *m;\n", tm); + if (modernargs) { + if (fastunpack) { + Printf(f_wrappers, tab2 "if (!SWIG_Python_UnpackTuple(args,(char*)\"swigregister\", 1, 1,&m)) return NULL;\n"); + } else { + Printf(f_wrappers, tab2 "if (!PyArg_UnpackTuple(args,(char*)\"swigregister\", 1, 1,&m)) return NULL;\n"); + } + } else { + Printf(f_wrappers, tab2 "if (!PyArg_ParseTuple(args,(char*)\"O:swigregister\", &m)) return NULL;\n"); + } + Printf(f_wrappers, tab2 "PyObject* d = PyModule_GetDict(m);\n"); + Printf(f_wrappers, tab2 "if(!d) return NULL;\n"); + Printf(f_wrappers, tab2 "%s\n", tm); + Printf(f_wrappers, tab2 "return SWIG_Py_Void();\n"); + Printf(f_wrappers, "}\n\n\n"); + + // Register the method in SwigMethods array + String *cname = NewStringf("%s_swigregister", iname); + add_method(cname, cname, 0); + Delete(cname); + } else { + Printf(f_init, "%s\n", tm); + } Delete(tm); have_tm = 1; } @@ -3184,9 +3209,15 @@ public: if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER))) { if (!in_class) { + Printv(f_shadow, "\n",NIL); + Printv(f_shadow, module, ".", iname, "_swigregister(",module,")\n", NIL); + Printv(f_shadow, iname, "_swigregister = ", module, ".", iname, "_swigregister\n", NIL); Printv(f_shadow, iname, " = ", module, ".", iname, "\n", NIL); } else { if (!(Getattr(n, "feature:python:callback"))) { + Printv(f_shadow_stubs, "\n",NIL); + Printv(f_shadow_stubs, module, ".", iname, "_swigregister(", module, ")\n", NIL); + Printv(f_shadow_stubs, iname, "_swigregister = ", module, ".", iname, "_swigregister\n", NIL); Printv(f_shadow_stubs, iname, " = ", module, ".", iname, "\n", NIL); } } From cfaf2f97fd207de1200eee821aab21c7d1014de3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tomulik?= Date: Wed, 31 Dec 2014 14:55:35 +0100 Subject: [PATCH 2/3] additional fixes to %constant directive --- .../python/constant_directive_runme.py | 20 ++++++----------- Source/Modules/python.cxx | 22 +++++++++---------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Examples/test-suite/python/constant_directive_runme.py b/Examples/test-suite/python/constant_directive_runme.py index b52b5e231..8887562e4 100644 --- a/Examples/test-suite/python/constant_directive_runme.py +++ b/Examples/test-suite/python/constant_directive_runme.py @@ -1,21 +1,15 @@ import constant_directive if type(constant_directive.TYPE1_CONSTANT1) != constant_directive.Type1: - print("TYPE1_CONSTANT1 type: {}".format(type(constant_directive.TYPE1_CONSTANT1))) - raise RuntimeError("fail"); + raise RuntimeError("Failure: TYPE1_CONSTANT1 type: {}".format(type(constant_directive.TYPE1_CONSTANT1))) if type(constant_directive.getType1Instance()) != constant_directive.Type1: - print("getType1Instance() type: {}".format(type(constant_directive.getType1Instance()))) - raise RuntimeError("fail"); + raise RuntimeError("Failure: getType1Instance() type: {}".format(type(constant_directive.getType1Instance()))) if constant_directive.TYPE1_CONSTANT1.val != 1: - print "constant_directive.TYPE1_CONSTANT1.val != 1" - print "constant_directive.TYPE1_CONSTANT1.val is %r" % constant_directive.TYPE1_CONSTANT1.val - raise RuntimeError("fail") + raise RuntimeError("constant_directive.TYPE1_CONSTANT1.val is %r (should be 1)" % constant_directive.TYPE1_CONSTANT1.val) + if constant_directive.TYPE1_CONSTANT2.val != 2: - print "constant_directive.TYPE1_CONSTANT2.val != 2" - print "constant_directive.TYPE1_CONSTANT2.val is %r" % constant_directive.TYPE1_CONSTANT2.val - raise RuntimeError("fail") + raise RuntimeError("constant_directive.TYPE1_CONSTANT2.val is %r (should be 2)" % constant_directive.TYPE1_CONSTANT2.val) + if constant_directive.TYPE1_CONSTANT3.val != 3: - print "constant_directive.TYPE1_CONSTANT3.val != 3" - print "constant_directive.TYPE1_CONSTANT3.val is %r" % constant_directive.TYPE1_CONSTANT3.val - raise RuntimeError("fail") + raise RuntimeError("constant_directive.TYPE1_CONSTANT3.val is %r (should be 3)" % constant_directive.TYPE1_CONSTANT3.val) diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index c52d40b53..4c703d326 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -3171,25 +3171,25 @@ public: Replaceall(tm, "$value", value); if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER)) && (!in_class || !Getattr(n, "feature:python:callback"))) { // Generate method which registers the new constant - Printf(f_wrappers, "SWIGINTERN PyObject* %s_swigregister(PyObject* SWIGUNUSEDPARM(self), PyObject* args) {\n", iname); - Printf(f_wrappers, tab2 "PyObject *m;\n", tm); + Printf(f_wrappers, "SWIGINTERN PyObject *%s_swigconstant(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {\n", iname); + Printf(f_wrappers, tab2 "PyObject *module;\n", tm); if (modernargs) { if (fastunpack) { - Printf(f_wrappers, tab2 "if (!SWIG_Python_UnpackTuple(args,(char*)\"swigregister\", 1, 1,&m)) return NULL;\n"); + Printf(f_wrappers, tab2 "if (!SWIG_Python_UnpackTuple(args,(char*)\"swigconstant\", 1, 1,&module)) return NULL;\n"); } else { - Printf(f_wrappers, tab2 "if (!PyArg_UnpackTuple(args,(char*)\"swigregister\", 1, 1,&m)) return NULL;\n"); + Printf(f_wrappers, tab2 "if (!PyArg_UnpackTuple(args,(char*)\"swigconstant\", 1, 1,&module)) return NULL;\n"); } } else { - Printf(f_wrappers, tab2 "if (!PyArg_ParseTuple(args,(char*)\"O:swigregister\", &m)) return NULL;\n"); + Printf(f_wrappers, tab2 "if (!PyArg_ParseTuple(args,(char*)\"O:swigconstant\", &module)) return NULL;\n"); } - Printf(f_wrappers, tab2 "PyObject* d = PyModule_GetDict(m);\n"); - Printf(f_wrappers, tab2 "if(!d) return NULL;\n"); + Printf(f_wrappers, tab2 "PyObject *d = PyModule_GetDict(module);\n"); + Printf(f_wrappers, tab2 "if (!d) return NULL;\n"); Printf(f_wrappers, tab2 "%s\n", tm); Printf(f_wrappers, tab2 "return SWIG_Py_Void();\n"); Printf(f_wrappers, "}\n\n\n"); // Register the method in SwigMethods array - String *cname = NewStringf("%s_swigregister", iname); + String *cname = NewStringf("%s_swigconstant", iname); add_method(cname, cname, 0); Delete(cname); } else { @@ -3210,14 +3210,12 @@ public: if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER))) { if (!in_class) { Printv(f_shadow, "\n",NIL); - Printv(f_shadow, module, ".", iname, "_swigregister(",module,")\n", NIL); - Printv(f_shadow, iname, "_swigregister = ", module, ".", iname, "_swigregister\n", NIL); + Printv(f_shadow, module, ".", iname, "_swigconstant(",module,")\n", NIL); Printv(f_shadow, iname, " = ", module, ".", iname, "\n", NIL); } else { if (!(Getattr(n, "feature:python:callback"))) { Printv(f_shadow_stubs, "\n",NIL); - Printv(f_shadow_stubs, module, ".", iname, "_swigregister(", module, ")\n", NIL); - Printv(f_shadow_stubs, iname, "_swigregister = ", module, ".", iname, "_swigregister\n", NIL); + Printv(f_shadow_stubs, module, ".", iname, "_swigconstant(", module, ")\n", NIL); Printv(f_shadow_stubs, iname, " = ", module, ".", iname, "\n", NIL); } } From 2f6dee3adb058b44e866fd7bd712a8b2a276395f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tomulik?= Date: Fri, 2 Jan 2015 18:48:17 +0100 Subject: [PATCH 3/3] constant_directive_runme.py and classic classes --- Examples/test-suite/python/constant_directive_runme.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Examples/test-suite/python/constant_directive_runme.py b/Examples/test-suite/python/constant_directive_runme.py index 8887562e4..48f85ce8a 100644 --- a/Examples/test-suite/python/constant_directive_runme.py +++ b/Examples/test-suite/python/constant_directive_runme.py @@ -1,8 +1,8 @@ import constant_directive -if type(constant_directive.TYPE1_CONSTANT1) != constant_directive.Type1: +if not isinstance(constant_directive.TYPE1_CONSTANT1,constant_directive.Type1): raise RuntimeError("Failure: TYPE1_CONSTANT1 type: {}".format(type(constant_directive.TYPE1_CONSTANT1))) -if type(constant_directive.getType1Instance()) != constant_directive.Type1: +if not isinstance(constant_directive.getType1Instance(),constant_directive.Type1): raise RuntimeError("Failure: getType1Instance() type: {}".format(type(constant_directive.getType1Instance()))) if constant_directive.TYPE1_CONSTANT1.val != 1: