[Python] Improve handling of SWIG_Py*Method_New

The SWIG_PyInstanceMethod_New method is no longer added to wrapped
classes except when it's actually needed, which is when
(!builtin && fastproxy) is true (which it isn't by default).

The SWIG_PyStaticMethod_New method is no longer is now similarly
gated - previously only (fastproxy) was checked.

Finally the C/C++ functions that implement these were always compiled
into the module, but now they're only included if
(!builtin && fastproxy) is true.

Issue noted by vadz in #2190.
This commit is contained in:
Olly Betts 2022-02-04 11:32:01 +13:00 • committed by Olly Betts
commit 951f946341
2 changed files with 24 additions and 8 deletions

View file

@ -8,6 +8,8 @@
%fragment("<stddef.h>"); // For offsetof %fragment("<stddef.h>"); // For offsetof
#endif #endif
#if defined SWIGPYTHON_FASTPROXY && !defined SWIGPYTHON_BUILTIN
%insert(runtime) %{ %insert(runtime) %{
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -24,6 +26,8 @@ SWIGINTERN PyObject *SWIG_PyStaticMethod_New(PyObject *SWIGUNUSEDPARM(self), PyO
#endif #endif
%} %}
#endif
%init %{ %init %{
#ifdef __cplusplus #ifdef __cplusplus
@ -103,6 +107,8 @@ SWIG_Python_FixMethods(PyMethodDef *methods, const swig_const_info *const_table,
} }
} }
#if defined SWIGPYTHON_FASTPROXY && !defined SWIGPYTHON_BUILTIN
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* Method creation and docstring support functions * Method creation and docstring support functions
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
@ -161,6 +167,8 @@ SWIGINTERN PyObject *SWIG_PyStaticMethod_New(PyObject *SWIGUNUSEDPARM(self), PyO
return PyStaticMethod_New(func); return PyStaticMethod_New(func);
} }
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -452,6 +452,10 @@ public:
SWIG_exit(EXIT_FAILURE); SWIG_exit(EXIT_FAILURE);
} }
if (fastproxy) {
Preprocessor_define("SWIGPYTHON_FASTPROXY", 0);
}
if (doxygen) if (doxygen)
doxygenTranslator = new PyDocConverter(doxygen_translator_flags); doxygenTranslator = new PyDocConverter(doxygen_translator_flags);
@ -619,6 +623,10 @@ public:
Printf(f_runtime, "#define SWIGPYTHON_BUILTIN\n"); Printf(f_runtime, "#define SWIGPYTHON_BUILTIN\n");
} }
if (fastproxy) {
Printf(f_runtime, "#define SWIGPYTHON_FASTPROXY\n");
}
Printf(f_runtime, "\n"); Printf(f_runtime, "\n");
Printf(f_header, "#ifdef SWIG_TypeQuery\n"); Printf(f_header, "#ifdef SWIG_TypeQuery\n");
@ -922,15 +930,15 @@ public:
* as a replacement of new.instancemethod in Python 3. * as a replacement of new.instancemethod in Python 3.
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
int add_pyinstancemethod_new() { int add_pyinstancemethod_new() {
String *name = NewString("SWIG_PyInstanceMethod_New"); if (!builtin && fastproxy) {
String *line = NewString(""); String *name = NewString("SWIG_PyInstanceMethod_New");
Printf(line, "\t { \"%s\", %s, METH_O, NULL},\n", name, name); String *line = NewString("");
Append(methods, line); Printf(line, "\t { \"%s\", %s, METH_O, NULL},\n", name, name);
if (fastproxy) { Append(methods, line);
Append(methods_proxydocs, line); Append(methods_proxydocs, line);
Delete(line);
Delete(name);
} }
Delete(line);
Delete(name);
return 0; return 0;
} }
@ -940,7 +948,7 @@ public:
* generated for static methods when using -fastproxy * generated for static methods when using -fastproxy
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
int add_pystaticmethod_new() { int add_pystaticmethod_new() {
if (fastproxy) { if (!builtin && fastproxy) {
String *name = NewString("SWIG_PyStaticMethod_New"); String *name = NewString("SWIG_PyStaticMethod_New");
String *line = NewString(""); String *line = NewString("");
Printf(line, "\t { \"%s\", %s, METH_O, NULL},\n", name, name); Printf(line, "\t { \"%s\", %s, METH_O, NULL},\n", name, name);