/trunk/Source/Modules/python.cxx
C++ | 1956 lines | 1621 code | 166 blank | 169 comment | 380 complexity | 115ecb1a1c01caaf3a190e19b5fad57d MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- /* -----------------------------------------------------------------------------
- * This file is part of SWIG, which is licensed as a whole under version 3
- * (or any later version) of the GNU General Public License. Some additional
- * terms also apply to certain portions of SWIG. The full details of the SWIG
- * license and copyrights can be found in the LICENSE and COPYRIGHT files
- * included with the SWIG source code as distributed by the SWIG developers
- * and at http://www.swig.org/legal.html.
- *
- * python.cxx
- *
- * Python language module for SWIG.
- * ----------------------------------------------------------------------------- */
- char cvsroot_python_cxx[] = "$Id: python.cxx 12877 2011-12-20 20:50:36Z wsfulton $";
- #include "swigmod.h"
- #include "cparse.h"
- static int treduce = SWIG_cparse_template_reduce(0);
- #include <ctype.h>
- #define PYSHADOW_MEMBER 0x2
- #define WARN_PYTHON_MULTIPLE_INH 405
- static String *const_code = 0;
- static String *module = 0;
- static String *package = 0;
- static String *mainmodule = 0;
- static String *interface = 0;
- static String *global_name = 0;
- static int shadow = 1;
- static int use_kw = 0;
- static int director_method_index = 0;
- static int builtin = 0;
- static File *f_begin = 0;
- static File *f_runtime = 0;
- static File *f_runtime_h = 0;
- static File *f_header = 0;
- static File *f_wrappers = 0;
- static File *f_directors = 0;
- static File *f_directors_h = 0;
- static File *f_init = 0;
- static File *f_shadow_py = 0;
- static String *f_shadow = 0;
- static Hash *f_shadow_imports = 0;
- static String *f_shadow_builtin_imports = 0;
- static String *f_shadow_stubs = 0;
- static Hash *builtin_getset = 0;
- static Hash *class_members = 0;
- static File *f_builtins = 0;
- static String *builtin_tp_init = 0;
- static String *builtin_methods = 0;
- static String *builtin_default_unref = 0;
- static String *methods;
- static String *class_name;
- static String *shadow_indent = 0;
- static int in_class = 0;
- static int classic = 0;
- static int modern = 0;
- static int new_repr = 1;
- static int no_header_file = 0;
- static int max_bases = 0;
- static int builtin_bases_needed = 0;
- static int py3 = 0;
- /* C++ Support + Shadow Classes */
- static int have_constructor;
- static int have_repr;
- static String *real_classname;
- /* Thread Support */
- static int threads = 0;
- static int nothreads = 0;
- static int classptr = 0;
- /* Other options */
- static int shadowimport = 1;
- static int buildnone = 0;
- static int nobuildnone = 0;
- static int safecstrings = 0;
- static int dirvtable = 0;
- static int proxydel = 1;
- static int fastunpack = 0;
- static int fastproxy = 0;
- static int fastquery = 0;
- static int fastinit = 0;
- static int olddefs = 0;
- static int modernargs = 0;
- static int aliasobj0 = 0;
- static int castmode = 0;
- static int extranative = 0;
- static int outputtuple = 0;
- static int nortti = 0;
- /* flags for the make_autodoc function */
- enum autodoc_t {
- AUTODOC_CLASS,
- AUTODOC_CTOR,
- AUTODOC_DTOR,
- AUTODOC_STATICFUNC,
- AUTODOC_FUNC,
- AUTODOC_METHOD
- };
- static const char *usage1 = (char *) "\
- Python Options (available with -python)\n\
- -aliasobj0 - Alias obj0 when using fastunpack, needed for some old typemaps \n\
- -buildnone - Use Py_BuildValue(" ") to obtain Py_None (default in Windows)\n\
- -builtin - Create new python built-in types, rather than proxy classes, for better performance\n\
- -castmode - Enable the casting mode, which allows implicit cast between types in python\n\
- -classic - Use classic classes only\n\
- -classptr - Generate shadow 'ClassPtr' as in older swig versions\n\
- -cppcast - Enable C++ casting operators (default) \n\
- -dirvtable - Generate a pseudo virtual table for directors for faster dispatch \n\
- -extranative - Return extra native C++ wraps for std containers when possible \n\
- -fastinit - Use fast init mechanism for classes (default)\n\
- -fastunpack - Use fast unpack mechanism to parse the argument functions \n\
- -fastproxy - Use fast proxy mechanism for member methods \n\
- -fastquery - Use fast query mechanism for types \n\
- -globals <name> - Set <name> used to access C global variable [default: 'cvar']\n\
- -interface <lib>- Set the lib name to <lib>\n\
- -keyword - Use keyword arguments\n\
- -modern - Use modern python features only, without compatibility code\n\
- -modernargs - Use \"modern\" args mechanism to pack/unpack the function arguments\n";
- static const char *usage2 = (char *) "\
- -newrepr - Use more informative version of __repr__ in proxy classes (default) \n\
- -newvwm - New value wrapper mode, use only when everything else fails \n\
- -noaliasobj0 - Don't generate an obj0 alias when using fastunpack (default) \n\
- -nobuildnone - Access Py_None directly (default in non-Windows systems)\n\
- -nocastmode - Disable the casting mode (default)\n\
- -nocppcast - Disable C++ casting operators, useful for generating bugs\n\
- -nodirvtable - Don't use the virtual table feature, resolve the python method each time (default)\n\
- -noexcept - No automatic exception handling\n\
- -noextranative - Don't use extra native C++ wraps for std containers when possible (default) \n\
- -nofastinit - Use traditional init mechanism for classes \n\
- -nofastunpack - Use traditional UnpackTuple method to parse the argument functions (default) \n\
- -nofastproxy - Use traditional proxy mechanism for member methods (default) \n\
- -nofastquery - Use traditional query mechanism for types (default) \n\
- -noh - Don't generate the output header file\n\
- -nomodern - Don't use modern python features which are not backwards compatible \n\
- -nomodernargs - Use classic ParseTuple/CallFunction methods to pack/unpack the function arguments (default) \n";
- static const char *usage3 = (char *) "\
- -noolddefs - Don't emit the old method definitions even when using fastproxy (default) \n\
- -nooutputtuple - Use a PyList for appending output values (default) \n\
- -noproxy - Don't generate proxy classes \n\
- -noproxydel - Don't generate the redundant __del__ method \n\
- -noproxyimport - Don't insert proxy import statements derived from the %import directive \n\
- -nortti - Disable the use of the native C++ RTTI with directors\n\
- -nosafecstrings - Avoid extra strings copies when possible (default)\n\
- -nothreads - Disable thread support for the entire interface\n\
- -olddefs - Keep the old method definitions even when using fastproxy\n\
- -oldrepr - Use shorter and old version of __repr__ in proxy classes\n\
- -outputtuple - Use a PyTuple for outputs instead of a PyList (use carefully with legacy interfaces) \n\
- -proxydel - Generate a __del__ method even though it is now redundant (default) \n\
- -safecstrings - Use safer (but slower) C string mapping, generating copies from Python -> C/C++\n\
- -threads - Add thread support for all the interface\n\
- -O - Enable the following optimization options: \n\
- -modern -fastdispatch -nosafecstrings -fvirtual -noproxydel \n\
- -fastproxy -fastinit -fastunpack -fastquery -modernargs -nobuildnone \n\
- -py3 - Generate code with Python 3 specific features:\n\
- Function annotation \n\
- \n";
- static String *getSlot(Node *n = NULL, const char *key = NULL) {
- static String *slot_default = NewString("0");
- String *val = key && *key ? Getattr(n, key) : NULL;
- return val ? val : slot_default;
- }
- static void printSlot(File *f, const String *slotval, const char *slotname, const char *functype = NULL) {
- String *slotval_override = functype ? NewStringf("(%s) %s", functype, slotval) : 0;
- if (slotval_override)
- slotval = slotval_override;
- int len = Len(slotval);
- int fieldwidth = len > 40 ? 0 : 40 - len;
- Printf(f, " %s, %*s/* %s */\n", slotval, fieldwidth, "", slotname);
- Delete(slotval_override);
- }
- static String *getClosure(String *functype, String *wrapper, int funpack = 0) {
- static const char *functypes[] = {
- "unaryfunc", "SWIGPY_UNARYFUNC_CLOSURE",
- "destructor", "SWIGPY_DESTRUCTOR_CLOSURE",
- "inquiry", "SWIGPY_INQUIRY_CLOSURE",
- "getiterfunc", "SWIGPY_UNARYFUNC_CLOSURE",
- "binaryfunc", "SWIGPY_BINARYFUNC_CLOSURE",
- "ternaryfunc", "SWIGPY_TERNARYFUNC_CLOSURE",
- "ternarycallfunc", "SWIGPY_TERNARYCALLFUNC_CLOSURE",
- "lenfunc", "SWIGPY_LENFUNC_CLOSURE",
- "ssizeargfunc", "SWIGPY_SSIZEARGFUNC_CLOSURE",
- "ssizessizeargfunc", "SWIGPY_SSIZESSIZEARGFUNC_CLOSURE",
- "ssizeobjargproc", "SWIGPY_SSIZEOBJARGPROC_CLOSURE",
- "ssizessizeobjargproc", "SWIGPY_SSIZESSIZEOBJARGPROC_CLOSURE",
- "objobjargproc", "SWIGPY_OBJOBJARGPROC_CLOSURE",
- "reprfunc", "SWIGPY_REPRFUNC_CLOSURE",
- "hashfunc", "SWIGPY_HASHFUNC_CLOSURE",
- "iternextfunc", "SWIGPY_ITERNEXT_CLOSURE",
- NULL
- };
- static const char *funpack_functypes[] = {
- "unaryfunc", "SWIGPY_UNARYFUNC_CLOSURE",
- "destructor", "SWIGPY_DESTRUCTOR_CLOSURE",
- "inquiry", "SWIGPY_INQUIRY_CLOSURE",
- "getiterfunc", "SWIGPY_UNARYFUNC_CLOSURE",
- "ternaryfunc", "SWIGPY_TERNARYFUNC_CLOSURE",
- "ternarycallfunc", "SWIGPY_TERNARYCALLFUNC_CLOSURE",
- "lenfunc", "SWIGPY_LENFUNC_CLOSURE",
- "ssizeargfunc", "SWIGPY_FUNPACK_SSIZEARGFUNC_CLOSURE",
- "ssizessizeargfunc", "SWIGPY_SSIZESSIZEARGFUNC_CLOSURE",
- "ssizeobjargproc", "SWIGPY_SSIZEOBJARGPROC_CLOSURE",
- "ssizessizeobjargproc", "SWIGPY_SSIZESSIZEOBJARGPROC_CLOSURE",
- "objobjargproc", "SWIGPY_OBJOBJARGPROC_CLOSURE",
- "reprfunc", "SWIGPY_REPRFUNC_CLOSURE",
- "hashfunc", "SWIGPY_HASHFUNC_CLOSURE",
- "iternextfunc", "SWIGPY_ITERNEXT_CLOSURE",
- NULL
- };
- if (!functype)
- return NULL;
- char *c = Char(functype);
- int i;
- if (funpack) {
- for (i = 0; funpack_functypes[i] != NULL; i += 2) {
- if (!strcmp(c, funpack_functypes[i]))
- return NewStringf("%s(%s)", funpack_functypes[i + 1], wrapper);
- }
- } else {
- for (i = 0; functypes[i] != NULL; i += 2) {
- if (!strcmp(c, functypes[i]))
- return NewStringf("%s(%s)", functypes[i + 1], wrapper);
- }
- }
- return NULL;
- }
- class PYTHON:public Language {
- public:
- PYTHON() {
- /* Add code to manage protected constructors and directors */
- director_prot_ctor_code = NewString("");
- Printv(director_prot_ctor_code,
- "if ( $comparison ) { /* subclassed */\n",
- " $director_new \n",
- "} else {\n", " SWIG_SetErrorMsg(PyExc_RuntimeError,\"accessing abstract class or protected constructor\"); \n", " SWIG_fail;\n", "}\n", NIL);
- director_multiple_inheritance = 1;
- director_language = 1;
- }
- /* ------------------------------------------------------------
- * Thread Implementation
- * ------------------------------------------------------------ */
- int threads_enable(Node *n) const {
- return threads && !GetFlagAttr(n, "feature:nothread");
- }
- int initialize_threads(String *f_init) {
- if (!threads) {
- return SWIG_OK;
- }
- Printf(f_init, "\n");
- Printf(f_init, "/* Initialize threading */\n");
- Printf(f_init, "SWIG_PYTHON_INITIALIZE_THREADS;\n");
- return SWIG_OK;
- }
- virtual void thread_begin_block(Node *n, String *f) {
- if (!GetFlag(n, "feature:nothreadblock")) {
- String *bb = Getattr(n, "feature:threadbeginblock");
- if (bb) {
- Append(f, bb);
- } else {
- Append(f, "SWIG_PYTHON_THREAD_BEGIN_BLOCK;\n");
- }
- }
- }
- virtual void thread_end_block(Node *n, String *f) {
- if (!GetFlag(n, "feature:nothreadblock")) {
- String *eb = Getattr(n, "feature:threadendblock");
- if (eb) {
- Append(f, eb);
- } else {
- Append(f, "SWIG_PYTHON_THREAD_END_BLOCK;\n");
- }
- }
- }
- virtual void thread_begin_allow(Node *n, String *f) {
- if (!GetFlag(n, "feature:nothreadallow")) {
- String *bb = Getattr(n, "feature:threadbeginallow");
- Append(f, "{\n");
- if (bb) {
- Append(f, bb);
- } else {
- Append(f, "SWIG_PYTHON_THREAD_BEGIN_ALLOW;\n");
- }
- }
- }
- virtual void thread_end_allow(Node *n, String *f) {
- if (!GetFlag(n, "feature:nothreadallow")) {
- String *eb = Getattr(n, "feature:threadendallow");
- Append(f, "\n");
- if (eb) {
- Append(f, eb);
- } else {
- Append(f, "SWIG_PYTHON_THREAD_END_ALLOW;");
- }
- Append(f, "\n}");
- }
- }
- /* ------------------------------------------------------------
- * main()
- * ------------------------------------------------------------ */
- virtual void main(int argc, char *argv[]) {
- int cppcast = 1;
- SWIG_library_directory("python");
- for (int i = 1; i < argc; i++) {
- if (argv[i]) {
- if (strcmp(argv[i], "-interface") == 0) {
- if (argv[i + 1]) {
- interface = NewString(argv[i + 1]);
- Swig_mark_arg(i);
- Swig_mark_arg(i + 1);
- i++;
- } else {
- Swig_arg_error();
- }
- /* end added */
- } else if (strcmp(argv[i], "-globals") == 0) {
- if (argv[i + 1]) {
- global_name = NewString(argv[i + 1]);
- Swig_mark_arg(i);
- Swig_mark_arg(i + 1);
- i++;
- } else {
- Swig_arg_error();
- }
- } else if ((strcmp(argv[i], "-shadow") == 0) || ((strcmp(argv[i], "-proxy") == 0))) {
- shadow = 1;
- Swig_mark_arg(i);
- } else if ((strcmp(argv[i], "-new_repr") == 0) || (strcmp(argv[i], "-newrepr") == 0)) {
- new_repr = 1;
- Swig_mark_arg(i);
- } else if ((strcmp(argv[i], "-old_repr") == 0) || (strcmp(argv[i], "-oldrepr") == 0)) {
- new_repr = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-classptr") == 0) {
- classptr = 1;
- Swig_mark_arg(i);
- } else if ((strcmp(argv[i], "-noproxy") == 0)) {
- shadow = 0;
- Swig_mark_arg(i);
- } else if ((strcmp(argv[i], "-noproxyimport") == 0)) {
- shadowimport = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-keyword") == 0) {
- use_kw = 1;
- SWIG_cparse_set_compact_default_args(1);
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-classic") == 0) {
- classic = 1;
- modernargs = 0;
- modern = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-cppcast") == 0) {
- cppcast = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nocppcast") == 0) {
- cppcast = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-outputtuple") == 0) {
- outputtuple = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nooutputtuple") == 0) {
- outputtuple = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nortti") == 0) {
- nortti = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-threads") == 0) {
- threads = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nothreads") == 0) {
- /* Turn off thread suppor mode */
- nothreads = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-safecstrings") == 0) {
- safecstrings = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nosafecstrings") == 0) {
- safecstrings = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-buildnone") == 0) {
- buildnone = 1;
- nobuildnone = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nobuildnone") == 0) {
- buildnone = 0;
- nobuildnone = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-dirvtable") == 0) {
- dirvtable = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nodirvtable") == 0) {
- dirvtable = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-fastunpack") == 0) {
- fastunpack = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nofastunpack") == 0) {
- fastunpack = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-fastproxy") == 0) {
- fastproxy = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nofastproxy") == 0) {
- fastproxy = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-fastquery") == 0) {
- fastquery = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nofastquery") == 0) {
- fastquery = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-fastinit") == 0) {
- fastinit = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nofastinit") == 0) {
- fastinit = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-olddefs") == 0) {
- olddefs = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-noolddefs") == 0) {
- olddefs = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-castmode") == 0) {
- castmode = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nocastmode") == 0) {
- castmode = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-extranative") == 0) {
- extranative = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-noextranative") == 0) {
- extranative = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-modernargs") == 0) {
- modernargs = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nomodernargs") == 0) {
- modernargs = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-aliasobj0") == 0) {
- aliasobj0 = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-noaliasobj0") == 0) {
- aliasobj0 = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-proxydel") == 0) {
- proxydel = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-noproxydel") == 0) {
- proxydel = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-modern") == 0) {
- classic = 0;
- modern = 1;
- modernargs = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-nomodern") == 0) {
- modern = 0;
- modernargs = 0;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-noh") == 0) {
- no_header_file = 1;
- Swig_mark_arg(i);
- } else if ((strcmp(argv[i], "-new_vwm") == 0) || (strcmp(argv[i], "-newvwm") == 0)) {
- /* Turn on new value wrapper mpde */
- Swig_value_wrapper_mode(1);
- no_header_file = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-O") == 0) {
- classic = 0;
- modern = 1;
- safecstrings = 0;
- buildnone = 0;
- nobuildnone = 1;
- classptr = 0;
- proxydel = 0;
- fastunpack = 1;
- fastproxy = 1;
- fastinit = 1;
- fastquery = 1;
- modernargs = 1;
- Wrapper_fast_dispatch_mode_set(1);
- Wrapper_virtual_elimination_mode_set(1);
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-help") == 0) {
- fputs(usage1, stdout);
- fputs(usage2, stdout);
- fputs(usage3, stdout);
- } else if (strcmp(argv[i], "-py3") == 0) {
- py3 = 1;
- Swig_mark_arg(i);
- } else if (strcmp(argv[i], "-builtin") == 0) {
- builtin = 1;
- Preprocessor_define("SWIGPYTHON_BUILTIN", 0);
- Swig_mark_arg(i);
- }
- }
- }
- if (py3) {
- /* force disable features that not compatible with Python 3.x */
- classic = 0;
- }
- if (cppcast) {
- Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0);
- }
- if (!global_name)
- global_name = NewString("cvar");
- Preprocessor_define("SWIGPYTHON 1", 0);
- SWIG_typemap_lang("python");
- SWIG_config_file("python.swg");
- allow_overloading();
- }
- /* ------------------------------------------------------------
- * top()
- * ------------------------------------------------------------ */
- virtual int top(Node *n) {
- /* check if directors are enabled for this module. note: this
- * is a "master" switch, without which no director code will be
- * emitted. %feature("director") statements are also required
- * to enable directors for individual classes or methods.
- *
- * use %module(directors="1") modulename at the start of the
- * interface file to enable director generation.
- */
- String *mod_docstring = NULL;
- {
- Node *mod = Getattr(n, "module");
- if (mod) {
- Node *options = Getattr(mod, "options");
- if (options) {
- int dirprot = 0;
- if (Getattr(options, "dirprot")) {
- dirprot = 1;
- }
- if (Getattr(options, "nodirprot")) {
- dirprot = 0;
- }
- if (Getattr(options, "directors")) {
- allow_directors();
- if (dirprot)
- allow_dirprot();
- }
- if (Getattr(options, "threads")) {
- threads = 1;
- }
- if (Getattr(options, "castmode")) {
- castmode = 1;
- }
- if (Getattr(options, "nocastmode")) {
- castmode = 0;
- }
- if (Getattr(options, "extranative")) {
- extranative = 1;
- }
- if (Getattr(options, "noextranative")) {
- extranative = 0;
- }
- if (Getattr(options, "outputtuple")) {
- outputtuple = 1;
- }
- if (Getattr(options, "nooutputtuple")) {
- outputtuple = 0;
- }
- mod_docstring = Getattr(options, "docstring");
- package = Getattr(options, "package");
- }
- }
- }
- /* Set comparison with none for ConstructorToFunction */
- setSubclassInstanceCheck(NewString("$arg != Py_None"));
- /* Initialize all of the output files */
- String *outfile = Getattr(n, "outfile");
- String *outfile_h = !no_header_file ? Getattr(n, "outfile_h") : 0;
- f_begin = NewFile(outfile, "w", SWIG_output_files());
- if (!f_begin) {
- FileErrorDisplay(outfile);
- SWIG_exit(EXIT_FAILURE);
- }
- f_runtime = NewString("");
- f_init = NewString("");
- f_header = NewString("");
- f_wrappers = NewString("");
- f_directors_h = NewString("");
- f_directors = NewString("");
- builtin_getset = NewHash();
- class_members = NewHash();
- builtin_methods = NewString("");
- builtin_default_unref = NewString("delete $self;");
- if (builtin) {
- f_builtins = NewString("");
- }
- if (directorsEnabled()) {
- if (!no_header_file) {
- f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files());
- if (!f_runtime_h) {
- FileErrorDisplay(outfile_h);
- SWIG_exit(EXIT_FAILURE);
- }
- } else {
- f_runtime_h = f_runtime;
- }
- }
- /* Register file targets with the SWIG file handler */
- Swig_register_filebyname("header", f_header);
- Swig_register_filebyname("wrapper", f_wrappers);
- Swig_register_filebyname("begin", f_begin);
- Swig_register_filebyname("runtime", f_runtime);
- Swig_register_filebyname("init", f_init);
- Swig_register_filebyname("director", f_directors);
- Swig_register_filebyname("director_h", f_directors_h);
- const_code = NewString("");
- methods = NewString("");
- Swig_banner(f_begin);
- Printf(f_runtime, "\n");
- Printf(f_runtime, "#define SWIGPYTHON\n");
- if (directorsEnabled()) {
- Printf(f_runtime, "#define SWIG_DIRECTORS\n");
- }
- if (nothreads) {
- Printf(f_runtime, "#define SWIG_PYTHON_NO_THREADS\n");
- } else if (threads) {
- Printf(f_runtime, "#define SWIG_PYTHON_THREADS\n");
- }
- if (safecstrings) {
- Printf(f_runtime, "#define SWIG_PYTHON_SAFE_CSTRINGS\n");
- }
- if (buildnone) {
- Printf(f_runtime, "#define SWIG_PYTHON_BUILD_NONE\n");
- }
- if (nobuildnone) {
- Printf(f_runtime, "#define SWIG_PYTHON_NO_BUILD_NONE\n");
- }
- if (!dirvtable) {
- Printf(f_runtime, "#define SWIG_PYTHON_DIRECTOR_NO_VTABLE\n");
- }
- if (outputtuple) {
- Printf(f_runtime, "#define SWIG_PYTHON_OUTPUT_TUPLE\n");
- }
- if (nortti) {
- Printf(f_runtime, "#ifndef SWIG_DIRECTOR_NORTTI\n");
- Printf(f_runtime, "#define SWIG_DIRECTOR_NORTTI\n");
- Printf(f_runtime, "#endif\n");
- }
- if (castmode) {
- Printf(f_runtime, "#define SWIG_CASTRANK_MODE\n");
- Printf(f_runtime, "#define SWIG_PYTHON_CAST_MODE\n");
- }
- if (extranative) {
- Printf(f_runtime, "#define SWIG_PYTHON_EXTRA_NATIVE_CONTAINERS\n");
- }
- if (classic) {
- Printf(f_runtime, "#define SWIG_PYTHON_CLASSIC\n");
- }
- if (builtin) {
- Printf(f_runtime, "#define SWIGPYTHON_BUILTIN\n");
- }
- Printf(f_runtime, "\n");
- Printf(f_header, "#if (PY_VERSION_HEX <= 0x02000000)\n");
- Printf(f_header, "# if !defined(SWIG_PYTHON_CLASSIC)\n");
- Printf(f_header, "# error \"This python version requires swig to be run with the '-classic' option\"\n");
- Printf(f_header, "# endif\n");
- Printf(f_header, "#endif\n");
- if (modern) {
- Printf(f_header, "#if (PY_VERSION_HEX <= 0x02020000)\n");
- Printf(f_header, "# error \"This python version requires swig to be run with the '-nomodern' option\"\n");
- Printf(f_header, "#endif\n");
- }
- if (modernargs) {
- Printf(f_header, "#if (PY_VERSION_HEX <= 0x02020000)\n");
- Printf(f_header, "# error \"This python version requires swig to be run with the '-nomodernargs' option\"\n");
- Printf(f_header, "#endif\n");
- }
- if (fastunpack) {
- Printf(f_header, "#ifndef METH_O\n");
- Printf(f_header, "# error \"This python version requires swig to be run with the '-nofastunpack' option\"\n");
- Printf(f_header, "#endif\n");
- }
- if (fastquery) {
- Printf(f_header, "#ifdef SWIG_TypeQuery\n");
- Printf(f_header, "# undef SWIG_TypeQuery\n");
- Printf(f_header, "#endif\n");
- Printf(f_header, "#define SWIG_TypeQuery SWIG_Python_TypeQuery\n");
- }
- /* Set module name */
- module = Copy(Getattr(n, "name"));
- mainmodule = Getattr(n, "name");
- if (directorsEnabled()) {
- Swig_banner(f_directors_h);
- Printf(f_directors_h, "\n");
- Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module);
- Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module);
- if (dirprot_mode()) {
- Printf(f_directors_h, "#include <map>\n");
- Printf(f_directors_h, "#include <string>\n\n");
- }
- Printf(f_directors, "\n\n");
- Printf(f_directors, "/* ---------------------------------------------------\n");
- Printf(f_directors, " * C++ director class methods\n");
- Printf(f_directors, " * --------------------------------------------------- */\n\n");
- if (outfile_h)
- Printf(f_directors, "#include \"%s\"\n\n", Swig_file_filename(outfile_h));
- }
- /* If shadow classing is enabled, we're going to change the module name to "_module" */
- if (shadow) {
- String *filen = NewStringf("%s%s.py", SWIG_output_directory(), Char(module));
- // If we don't have an interface then change the module name X to _X
- if (interface)
- module = interface;
- else
- Insert(module, 0, "_");
- if ((f_shadow_py = NewFile(filen, "w", SWIG_output_files())) == 0) {
- FileErrorDisplay(filen);
- SWIG_exit(EXIT_FAILURE);
- }
- Delete(filen);
- filen = NULL;
- f_shadow = NewString("");
- f_shadow_imports = NewHash();
- f_shadow_builtin_imports = NewString("");
- f_shadow_stubs = NewString("");
- Swig_register_filebyname("shadow", f_shadow);
- Swig_register_filebyname("python", f_shadow);
- if (mod_docstring && Len(mod_docstring)) {
- Printv(f_shadow, "\"\"\"\n", mod_docstring, "\n\"\"\"\n\n", NIL);
- Delete(mod_docstring);
- mod_docstring = NULL;
- }
- Printv(f_shadow, "\nfrom sys import version_info\n", NULL);
- if (!builtin && fastproxy) {
- Printv(f_shadow, "if version_info >= (3,0,0):\n", NULL);
- Printf(f_shadow, tab4 "new_instancemethod = lambda func, inst, cls: %s.SWIG_PyInstanceMethod_New(func)\n", module);
- Printv(f_shadow, "else:\n", NULL);
- Printv(f_shadow, tab4, "from new import instancemethod as new_instancemethod\n", NULL);
- }
- /* Import the C-extension module. This should be a relative import,
- * since the shadow module may also have been imported by a relative
- * import, and there is thus no guarantee that the C-extension is on
- * sys.path. Relative imports must be explicitly specified from 2.6.0
- * onwards (implicit relative imports will raise a DeprecationWarning
- * in 2.6, and fail in 2.7 onwards), but the relative import syntax
- * isn't available in python 2.4 or earlier, so we have to write some
- * code conditional on the python version.
- */
- Printv(f_shadow, "if version_info >= (2,6,0):\n", NULL);
- Printv(f_shadow, tab4, "def swig_import_helper():\n", NULL);
- Printv(f_shadow, tab8, "from os.path import dirname\n", NULL);
- Printv(f_shadow, tab8, "import imp\n", NULL);
- Printv(f_shadow, tab8, "fp = None\n", NULL);
- Printv(f_shadow, tab8, "try:\n", NULL);
- Printf(f_shadow, tab4 tab8 "fp, pathname, description = imp.find_module('%s', [dirname(__file__)])\n", module);
- Printf(f_shadow, tab8 "except ImportError:\n");
- /* At here, the module may already loaded, so simply import it. */
- Printf(f_shadow, tab4 tab8 "import %s\n", module);
- Printf(f_shadow, tab4 tab8 "return %s\n", module);
- Printv(f_shadow, tab8 "if fp is not None:\n", NULL);
- Printv(f_shadow, tab4 tab8 "try:\n", NULL);
- Printf(f_shadow, tab8 tab8 "_mod = imp.load_module('%s', fp, pathname, description)\n", module);
- Printv(f_shadow, tab4 tab8, "finally:\n", NULL);
- Printv(f_shadow, tab8 tab8, "fp.close()\n", NULL);
- Printv(f_shadow, tab4 tab8, "return _mod\n", NULL);
- Printf(f_shadow, tab4 "%s = swig_import_helper()\n", module);
- Printv(f_shadow, tab4, "del swig_import_helper\n", NULL);
- Printv(f_shadow, "else:\n", NULL);
- Printf(f_shadow, tab4 "import %s\n", module);
- /* Delete the version_info symbol since we don't use it elsewhere in the
- * module. */
- Printv(f_shadow, "del version_info\n", NULL);
- if (builtin) {
- Printf(f_shadow, "from %s import *\n", module);
- }
- if (modern || !classic) {
- Printv(f_shadow, "try:\n", tab4, "_swig_property = property\n", "except NameError:\n", tab4, "pass # Python < 2.2 doesn't have 'property'.\n", NULL);
- }
- /* if (!modern) */
- /* always needed, a class can be forced to be no-modern, such as an exception */
- {
- // Python-2.2 object hack
- Printv(f_shadow,
- "def _swig_setattr_nondynamic(self,class_type,name,value,static=1):\n",
- tab4, "if (name == \"thisown\"): return self.this.own(value)\n",
- tab4, "if (name == \"this\"):\n", tab4, tab4, "if type(value).__name__ == 'SwigPyObject':\n", tab4, tab8, "self.__dict__[name] = value\n",
- #ifdef USE_THISOWN
- tab4, tab8, "if hasattr(value,\"thisown\"): self.__dict__[\"thisown\"] = value.thisown\n", tab4, tab8, "del value.thisown\n",
- #endif
- tab4, tab8, "return\n", tab4, "method = class_type.__swig_setmethods__.get(name,None)\n", tab4, "if method: return method(self,value)\n",
- #ifdef USE_THISOWN
- tab4, "if (not static) or (name == \"thisown\"):\n",
- #else
- tab4, "if (not static):\n",
- #endif
- tab4, tab4, "self.__dict__[name] = value\n",
- tab4, "else:\n",
- tab4, tab4, "raise AttributeError(\"You cannot add attributes to %s\" % self)\n\n",
- "def _swig_setattr(self,class_type,name,value):\n", tab4, "return _swig_setattr_nondynamic(self,class_type,name,value,0)\n\n", NIL);
- Printv(f_shadow,
- "def _swig_getattr(self,class_type,name):\n",
- tab4, "if (name == \"thisown\"): return self.this.own()\n",
- tab4, "method = class_type.__swig_getmethods__.get(name,None)\n",
- tab4, "if method: return method(self)\n", tab4, "raise AttributeError(name)\n\n", NIL);
- Printv(f_shadow,
- "def _swig_repr(self):\n",
- tab4, "try: strthis = \"proxy of \" + self.this.__repr__()\n",
- tab4, "except: strthis = \"\"\n", tab4, "return \"<%s.%s; %s >\" % (self.__class__.__module__, self.__class__.__name__, strthis,)\n\n", NIL);
- if (!classic) {
- /* Usage of types.ObjectType is deprecated.
- * But don't sure wether this would broken old Python?
- */
- Printv(f_shadow,
- // "import types\n",
- "try:\n",
- // " _object = types.ObjectType\n",
- " _object = object\n", " _newclass = 1\n", "except AttributeError:\n", " class _object : pass\n", " _newclass = 0\n",
- // "del types\n",
- "\n\n", NIL);
- }
- }
- if (modern) {
- Printv(f_shadow, "def _swig_setattr_nondynamic_method(set):\n", tab4, "def set_attr(self,name,value):\n",
- #ifdef USE_THISOWN
- tab4, tab4, "if hasattr(self,name) or (name in (\"this\", \"thisown\")):\n",
- #else
- tab4, tab4, "if (name == \"thisown\"): return self.this.own(value)\n", tab4, tab4, "if hasattr(self,name) or (name == \"this\"):\n",
- #endif
- tab4, tab4, tab4, "set(self,name,value)\n",
- tab4, tab4, "else:\n",
- tab4, tab4, tab4, "raise AttributeError(\"You cannot add attributes to %s\" % self)\n", tab4, "return set_attr\n\n\n", NIL);
- }
- if (directorsEnabled()) {
- // Try loading weakref.proxy, which is only available in Python 2.1 and higher
- Printv(f_shadow,
- "try:\n", tab4, "import weakref\n", tab4, "weakref_proxy = weakref.proxy\n", "except:\n", tab4, "weakref_proxy = lambda x: x\n", "\n\n", NIL);
- }
- }
- // Include some information in the code
- Printf(f_header, "\n/*-----------------------------------------------\n @(target):= %s.so\n\
- ------------------------------------------------*/\n", module);
- Printf(f_header, "#if PY_VERSION_HEX >= 0x03000000\n");
- Printf(f_header, "# define SWIG_init PyInit_%s\n\n", module);
- Printf(f_header, "#else\n");
- Printf(f_header, "# define SWIG_init init%s\n\n", module);
- Printf(f_header, "#endif\n");
- Printf(f_header, "#define SWIG_name \"%s\"\n", module);
- Printf(f_wrappers, "#ifdef __cplusplus\n");
- Printf(f_wrappers, "extern \"C\" {\n");
- Printf(f_wrappers, "#endif\n");
- Append(const_code, "static swig_const_info swig_const_table[] = {\n");
- Append(methods, "static PyMethodDef SwigMethods[] = {\n");
- /* the method exported for replacement of new.instancemethod in Python 3 */
- add_pyinstancemethod_new();
- if (builtin) {
- SwigType *s = NewString("SwigPyObject");
- SwigType_add_pointer(s);
- SwigType_remember(s);
- Delete(s);
- }
- /* emit code */
- Language::top(n);
- if (directorsEnabled()) {
- // Insert director runtime into the f_runtime file (make it occur before %header section)
- Swig_insert_file("director.swg", f_runtime);
- }
- /* Close language module */
- Append(methods, "\t { NULL, NULL, 0, NULL }\n");
- Append(methods, "};\n");
- Printf(f_wrappers, "%s\n", methods);
- if (builtin) {
- Dump(f_builtins, f_wrappers);
- }
- SwigType_emit_type_table(f_runtime, f_wrappers);
- Append(const_code, "{0, 0, 0, 0.0, 0, 0}};\n");
- Printf(f_wrappers, "%s\n", const_code);
- initialize_threads(f_init);
- Printf(f_init, "#if PY_VERSION_HEX >= 0x03000000\n");
- Printf(f_init, " return m;\n");
- Printf(f_init, "#else\n");
- Printf(f_init, " return;\n");
- Printf(f_init, "#endif\n");
- Printf(f_init, "}\n");
- Printf(f_wrappers, "#ifdef __cplusplus\n");
- Printf(f_wrappers, "}\n");
- Printf(f_wrappers, "#endif\n");
- if (shadow) {
- Swig_banner_target_lang(f_shadow_py, "#");
- if (!modern) {
- Printv(f_shadow, "# This file is compatible with both classic and new-style classes.\n", NIL);
- }
- Printv(f_shadow_py, "\n", f_shadow_builtin_imports, "\n", NIL);
- Printv(f_shadow_py, f_shadow, "\n", NIL);
- Printv(f_shadow_py, f_shadow_stubs, "\n", NIL);
- Close(f_shadow_py);
- Delete(f_shadow_py);
- }
- /* Close all of the files */
- Dump(f_runtime, f_begin);
- Dump(f_header, f_begin);
- if (directorsEnabled()) {
- Dump(f_directors_h, f_runtime_h);
- Printf(f_runtime_h, "\n");
- Printf(f_runtime_h, "#endif\n");
- if (f_runtime_h != f_begin)
- Close(f_runtime_h);
- Dump(f_directors, f_begin);
- }
- Dump(f_wrappers, f_begin);
- if (builtin && builtin_bases_needed)
- Printf(f_begin, "static PyTypeObject *builtin_bases[%d];\n\n", max_bases + 2);
- Wrapper_pretty_print(f_init, f_begin);
- Delete(f_header);
- Delete(f_wrappers);
- Delete(f_builtins);
- Delete(f_init);
- Delete(f_directors);
- Delete(f_directors_h);
- Close(f_begin);
- Delete(f_runtime);
- Delete(f_begin);
- return SWIG_OK;
- }
- /* ------------------------------------------------------------
- * Emit the wrapper for PyInstanceMethod_New to MethodDef array.
- * This wrapper is used to implement -fastproxy,
- * as a replacement of new.instancemethod in Python 3.
- * ------------------------------------------------------------ */
- int add_pyinstancemethod_new() {
- String *name = NewString("SWIG_PyInstanceMethod_New");
- Printf(methods, "\t { (char *)\"%s\", (PyCFunction)%s, METH_O, NULL},\n", name, name);
- Delete(name);
- return 0;
- }
- /* ------------------------------------------------------------
- * importDirective()
- * ------------------------------------------------------------ */
- virtual int importDirective(Node *n) {
- if (shadow) {
- String *modname = Getattr(n, "module");
- if (modname) {
- String *import = NewString("import ");
- // Find the module node for this imported module. It should be the
- // first child but search just in case.
- Node *mod = firstChild(n);
- while (mod && Strcmp(nodeType(mod), "module") != 0)
- mod = nextSibling(mod);
- // Is the imported module in another package? (IOW, does it use the
- // %module(package="name") option and it's different than the package
- // of this module.)
- Node *options = Getattr(mod, "options");
- String *pkg = options ? Getattr(options, "package") : 0;
- if (pkg && (!package || Strcmp(pkg, package) != 0)) {
- Printf(import, "%s.", pkg);
- }
- // finally, output the name of the imported module
- if (shadowimport) {
- if (!options || (!Getattr(options, "noshadow") && !Getattr(options, "noproxy"))) {
- Printf(import, "_%s\n", modname);
- if (!GetFlagAttr(f_shadow_imports, import)) {
- if (pkg && (!package || Strcmp(pkg, package) != 0)) {
- Printf(builtin ? f_shadow_builtin_imports : f_shadow, "import %s.%s\n", pkg, modname);
- } else {
- Printf(builtin ? f_shadow_builtin_imports : f_shadow, "import %s\n", modname);
- }
- SetFlag(f_shadow_imports, import);
- }
- }
- }
- Delete(import);
- }
- }
- return Language::importDirective(n);
- }
- /* ------------------------------------------------------------
- * funcCall()
- * Emit shadow code to call a function in the extension
- * module. Using proper argument and calling style for
- * given node n.
- * ------------------------------------------------------------ */
- String *funcCall(String *name, String *parms) {
- String *str = NewString("");
- Printv(str, module, ".", name, "(", parms, ")", NIL);
- return str;
- }
- /* ------------------------------------------------------------
- * pythoncode() - Output python code into the shadow file
- * ------------------------------------------------------------ */
- String *pythoncode(String *code, const_String_or_char_ptr indent) {
- String *out = NewString("");
- String *temp;
- char *t;
- if (!indent)
- indent = "";
- temp = NewString(code);
- t = Char(temp);
- if (*t == '{') {
- Delitem(temp, 0);
- Delitem(temp, DOH_END);
- }
- /* Split the input text into lines */
- List *clist = DohSplitLines(temp);
- Delete(temp);
- int initial = 0;
- String *s = 0;
- Iterator si;
- /* Get the initial indentation */
- for (si = First(clist); si.item; si = Next(si)) {
- s = si.item;
- if (Len(s)) {
- char *c = Char(s);
- while (*c) {
- if (!isspace(*c))
- break;
- initial++;
- c++;
- }
- if (*c && !isspace(*c)) {
- break;
- } else {
- initial = 0;
- }
- }
- }
- while (si.item) {
- s = si.item;
- if (Len(s) > initial) {
- char *c = Char(s);
- c += initial;
- Printv(out, indent, c, "\n", NIL);
- } else {
- Printv(out, "\n", NIL);
- }
- si = Next(si);
- }
- Delete(clist);
- return out;
- }
- /* ------------------------------------------------------------
- * autodoc level declarations
- * ------------------------------------------------------------ */
- enum autodoc_l {
- NO_AUTODOC = -2, // no autodoc
- STRING_AUTODOC = -1, // use provided string
- NAMES_AUTODOC = 0, // only parameter names
- TYPES_AUTODOC = 1, // parameter names and types
- EXTEND_AUTODOC = 2, // extended documentation and parameter names
- EXTEND_TYPES_AUTODOC = 3 // extended documentation and parameter types + names
- };
- autodoc_l autodoc_level(String *autodoc) {
- autodoc_l dlevel = NO_AUTODOC;
- if (autodoc) {
- char *c = Char(autodoc);
- if (c && isdigit(c[0])) {
- dlevel = (autodoc_l) atoi(c);
- } else {
- if (strcmp(c, "extended") == 0) {
- dlevel = EXTEND_AUTODOC;
- } else {
- dlevel = STRING_AUTODOC;
- }
- }
- }
- return dlevel;
- }
- /* ------------------------------------------------------------
- * have_docstring()
- * Check if there is a docstring directive and it has text,
- * or there is an autodoc flag set
- * ------------------------------------------------------------ */
- bool have_docstring(Node *n) {
- String *str = Getattr(n, "feature:docstring");
- return (str && Len(str) > 0) || (Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc"));
- }
- /* ------------------------------------------------------------
- * docstring()
- * Get the docstring text, stripping off {} if neccessary,
- * and enclose in triple double quotes. If autodoc is also
- * set then it will build a combined docstring.
- * ------------------------------------------------------------ */
- String *docstring(Node *n, autodoc_t ad_type, const String *indent, bool use_triple = true) {
- String *str = Getattr(n, "feature:docstring");
- bool have_ds = (str && Len(str) > 0);
- bool have_auto = (Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc"));
- const char *triple_double = use_triple ? "\"\"\"" : "";
- String *autodoc = NULL;
- String *doc = NULL;
- if (have_ds) {
- char *t = Char(str);
- if (*t == '{') {
- Delitem(str, 0);
- Delitem(str, DOH_END);
- }
- }
- if (have_auto) {
- autodoc = make_autodoc(n, ad_type);
- have_auto = (autodoc && Len(autodoc) > 0);
- }
- // If there is more than one line then make docstrings like this:
- //
- // """
- // This is line1
- // And here is line2 followed by the rest of them
- // """
- //
- // otherwise, put it all on a single line
- //
- if (have_auto && have_ds) { // Both autodoc and docstring are present
- doc = NewString("");
- Printv(doc, triple_double, "\n", pythoncode(autodoc, indent), "\n", pythoncode(str, indent), indent, triple_double, NIL);
- } else if (!have_auto && have_ds) { // only docstring
- if (Strchr(str, '\n') == 0) {
- doc = NewStringf("%s%s%s", triple_double, str, triple_double);
- } else {
- doc = NewString("");
- Printv(doc, triple_double, "\n", pythoncode(str, indent), indent, triple_double, NIL);
- }
- } else if (have_auto && !have_ds) { // only autodoc
- if (Strchr(autodoc, '\n') == 0) {
- doc = NewStringf("%s%s%s", triple_double, autodoc, triple_double);
- } else {
- doc = NewString("");
- Printv(doc, triple_double, "\n", pythoncode(autodoc, indent), indent, triple_double, NIL);
- }
- } else
- doc = NewString("");
- // Save the generated strings in the parse tree in case they are used later
- // by post processing tools
- Setattr(n, "python:docstring", doc);
- Setattr(n, "python:autodoc", autodoc);
- return doc;
- }
- /* ------------------------------------------------------------
- * cdocstring()
- * Get the docstring text as it would appear in C-language
- * source code.
- * ------------------------------------------------------------ */
- String *cdocstring(Node *n, autodoc_t ad_type)
- {
- String *ds = docstring(n, ad_type, "", false);
- Replaceall(ds, "\\", "\\\\");
- Replaceall(ds, "\"", "\\\"");
- Replaceall(ds, "\n", "\\n\"\n\t\t\"");
- return ds;
- }
- /* -----------------------------------------------------------------------------
- * addMissingParameterNames()
- * For functions that have not had nameless parameters set in the Language class.
- *
- * Inputs:
- * plist - entire parameter list
- * arg_offset - argument number for first parameter
- * Side effects:
- * The "lname" attribute in each parameter in plist will be contain a parameter name
- * ----------------------------------------------------------------------------- */
- void addMissingParameterNames(ParmList *plist, int arg_offset) {
- Parm *p = plist;
- int i = arg_offset;
- while (p) {
- if (!Getattr(p, "lname")) {
- String *pname = Swig_cparm_name(p, i);
- Delete(pname);
- }
- i++;
- p = nextSibling(p);
- }
- }
- /* ------------------------------------------------------------
- * make_autodocParmList()
- * Generate the documentation for the function parameters
- * Parameters:
- * func_annotation: Function annotation support
- * ------------------------------------------------------------ */
- String *make_autodocParmList(Node *n, bool showTypes, bool calling = false, bool func_annotation = false) {
- String *doc = NewString("");
- String *pdocs = 0;
- ParmList *plist = CopyParmList(Getattr(n, "parms"));
- Parm *p;
- Parm *pnext;
- int lines = 0;
- int start_arg_num = is_wrapping_class() ? 1 : 0;
- const int maxwidth = 80;
- if (calling)
- func_annotation = false;
- addMissingParameterNames(plist, start_arg_num); // for $1_name substitutions done in Swig_typemap_attach_parms
- Swig_typemap_attach_parms("in", plist, 0);
- Swig_typemap_attach_parms("doc", plist, 0);
- if (Strcmp(ParmList_protostr(plist), "void") == 0) {
- //No parameters actually
- return doc;
- }
- for (p = plist; p; p = pnext) {
- String *tm = Getattr(p, "tmap:in");
- if (tm) {
- pnext = Getattr(p, "tmap:in:next");
- if (checkAttribute(p, "tmap:in:numinputs", "0")) {
- continue;
- }
- } else {
- pnext = nextSibling(p);
- }
- String *name = 0;
- String *type = 0;
- String *value = 0;
- String *pdoc = Getattr(p, "tmap:doc");
- if (pdoc) {
- name = Getattr(p, "tmap:doc:name");
- type = Getattr(p, "tmap:doc:type");
- value = Getattr(p, "tmap:doc:value");
- }
- // Note: the generated name should be consistent with that in kwnames[]
- name = name ? name : Getattr(p, "name");
- name = name ? name : Getattr(p, "lname");
- name = Swig_name_make(p, 0, name, 0, 0); // rename parameter if a keyword
- type = type ? type : Getattr(p, "type");
- value = value ? value : Getattr(p, "value");
- if (SwigType_isvarargs(type))
- break;
- if (Len(doc)) {
- // add a comma to the previous one if any
- Append(doc, ", ");
- // Do we need to wrap a long line?
- if ((Len(doc) - lines * maxwidth) > maxwidth) {
- Printf(doc, "\n%s", tab4);
- lines += 1;
- }
- }
- // Do the param type too?
- Node *nn = classLookup(Getattr(p, "type"));
- String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0);
- if (showTypes)
- Printf(doc, "%s ", type_str);
- Append(doc, name);
- if (pdoc) {
- if (!pdocs)
- pdocs = NewString("\nParameters:\n");
- Printf(pdocs, " %s\n", pdoc);
- }
- // Write the function annotation
- if (func_annotation)
- Printf(doc, " : '%s'", type_str);
- // Write default value
- if (value && !calling) {
- String *new_value = convertValue(value, Getattr(p, "type"));
- if (new_value) {
- value = new_value;
- } else {
- Node *lookup = Swig_symbol_clookup(value, 0);
- if (lookup)
- value = Getattr(lookup, "sym:name");
- }
- Printf(doc, "=%s", value);
- }
- Delete(type_str);
- Delete(name);
- }
- if (pdocs)
- Setattr(n, "feature:pdocs", pdocs);
- Delete(plist);
- return doc;
- }
- /* ------------------------------------------------------------
- * make_autodoc()
- * Build a docstring for the node, using parameter and other
- * info in the parse tree. If the value of the autodoc
- * attribute is "0" then do not include parameter types, if
- * it is "1" (the default) then do. If it has some other
- * value then assume it is supplied by the extension writer
- * and use it directly.
- * ------------------------------------------------------------ */
- String *make_autodoc(Node *n, autodoc_t ad_type) {
- int extended = 0;
- // If the function is overloaded then this function is called
- // for the last one. Rewind to the first so the docstrings are
- // in order.
- while (Getattr(n, "sym:previousSibling"))
- n = Getattr(n, "sym:previousSibling");
- String *doc = NewString("");
- while (n) {
- bool showTypes = false;
- bool skipAuto = false;
- String *autodoc = Getattr(n, "feature:autodoc");
- autodoc_l dlevel = autodoc_level(autodoc);
- switch (dlevel) {
- case NO_AUTODOC:
- break;
- case NAMES_AUTODOC:
- showTypes = false;
- break;
- case TYPES_AUTODOC:
- showTypes = true;
- break;
- case EXTEND_AUTODOC:
- extended = 1;
- showTypes = false;
- break;
- case EXTEND_TYPES_AUTODOC:
- extended = 1;
- showTypes = true;
- break;
- case STRING_AUTODOC:
- Append(doc, autodoc);
- skipAuto = true;
- break;
- }
- if (!skipAuto) {
- String *symname = Getattr(n, "sym:name");
- SwigType *type = Getattr(n, "type");
- String *type_str = NULL;
- if (type) {
- if (Strcmp(type, "void") == 0) {
- type_str = NULL;
- } else {
- Node *nn = classLookup(type);
- type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0);
- }
- }
- switch (ad_type) {
- case AUTODOC_CLASS:
- {
- // Only do the autodoc if there isn't a docstring for the class
- String *str = Getattr(n, "feature:docstring");
- if (!str || Len(str) == 0) {
- if (CPlusPlus) {
- Printf(doc, "Proxy of C++ %s class", real_classname);
- } else {
- Printf(doc, "Proxy of C %s struct", real_classname);
- }
- }
- }
- break;
- case AUTODOC_CTOR:
- if (Strcmp(class_name, symname) == 0) {
- String *paramList = make_autodocParmList(n, showTypes);
- Printf(doc, "__init__(");
- if (showTypes)
- Printf(doc, "%s ", getClassName());
- if (Len(paramList))
- Printf(doc, "self, %s) -> %s", paramList, class_name);
- else
- Printf(doc, "self) -> %s", class_name);
- } else
- Printf(doc, "%s(%s) -> %s", symname, make_autodocParmList(n, showTypes), class_name);
- break;
- case AUTODOC_DTOR:
- if (showTypes)
- Printf(doc…
Large files files are truncated, but you can click here to view the full file