[Python] Sort out handling of self parameter
When not using -builtin, the self parameter is now still made available so that user typemaps can use it. Fixes #967. When using -builtin, fix -Wunused-parameter warnings in the generated wrapper code. See #801. Based on a commit peeled out of #801 by teythoon.
This commit is contained in:
parent
b77c0d02bc
commit
7cb2f46e06
2 changed files with 25 additions and 9 deletions
|
|
@ -7,6 +7,12 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
|
||||||
Version 4.1.0 (in progress)
|
Version 4.1.0 (in progress)
|
||||||
===========================
|
===========================
|
||||||
|
|
||||||
|
2022-02-03: olly
|
||||||
|
[Python] #967 Make `self` parameter available to user typemaps.
|
||||||
|
|
||||||
|
2022-02-03: teythoon
|
||||||
|
[Python] #801 Fix -Wunused-parameter warnings with builtin,
|
||||||
|
|
||||||
2022-02-03: teythoon
|
2022-02-03: teythoon
|
||||||
#801 Fix -Wstrict-prototypes warnings in generated pointer
|
#801 Fix -Wstrict-prototypes warnings in generated pointer
|
||||||
functions.
|
functions.
|
||||||
|
|
|
||||||
|
|
@ -2545,6 +2545,11 @@ public:
|
||||||
const char *builtin_kwargs = builtin_ctor ? ", PyObject *kwargs" : "";
|
const char *builtin_kwargs = builtin_ctor ? ", PyObject *kwargs" : "";
|
||||||
Printv(f->def, linkage, builtin_ctor ? "int " : "PyObject *", wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL);
|
Printv(f->def, linkage, builtin_ctor ? "int " : "PyObject *", wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL);
|
||||||
|
|
||||||
|
if (builtin) {
|
||||||
|
/* Avoid warning if the self parameter is not used. */
|
||||||
|
Append(f->code, "(void)self;\n");
|
||||||
|
}
|
||||||
|
|
||||||
Wrapper_add_local(f, "argc", "Py_ssize_t argc");
|
Wrapper_add_local(f, "argc", "Py_ssize_t argc");
|
||||||
Printf(tmp, "PyObject *argv[%d] = {0}", maxargs + 1);
|
Printf(tmp, "PyObject *argv[%d] = {0}", maxargs + 1);
|
||||||
Wrapper_add_local(f, "argv", tmp);
|
Wrapper_add_local(f, "argv", tmp);
|
||||||
|
|
@ -2714,7 +2719,6 @@ public:
|
||||||
bool add_self = builtin_self && (!builtin_ctor || director_class);
|
bool add_self = builtin_self && (!builtin_ctor || director_class);
|
||||||
bool builtin_getter = (builtin && GetFlag(n, "memberget"));
|
bool builtin_getter = (builtin && GetFlag(n, "memberget"));
|
||||||
bool builtin_setter = (builtin && GetFlag(n, "memberset") && !builtin_getter);
|
bool builtin_setter = (builtin && GetFlag(n, "memberset") && !builtin_getter);
|
||||||
char const *self_param = builtin ? "self" : "SWIGUNUSEDPARM(self)";
|
|
||||||
char const *wrap_return = builtin_ctor ? "int " : "PyObject *";
|
char const *wrap_return = builtin_ctor ? "int " : "PyObject *";
|
||||||
String *linkage = NewString("SWIGINTERN ");
|
String *linkage = NewString("SWIGINTERN ");
|
||||||
String *wrapper_name = Swig_name_wrapper(iname);
|
String *wrapper_name = Swig_name_wrapper(iname);
|
||||||
|
|
@ -2769,9 +2773,9 @@ public:
|
||||||
const char *builtin_kwargs = builtin_ctor ? ", PyObject *kwargs" : "";
|
const char *builtin_kwargs = builtin_ctor ? ", PyObject *kwargs" : "";
|
||||||
if (!allow_kwargs || overname) {
|
if (!allow_kwargs || overname) {
|
||||||
if (!varargs) {
|
if (!varargs) {
|
||||||
Printv(f->def, linkage, wrap_return, wname, "(PyObject *", self_param, ", PyObject *args", builtin_kwargs, ") {", NIL);
|
Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL);
|
||||||
} else {
|
} else {
|
||||||
Printv(f->def, linkage, wrap_return, wname, "__varargs__", "(PyObject *", self_param, ", PyObject *args, PyObject *varargs", builtin_kwargs, ") {", NIL);
|
Printv(f->def, linkage, wrap_return, wname, "__varargs__", "(PyObject *self, PyObject *args, PyObject *varargs", builtin_kwargs, ") {", NIL);
|
||||||
}
|
}
|
||||||
if (allow_kwargs) {
|
if (allow_kwargs) {
|
||||||
Swig_warning(WARN_LANG_OVERLOAD_KEYWORD, input_file, line_number, "Can't use keyword arguments with overloaded functions (%s).\n", Swig_name_decl(n));
|
Swig_warning(WARN_LANG_OVERLOAD_KEYWORD, input_file, line_number, "Can't use keyword arguments with overloaded functions (%s).\n", Swig_name_decl(n));
|
||||||
|
|
@ -2782,8 +2786,14 @@ public:
|
||||||
Swig_warning(WARN_LANG_VARARGS_KEYWORD, input_file, line_number, "Can't wrap varargs with keyword arguments enabled\n");
|
Swig_warning(WARN_LANG_VARARGS_KEYWORD, input_file, line_number, "Can't wrap varargs with keyword arguments enabled\n");
|
||||||
varargs = 0;
|
varargs = 0;
|
||||||
}
|
}
|
||||||
Printv(f->def, linkage, wrap_return, wname, "(PyObject *", self_param, ", PyObject *args, PyObject *kwargs) {", NIL);
|
Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, PyObject *args, PyObject *kwargs) {", NIL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (builtin) {
|
||||||
|
/* Avoid warning if the self parameter is not used. */
|
||||||
|
Append(f->code, "(void)self;\n");
|
||||||
|
}
|
||||||
|
|
||||||
if (!builtin || !in_class || tuple_arguments > 0 || builtin_ctor) {
|
if (!builtin || !in_class || tuple_arguments > 0 || builtin_ctor) {
|
||||||
if (!allow_kwargs) {
|
if (!allow_kwargs) {
|
||||||
Append(parse_args, " if (!PyArg_ParseTuple(args, \"");
|
Append(parse_args, " if (!PyArg_ParseTuple(args, \"");
|
||||||
|
|
@ -2942,14 +2952,14 @@ public:
|
||||||
Clear(f->def);
|
Clear(f->def);
|
||||||
if (overname) {
|
if (overname) {
|
||||||
if (noargs) {
|
if (noargs) {
|
||||||
Printv(f->def, linkage, wrap_return, wname, "(PyObject *", self_param, ", Py_ssize_t nobjs, PyObject **SWIGUNUSEDPARM(swig_obj)) {", NIL);
|
Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, Py_ssize_t nobjs, PyObject **SWIGUNUSEDPARM(swig_obj)) {", NIL);
|
||||||
} else {
|
} else {
|
||||||
Printv(f->def, linkage, wrap_return, wname, "(PyObject *", self_param, ", Py_ssize_t nobjs, PyObject **swig_obj) {", NIL);
|
Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, Py_ssize_t nobjs, PyObject **swig_obj) {", NIL);
|
||||||
}
|
}
|
||||||
Printf(parse_args, "if ((nobjs < %d) || (nobjs > %d)) SWIG_fail;\n", num_required, num_arguments);
|
Printf(parse_args, "if ((nobjs < %d) || (nobjs > %d)) SWIG_fail;\n", num_required, num_arguments);
|
||||||
} else {
|
} else {
|
||||||
int is_tp_call = Equal(Getattr(n, "feature:python:slot"), "tp_call");
|
int is_tp_call = Equal(Getattr(n, "feature:python:slot"), "tp_call");
|
||||||
Printv(f->def, linkage, wrap_return, wname, "(PyObject *", self_param, ", PyObject *args", builtin_kwargs, ") {", NIL);
|
Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL);
|
||||||
if (builtin_ctor)
|
if (builtin_ctor)
|
||||||
Printf(parse_args, "if (!SWIG_Python_CheckNoKeywords(kwargs, \"%s\")) SWIG_fail;\n", iname);
|
Printf(parse_args, "if (!SWIG_Python_CheckNoKeywords(kwargs, \"%s\")) SWIG_fail;\n", iname);
|
||||||
if (onearg && !builtin_ctor && !is_tp_call) {
|
if (onearg && !builtin_ctor && !is_tp_call) {
|
||||||
|
|
@ -3241,9 +3251,9 @@ public:
|
||||||
f = NewWrapper();
|
f = NewWrapper();
|
||||||
if (funpack) {
|
if (funpack) {
|
||||||
// Note: funpack is currently always false for varargs
|
// Note: funpack is currently always false for varargs
|
||||||
Printv(f->def, linkage, wrap_return, wname, "(PyObject *", self_param, ", Py_ssize_t nobjs, PyObject **swig_obj) {", NIL);
|
Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, Py_ssize_t nobjs, PyObject **swig_obj) {", NIL);
|
||||||
} else {
|
} else {
|
||||||
Printv(f->def, linkage, wrap_return, wname, "(PyObject *", self_param, ", PyObject *args", builtin_kwargs, ") {", NIL);
|
Printv(f->def, linkage, wrap_return, wname, "(PyObject *self, PyObject *args", builtin_kwargs, ") {", NIL);
|
||||||
}
|
}
|
||||||
Wrapper_add_local(f, "resultobj", builtin_ctor ? "int resultobj" : "PyObject *resultobj");
|
Wrapper_add_local(f, "resultobj", builtin_ctor ? "int resultobj" : "PyObject *resultobj");
|
||||||
Wrapper_add_local(f, "varargs", "PyObject *varargs");
|
Wrapper_add_local(f, "varargs", "PyObject *varargs");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue