Skip to content

Commit

Permalink
[Python] Fix new GCC8 warnings in generated code
Browse files Browse the repository at this point in the history
Avoid casts between incompatible function types where possible (when
keyword args are in use, it is not possible to avoid such warnings as
they are inherent in the design of Python's C API in that particular
case).  Fixes #1259.
  • Loading branch information
ojwb committed Jun 11, 2018
1 parent a539d9e commit 7f98830
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
7 changes: 7 additions & 0 deletions CHANGES.current
Expand Up @@ -7,6 +7,13 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.0.0 (in progress)
===========================

2018-06-11: olly
[Python] Fix new GCC8 warnings in generated code by avoiding casts
between incompatible function types where possible (when keyword
args are in use, it is not possible to avoid such warnings as they
are inherent in the design of Python's C API in that particular
case). Fixes #1259.

2018-06-08: philippkraft
[Python] Stop exposing <CLASS>_swigregister to Python. It's not
useful for user Python code to call this, and it just clutters the
Expand Down
4 changes: 2 additions & 2 deletions Lib/python/pyinit.swg
Expand Up @@ -347,8 +347,8 @@ SWIG_init(void) {
(char *)"this", &SwigPyBuiltin_ThisClosure, NULL, NULL, NULL
};
static SwigPyGetSet thisown_getset_closure = {
(PyCFunction) SwigPyObject_own,
(PyCFunction) SwigPyObject_own
SwigPyObject_own,
SwigPyObject_own
};
static PyGetSetDef thisown_getset_def = {
(char *)"thisown", SwigPyBuiltin_GetterClosure, SwigPyBuiltin_SetterClosure, NULL, &thisown_getset_closure
Expand Down
20 changes: 14 additions & 6 deletions Lib/python/pyrun.swg
Expand Up @@ -445,6 +445,14 @@ SwigPyObject_repr(SwigPyObject *v)
return repr;
}

/* We need a version taking two PyObject* parameters so it's a valid
* PyCFunction to use in swigobject_methods[]. */
static PyObject *
SwigPyObject_repr2(PyObject *v, PyObject *SWIGUNUSEDPARM(args))
{
return SwigPyObject_repr((SwigPyObject*)v);
}

