Python: Option to generate flat class methods

This commit is contained in:
Julien Schueller 2021-12-31 09:50:17 +01:00
commit 484e5316f2
3 changed files with 14 additions and 9 deletions

View file

@ -957,6 +957,7 @@ swig -python -help
<tr><td>-doxygen </td><td>Convert C++ doxygen comments to pydoc comments in proxy classes</td></tr>
<tr><td>-extranative </td><td>Return extra native wrappers for C++ std containers wherever possible</td></tr>
<tr><td>-fastproxy </td><td>Use fast proxy mechanism for member methods</td></tr>
<tr><td>-flatstaticmethod </td><td>Generate Foo_bar for static method Foo::bar</td></tr>
<tr><td>-globals &lt;name&gt; </td><td>Set &lt;name&gt; used to access C global variable (default: 'cvar')</td></tr>
<tr><td>-interface &lt;mod&gt;</td><td>Set low-level C/C++ module name to &lt;mod&gt; (default: module name prefixed by '_')</td></tr>
<tr><td>-keyword </td><td>Use keyword arguments</td></tr>
@ -1616,16 +1617,15 @@ In Python, the static member can be accessed in three different ways:
<div class="targetlang">
<pre>
&gt;&gt;&gt; example.Spam_foo() # Spam::foo()
&gt;&gt;&gt; s = example.Spam()
&gt;&gt;&gt; s.foo() # Spam::foo() via an instance
&gt;&gt;&gt; example.Spam.foo() # Spam::foo() using Python-2.2 and later
&gt;&gt;&gt; example.Spam.foo() # Spam::foo() using class method
&gt;&gt;&gt; example.Spam_foo() # Spam::foo() "flattened" name
</pre>
</div>
<p>
The first two methods of access are supported in all versions of Python. The
last technique is only available in Python-2.2 and later versions.
The last technique is only available when using the <tt>-flatstaticmethod</tt> option.
</p>
<p>

View file

@ -100,7 +100,7 @@ LIBS = -L.
VALGRIND_OPT += --suppressions=pythonswig.supp
# Custom tests - tests with additional commandline options
#python_flatstaticmethod.cpptest: SWIGOPT += -flatstaticmethod
python_flatstaticmethod.cpptest: SWIGOPT += -flatstaticmethod
# Rules for the different types of tests
%.cpptest:

View file

@ -93,6 +93,7 @@ static int castmode = 0;
static int extranative = 0;
static int nortti = 0;
static int relativeimport = 0;
static int flat_static_method = 0;
/* flags for the make_autodoc function */
namespace {
@ -118,6 +119,7 @@ Python Options (available with -python)\n\
-doxygen - Convert C++ doxygen comments to pydoc comments in proxy classes\n\
-extranative - Return extra native wrappers for C++ std containers wherever possible\n\
-fastproxy - Use fast proxy mechanism for member methods\n\
-flatstaticmethod - Generate Foo_bar for static method Foo::bar\n\
-globals <name> - Set <name> used to access C global variable (default: 'cvar')\n\
-interface <mod>- Set low-level C/C++ module name to <mod> (default: module name prefixed by '_')\n\
-keyword - Use keyword arguments\n";
@ -373,6 +375,9 @@ public:
} else if (strcmp(argv[i], "-extranative") == 0) {
extranative = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-flatstaticmethod") == 0) {
flat_static_method = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-noh") == 0) {
no_header_file = 1;
Swig_mark_arg(i);
@ -2636,11 +2641,11 @@ public:
Printv(f->code, "}\n", NIL);
Wrapper_print(f, f_wrappers);
Node *p = Getattr(n, "sym:previousSibling");
if (!builtin_self)
if (!builtin_self && (flat_static_method || !in_class || !builtin))
add_method(symname, wname, 0, p);
/* Create a shadow for this function (if enabled and not in a member function) */
if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER))) {
if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER)) && (flat_static_method || !in_class)) {
emitFunctionShadowHelper(n, in_class ? f_shadow_stubs : f_shadow, symname, 0);
}
DelWrapper(f);
@ -3309,11 +3314,11 @@ public:
/* Now register the function with the interpreter. */
if (!Getattr(n, "sym:overloaded")) {
if (!builtin_self)
if (!builtin_self && (flat_static_method || !in_class || !builtin))
add_method(iname, wname, allow_kwargs, n, funpack, num_required, num_arguments);
/* Create a shadow for this function (if enabled and not in a member function) */
if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER))) {
if (!builtin && (shadow) && (!(shadow & PYSHADOW_MEMBER)) && (flat_static_method || !in_class)) {
emitFunctionShadowHelper(n, in_class ? f_shadow_stubs : f_shadow, iname, allow_kwargs);
}
} else {