From a1f40568d689bb722b7e72da4d40db5ab97bad39 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 19 Oct 2018 08:20:13 +0100 Subject: [PATCH] Remove non-const char * usage where the Python API now supports it Python fixed many APIs to use const char * instead of char * at around Python 2.4. As we support 2.7 and later, we can now remove the non-const string usage. Types changed: PyArg_ParseTuple PyArg_ParseTupleAndKeywords PyArg_UnpackTuple PyDict_SetItemString PyMethodDef PyModuleDef SWIG_Python_UnpackTuple SWIG_Python_str_FromChar SWIG_addvarlink swig_const_info --- Lib/python/defarg.swg | 2 +- Lib/python/pyapi.swg | 2 +- Lib/python/pyinit.swg | 8 +++---- Lib/python/pyrun.swg | 33 +++++++++++++--------------- Lib/python/pytypemaps.swg | 2 +- Source/Modules/python.cxx | 46 +++++++++++++++++++-------------------- 6 files changed, 45 insertions(+), 48 deletions(-) diff --git a/Lib/python/defarg.swg b/Lib/python/defarg.swg index 10c991615..59450bd82 100644 --- a/Lib/python/defarg.swg +++ b/Lib/python/defarg.swg @@ -24,7 +24,7 @@ SWIGINTERN PyObject *swig_call_defargs(PyObject *self, PyObject *args) { PyObject *func; PyObject *parms; - if (!PyArg_ParseTuple(args,"OO",&func,&parms)) + if (!PyArg_ParseTuple(args, "OO", &func, &parms)) return NULL; if (!PyCallable_Check(func)) { diff --git a/Lib/python/pyapi.swg b/Lib/python/pyapi.swg index 700f55d43..89f57fc83 100644 --- a/Lib/python/pyapi.swg +++ b/Lib/python/pyapi.swg @@ -17,7 +17,7 @@ extern "C" { /* Constant information structure */ typedef struct swig_const_info { int type; - char *name; + const char *name; long lvalue; double dvalue; void *pvalue; diff --git a/Lib/python/pyinit.swg b/Lib/python/pyinit.swg index d4985ee78..621d6f9ed 100644 --- a/Lib/python/pyinit.swg +++ b/Lib/python/pyinit.swg @@ -151,7 +151,7 @@ swig_varlink_type(void) { PyObject_HEAD_INIT(NULL) 0, /* ob_size */ #endif - (char *)"swigvarlink", /* tp_name */ + "swigvarlink", /* tp_name */ sizeof(swig_varlinkobject), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor) swig_varlink_dealloc, /* tp_dealloc */ @@ -208,7 +208,7 @@ SWIG_Python_newvarlink(void) { } SWIGINTERN void -SWIG_Python_addvarlink(PyObject *p, char *name, PyObject *(*get_attr)(void), int (*set_attr)(PyObject *p)) { +SWIG_Python_addvarlink(PyObject *p, const char *name, PyObject *(*get_attr)(void), int (*set_attr)(PyObject *p)) { swig_varlinkobject *v = (swig_varlinkobject *) p; swig_globalvar *gv = (swig_globalvar *) malloc(sizeof(swig_globalvar)); if (gv) { @@ -330,7 +330,7 @@ SWIG_init(void) { #if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef SWIG_module = { PyModuleDef_HEAD_INIT, - (char *) SWIG_name, + SWIG_name, NULL, -1, SwigMethods, @@ -393,7 +393,7 @@ SWIG_init(void) { #if PY_VERSION_HEX >= 0x03000000 m = PyModule_Create(&SWIG_module); #else - m = Py_InitModule((char *) SWIG_name, SwigMethods); + m = Py_InitModule(SWIG_name, SwigMethods); #endif md = d = PyModule_GetDict(m); diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index 9850a7c27..2e1ddddbc 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -208,7 +208,7 @@ SWIG_Python_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssi } /* A functor is a function object with one single object argument */ -#define SWIG_Python_CallFunctor(functor, obj) PyObject_CallFunction(functor, (char*)"O", obj); +#define SWIG_Python_CallFunctor(functor, obj) PyObject_CallFunction(functor, (char *)"O", obj); /* Helper for static pointer initialization for both C and C++ code, for example @@ -582,23 +582,20 @@ SWIGINTERN PyObject* SwigPyObject_own(PyObject *v, PyObject *args) { PyObject *val = 0; - if (!PyArg_UnpackTuple(args, "own", 0, 1, &val)) - { - return NULL; + if (!PyArg_UnpackTuple(args, "own", 0, 1, &val)) { + return NULL; + } else { + SwigPyObject *sobj = (SwigPyObject *)v; + PyObject *obj = PyBool_FromLong(sobj->own); + if (val) { + if (PyObject_IsTrue(val)) { + SwigPyObject_acquire(v,args); + } else { + SwigPyObject_disown(v,args); + } } - else - { - SwigPyObject *sobj = (SwigPyObject *)v; - PyObject *obj = PyBool_FromLong(sobj->own); - if (val) { - if (PyObject_IsTrue(val)) { - SwigPyObject_acquire(v,args); - } else { - SwigPyObject_disown(v,args); - } - } - return obj; - } + return obj; + } } static PyMethodDef @@ -805,7 +802,7 @@ SwigPyPacked_compare(SwigPyPacked *v, SwigPyPacked *w) size_t i = v->size; size_t j = w->size; int s = (i < j) ? -1 : ((i > j) ? 1 : 0); - return s ? s : strncmp((char *)v->pack, (char *)w->pack, 2*v->size); + return s ? s : strncmp((const char *)v->pack, (const char *)w->pack, 2*v->size); } SWIGRUNTIME PyTypeObject* SwigPyPacked_TypeOnce(void); diff --git a/Lib/python/pytypemaps.swg b/Lib/python/pytypemaps.swg index 48b0bcdc8..0eda17cda 100644 --- a/Lib/python/pytypemaps.swg +++ b/Lib/python/pytypemaps.swg @@ -80,7 +80,7 @@ /* Consttab, needed for callbacks, it should be removed later */ %typemap(consttab) SWIGTYPE ((*)(ANY)) -{ SWIG_PY_POINTER, (char*)"$symname", 0, 0, (void *)($value), &$descriptor } +{ SWIG_PY_POINTER, "$symname", 0, 0, (void *)($value), &$descriptor } %typemap(consttab) SWIGTYPE ((* const)(ANY)) = SWIGTYPE ((*)(ANY)); %typemap(constcode) SWIGTYPE ((*)(ANY)) ""; diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index cd993d6fd..541115773 100755 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -2437,10 +2437,10 @@ public: Append(methods, "NULL"); } else if (have_docstring(n)) { String *ds = cdocstring(n, AUTODOC_FUNC); - Printf(methods, "(char *)\"%s\"", ds); + Printf(methods, "\"%s\"", ds); Delete(ds); } else if (Getattr(n, "feature:callback")) { - Printf(methods, "(char *)\"swig_ptr: %s\"", Getattr(n, "feature:callback:name")); + Printf(methods, "\"swig_ptr: %s\"", Getattr(n, "feature:callback:name")); } else { Append(methods, "NULL"); } @@ -2512,7 +2512,7 @@ public: Append(f->code, "argc++;\n"); } else { String *iname = Getattr(n, "sym:name"); - Printf(f->code, "if (!(argc = SWIG_Python_UnpackTuple(args,\"%s\",0,%d,argv%s))) SWIG_fail;\n", iname, maxargs, add_self ? "+1" : ""); + Printf(f->code, "if (!(argc = SWIG_Python_UnpackTuple(args, \"%s\", 0, %d, argv%s))) SWIG_fail;\n", iname, maxargs, add_self ? "+1" : ""); if (add_self) Append(f->code, "argv[0] = self;\n"); else @@ -2722,10 +2722,10 @@ public: } if (!builtin || !in_class || tuple_arguments > 0) { if (!allow_kwargs) { - Append(parse_args, " if (!PyArg_ParseTuple(args,(char *)\""); + Append(parse_args, " if (!PyArg_ParseTuple(args, \""); } else { - Append(parse_args, " if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)\""); - Append(arglist, ",kwnames"); + Append(parse_args, " if (!PyArg_ParseTupleAndKeywords(args, kwargs, \""); + Append(arglist, ", kwnames"); } } @@ -2815,7 +2815,7 @@ public: sprintf(source, "obj%d", builtin_ctor ? i + 1 : i); if (parse_from_tuple) { - Putc(',', arglist); + Printf(arglist, ", "); if (i == num_required) Putc('|', parse_args); /* Optional argument separator */ } @@ -2823,7 +2823,7 @@ public: /* Keyword argument handling */ if (allow_kwargs && parse_from_tuple) { String *name = makeParameterName(n, p, i + 1); - Printf(kwargs, "(char *) \"%s\",", name); + Printf(kwargs, " (char *)\"%s\", ", name); Delete(name); } @@ -2888,7 +2888,7 @@ public: /* finish argument marshalling */ Append(kwargs, " NULL }"); if (allow_kwargs) { - Printv(f->locals, " char * kwnames[] = ", kwargs, ";\n", NIL); + Printv(f->locals, " char * kwnames[] = ", kwargs, ";\n", NIL); } if (builtin && !funpack && in_class && tuple_arguments == 0) { @@ -2915,13 +2915,13 @@ public: Printf(parse_args, "if (!args) SWIG_fail;\n"); Append(parse_args, "swig_obj[0] = args;\n"); } else if (!noargs) { - Printf(parse_args, "if (!SWIG_Python_UnpackTuple(args,\"%s\",%d,%d,swig_obj)) SWIG_fail;\n", iname, num_fixed_arguments, tuple_arguments); + Printf(parse_args, "if (!SWIG_Python_UnpackTuple(args, \"%s\", %d, %d, swig_obj)) SWIG_fail;\n", iname, num_fixed_arguments, tuple_arguments); } else if (noargs) { - Printf(parse_args, "if (!SWIG_Python_UnpackTuple(args,\"%s\",%d,%d,0)) SWIG_fail;\n", iname, num_fixed_arguments, tuple_arguments); + Printf(parse_args, "if (!SWIG_Python_UnpackTuple(args, \"%s\", %d, %d, 0)) SWIG_fail;\n", iname, num_fixed_arguments, tuple_arguments); } } } else { - Printf(parse_args, "if (!PyArg_UnpackTuple(args,(char *)\"%s\",%d,%d", iname, num_fixed_arguments, tuple_arguments); + Printf(parse_args, "if (!PyArg_UnpackTuple(args, \"%s\", %d, %d", iname, num_fixed_arguments, tuple_arguments); Printv(parse_args, arglist, ")) SWIG_fail;\n", NIL); } } @@ -3381,7 +3381,7 @@ public: Python dictionary. */ if (!have_globals) { - Printf(f_init, "\t PyDict_SetItemString(md,(char *)\"%s\", SWIG_globals());\n", global_name); + Printf(f_init, "\t PyDict_SetItemString(md, \"%s\", SWIG_globals());\n", global_name); if (builtin) Printf(f_init, "\t SwigPyBuiltin_AddPublicSymbol(public_interface, \"%s\");\n", global_name); have_globals = 1; @@ -3469,9 +3469,9 @@ public: Wrapper_print(getf, f_wrappers); /* Now add this to the variable linking mechanism */ - Printf(f_init, "\t SWIG_addvarlink(SWIG_globals(),(char *)\"%s\",%s, %s);\n", iname, vargetname, varsetname); + Printf(f_init, "\t SWIG_addvarlink(SWIG_globals(), \"%s\", %s, %s);\n", iname, vargetname, varsetname); if (builtin && shadow && !assignable && !in_class) { - Printf(f_init, "\t PyDict_SetItemString(md, (char *)\"%s\", PyObject_GetAttrString(SWIG_globals(), \"%s\"));\n", iname, iname); + Printf(f_init, "\t PyDict_SetItemString(md, \"%s\", PyObject_GetAttrString(SWIG_globals(), \"%s\"));\n", iname, iname); Printf(f_init, "\t SwigPyBuiltin_AddPublicSymbol(public_interface, \"%s\");\n", iname); } Delete(vargetname); @@ -3564,9 +3564,9 @@ public: Printf(f_wrappers, tab2 "PyObject *module;\n", tm); Printf(f_wrappers, tab2 "PyObject *d;\n"); if (fastunpack) { - Printf(f_wrappers, tab2 "if (!SWIG_Python_UnpackTuple(args,(char *)\"swigconstant\", 1, 1,&module)) return NULL;\n"); + Printf(f_wrappers, tab2 "if (!SWIG_Python_UnpackTuple(args, \"swigconstant\", 1, 1, &module)) return NULL;\n"); } else { - Printf(f_wrappers, tab2 "if (!PyArg_UnpackTuple(args,(char *)\"swigconstant\", 1, 1,&module)) return NULL;\n"); + Printf(f_wrappers, tab2 "if (!PyArg_UnpackTuple(args, \"swigconstant\", 1, 1, &module)) return NULL;\n"); } Printf(f_wrappers, tab2 "d = PyModule_GetDict(module);\n"); Printf(f_wrappers, tab2 "if (!d) return NULL;\n"); @@ -3969,7 +3969,7 @@ public: String *gspair = NewStringf("%s_%s_getset", symname, memname); Printf(f, "static SwigPyGetSet %s = { %s, %s };\n", gspair, getter ? getter : "0", setter ? setter : "0"); String *entry = - NewStringf("{ (char *) \"%s\", (getter) %s, (setter) %s, (char *)\"%s.%s\", (void *) &%s }\n", memname, getter_closure, + NewStringf("{ (char *)\"%s\", (getter)%s, (setter)%s, (char *)\"%s.%s\", (void *)&%s }\n", memname, getter_closure, setter_closure, name, memname, gspair); if (GetFlag(mgetset, "static")) { Printf(f, "static PyGetSetDef %s_def = %s;\n", gspair, entry); @@ -4481,9 +4481,9 @@ public: Printv(f_wrappers, "SWIGINTERN PyObject *", class_name, "_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {\n", NIL); Printv(f_wrappers, " PyObject *obj;\n", NIL); if (fastunpack) { - Printv(f_wrappers, " if (!SWIG_Python_UnpackTuple(args,(char *)\"swigregister\", 1, 1,&obj)) return NULL;\n", NIL); + Printv(f_wrappers, " if (!SWIG_Python_UnpackTuple(args, \"swigregister\", 1, 1, &obj)) return NULL;\n", NIL); } else { - Printv(f_wrappers, " if (!PyArg_UnpackTuple(args,(char *)\"swigregister\", 1, 1,&obj)) return NULL;\n", NIL); + Printv(f_wrappers, " if (!PyArg_UnpackTuple(args, \"swigregister\", 1, 1, &obj)) return NULL;\n", NIL); } Printv(f_wrappers, @@ -5018,7 +5018,7 @@ public: Wrapper_add_local(f, "res", "int res"); if (!funpack) { Wrapper_add_local(f, "value", "PyObject *value"); - Append(f->code, "if (!PyArg_ParseTuple(args,(char *)\"O:set\",&value)) return NULL;\n"); + Append(f->code, "if (!PyArg_ParseTuple(args, \"O:set\", &value)) return NULL;\n"); } Printf(f->code, "res = %s(%s);\n", varsetname, funpack ? "args" : "value"); Append(f->code, "return !res ? SWIG_Py_Void() : NULL;\n"); @@ -5492,11 +5492,11 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { if (use_parse) { Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_CallMethod(swig_get_self(), (char *)\"%s\", (char *)\"(%s)\" %s);\n", Swig_cresult_name(), pyname, parse_args, arglist); } else { - Printf(w->code, "swig::SwigVar_PyObject swig_method_name = SWIG_Python_str_FromChar((char *)\"%s\");\n", pyname); + Printf(w->code, "swig::SwigVar_PyObject swig_method_name = SWIG_Python_str_FromChar(\"%s\");\n", pyname); Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_CallMethodObjArgs(swig_get_self(), (PyObject *) swig_method_name %s, NULL);\n", Swig_cresult_name(), arglist); } } else { - Printf(w->code, "swig::SwigVar_PyObject swig_method_name = SWIG_Python_str_FromChar((char *)\"%s\");\n", pyname); + Printf(w->code, "swig::SwigVar_PyObject swig_method_name = SWIG_Python_str_FromChar(\"%s\");\n", pyname); Printf(w->code, "swig::SwigVar_PyObject %s = PyObject_CallMethodObjArgs(swig_get_self(), (PyObject *) swig_method_name, NULL);\n", Swig_cresult_name()); } Append(w->code, "#endif\n");