SWIGRUNTIME int
SwigPyObject_compare(SwigPyObject *v, SwigPyObject *w)
{
Expand Down Expand Up @@ -621,12 +629,12 @@ SwigPyObject_own(PyObject *v, PyObject *args)

static PyMethodDef
swigobject_methods[] = {
{"disown", (PyCFunction)SwigPyObject_disown, METH_NOARGS, "releases ownership of the pointer"},
{"acquire", (PyCFunction)SwigPyObject_acquire, METH_NOARGS, "acquires ownership of the pointer"},
{"own", (PyCFunction)SwigPyObject_own, METH_VARARGS, "returns/sets ownership of the pointer"},
{"append", (PyCFunction)SwigPyObject_append, METH_O, "appends another 'this' object"},
{"next", (PyCFunction)SwigPyObject_next, METH_NOARGS, "returns the next 'this' object"},
{"__repr__",(PyCFunction)SwigPyObject_repr, METH_NOARGS, "returns object representation"},
{"disown", SwigPyObject_disown, METH_NOARGS, "releases ownership of the pointer"},
{"acquire", SwigPyObject_acquire, METH_NOARGS, "acquires ownership of the pointer"},
{"own", SwigPyObject_own, METH_VARARGS, "returns/sets ownership of the pointer"},
{"append", SwigPyObject_append, METH_O, "appends another 'this' object"},
{"next", SwigPyObject_next, METH_NOARGS, "returns the next 'this' object"},
{"__repr__",SwigPyObject_repr2, METH_NOARGS, "returns object representation"},
{0, 0, 0, 0}
};

Expand Down
26 changes: 13 additions & 13 deletions Source/Modules/python.cxx
Expand Up @@ -1090,7 +1090,7 @@ class PYTHON:public Language {
* ------------------------------------------------------------ */
int add_pyinstancemethod_new() {
String *name = NewString("SWIG_PyInstanceMethod_New");
Printf(methods, "\t { (char *)\"%s\", (PyCFunction)%s, METH_O, NULL},\n", name, name);
Printf(methods, "\t { \"%s\", %s, METH_O, NULL},\n", name, name);
Delete(name);
return 0;
}
Expand Down Expand Up @@ -2507,17 +2507,17 @@ class PYTHON:public Language {
if (!kw) {
if (n && funpack) {
if (num_required == 0 && num_arguments == 0) {
Printf(methods, "\t { (char *)\"%s\", (PyCFunction)%s, METH_NOARGS, ", name, function);
Printf(methods, "\t { \"%s\", %s, METH_NOARGS, ", name, function);
} else if (num_required == 1 && num_arguments == 1) {
Printf(methods, "\t { (char *)\"%s\", (PyCFunction)%s, METH_O, ", name, function);
Printf(methods, "\t { \"%s\", %s, METH_O, ", name, function);
} else {
Printf(methods, "\t { (char *)\"%s\", %s, METH_VARARGS, ", name, function);
Printf(methods, "\t { \"%s\", %s, METH_VARARGS, ", name, function);
}
} else {
Printf(methods, "\t { (char *)\"%s\", %s, METH_VARARGS, ", name, function);
Printf(methods, "\t { \"%s\", %s, METH_VARARGS, ", name, function);
}
} else {
Printf(methods, "\t { (char *)\"%s\", (PyCFunction) %s, METH_VARARGS | METH_KEYWORDS, ", name, function);
Printf(methods, "\t { \"%s\", (PyCFunction)%s, METH_VARARGS|METH_KEYWORDS, ", name, function);
}

if (!n) {
Expand Down Expand Up @@ -3898,7 +3898,7 @@ class PYTHON:public Language {
if (shadow) {
if (builtin) {
String *rname = SwigType_namestr(real_classname);
Printf(builtin_methods, " { \"__disown__\", (PyCFunction) Swig::Director::swig_pyobj_disown< %s >, METH_NOARGS, \"\" },\n", rname);
Printf(builtin_methods, " { \"__disown__\", Swig::Director::swig_pyobj_disown< %s >, METH_NOARGS, \"\" },\n", rname);
Delete(rname);
} else {
String *symname = Getattr(n, "sym:name");
Expand Down Expand Up @@ -4738,13 +4738,13 @@ class PYTHON:public Language {
int argcount = Getattr(n, "python:argcount") ? atoi(Char(Getattr(n, "python:argcount"))) : 2;
String *ds = have_docstring(n) ? cdocstring(n, AUTODOC_METHOD) : NewString("");
if (check_kwargs(n)) {
Printf(builtin_methods, " { \"%s\", (PyCFunction) %s, METH_VARARGS|METH_KEYWORDS, (char *) \"%s\" },\n", symname, wname, ds);
Printf(builtin_methods, " { \"%s\", (PyCFunction)%s, METH_VARARGS|METH_KEYWORDS, \"%s\" },\n", symname, wname, ds);
} else if (argcount == 0) {
Printf(builtin_methods, " { \"%s\", (PyCFunction) %s, METH_NOARGS, (char *) \"%s\" },\n", symname, wname, ds);
Printf(builtin_methods, " { \"%s\", %s, METH_NOARGS, \"%s\" },\n", symname, wname, ds);
} else if (argcount == 1) {
Printf(builtin_methods, " { \"%s\", (PyCFunction) %s, METH_O, (char *) \"%s\" },\n", symname, wname, ds);
Printf(builtin_methods, " { \"%s\", %s, METH_O, \"%s\" },\n", symname, wname, ds);
} else {
Printf(builtin_methods, " { \"%s\", (PyCFunction) %s, METH_VARARGS, (char *) \"%s\" },\n", symname, wname, ds);
Printf(builtin_methods, " { \"%s\", %s, METH_VARARGS, \"%s\" },\n", symname, wname, ds);
}
Delete(fullname);
Delete(wname);
Expand Down Expand Up @@ -4845,10 +4845,10 @@ class PYTHON:public Language {
Append(pyflags, "METH_VARARGS");
if (have_docstring(n)) {
String *ds = cdocstring(n, AUTODOC_STATICFUNC);
Printf(builtin_methods, " { \"%s\", (PyCFunction) %s, %s, (char *) \"%s\" },\n", symname, wname, pyflags, ds);
Printf(builtin_methods, " { \"%s\", (PyCFunction)%s, %s, \"%s\" },\n", symname, wname, pyflags, ds);
Delete(ds);
} else {
Printf(builtin_methods, " { \"%s\", (PyCFunction) %s, %s, \"\" },\n", symname, wname, pyflags);
Printf(builtin_methods, " { \"%s\", (PyCFunction)%s, %s, \"\" },\n", symname, wname, pyflags);
}
Delete(fullname);
Delete(wname);
Expand Down

0 comments on commit 7f98830

Please sign in to comment.