From 9e952010b9bd7928528a6319fde9b3386424cd4e Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Sat, 4 Mar 2006 11:06:51 +0000 Subject: [PATCH] add -fastinit option after issue reported by Robin Dunn git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8951 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- CHANGES.current | 7 +++++ Lib/python/pyrun.swg | 41 +++++++++++++++++++++++++++++ Source/Modules/python.cxx | 54 ++++++++++++++++++++++----------------- 3 files changed, 79 insertions(+), 23 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 018fc4548..5e0efd994 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -1,6 +1,13 @@ Version 1.3.29 (In progress) ============================ +03/04/2006: mmatus + [Python] + Add the -fastinit option to enable faster __init__ methods. + As with the other 'fast' options, this one also gets + enabled when using -O. + + 03/02/2006: wsfulton [Java] Removed extra (void *) cast when casting pointers to and from jlong as this diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index bdbc38776..0b2994ad7 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -1167,6 +1167,47 @@ SWIG_Python_NewShadowInstance(PySwigClientData *data, PyObject *swig_this) #endif } +SWIGRUNTIME void +SWIG_Python_SetSwigThis(PyObject *inst, PyObject *swig_this) +{ +#if (PY_VERSION_HEX >= 0x02020000) && !defined(SWIG_PYTHON_SLOW_GETSET_THIS) + PyObject **dictptr = _PyObject_GetDictPtr(inst); + if (dictptr != NULL) { + PyObject *dict = *dictptr; + if (dict == NULL) { + dict = PyDict_New(); + *dictptr = dict; + PyDict_SetItem(dict, SWIG_This(), swig_this); + } + } else { + PyObject *dict = PyObject_GetAttrString(inst, "__dict__"); + PyDict_SetItem(dict, SWIG_This(), swig_this); + Py_DECREF(dict); + } +#else + PyObject *dict = PyObject_GetAttrString(inst, "__dict__"); + PyDict_SetItem(dict, SWIG_This(), swig_this); + Py_DECREF(dict); +#endif +} + + +SWIGINTERN PyObject * +SWIG_Python_InitShadowInstance(PyObject *args) { + PyObject *obj[2]; + if (!SWIG_Python_UnpackTuple(args,(char*)"swiginit", 2, 2, obj)) { + return NULL; + } else { + PySwigObject *sthis = SWIG_Python_GetSwigThis(obj[0]); + if (sthis) { + PySwigObject_append((PyObject*) sthis, obj[1]); + } else { + SWIG_Python_SetSwigThis(obj[0], obj[1]); + } + return SWIG_Py_Void(); + } +} + /* Create a new pointer object */ SWIGRUNTIME PyObject * diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 1a3d30a78..ace20049f 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -70,6 +70,7 @@ static int dirvtable = 0; static int proxydel = 1; static int fastunpack = 0; static int fastproxy = 0; +static int fastinit = 0; static int olddefs = 0; static int modernargs = 0; static int aliasobj0 = 0; @@ -100,6 +101,7 @@ Python Options (available with -python)\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 \n\ -fastunpack - Use fast unpack mechanism to parse the argument functions \n\ -fastproxy - Use fast proxy mechanism for member methods \n\ -globals - Set used to access C global variable [default: 'cvar']\n\ @@ -117,6 +119,7 @@ static const char *usage2 = (char *)"\ -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 (default) \n\ -nofastunpack - Use traditional UnpackTuple method to parse the argument functions (default) \n\ -nofastproxy - Use traditional proxy mechanism for member methods (default) \n\ -noh - Don't generate the output header file\n\ @@ -138,7 +141,7 @@ static const char *usage2 = (char *)"\ -threads - Add thread support for all the interface\n\ -O - Enable all the optimizations options: \n\ -modern -fastdispatch -dirvtable -nosafecstrings -fvirtual \n\ - -noproxydel -fastproxy -fastunpack -modernargs -nobuildnone \n\ + -noproxydel -fastproxy -fastinit -fastunpack -modernargs -nobuildnone \n\ \n"; class PYTHON : public Language { @@ -336,6 +339,12 @@ public: } else if (strcmp(argv[i],"-nofastproxy") == 0) { fastproxy = 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); @@ -402,6 +411,7 @@ public: proxydel = 0; fastunpack = 1; fastproxy = 1; + fastinit = 1; modernargs = 1; Wrapper_fast_dispatch_mode_set(1); Wrapper_virtual_elimination_mode_set(1); @@ -2688,8 +2698,15 @@ public: } if (!have_constructor) { Printv(f_shadow_file, tab4,"def __init__(self): raise AttributeError, \"No constructor defined\"\n", NIL); + } else if (fastinit) { + + Printv(f_wrappers, "SWIGINTERN PyObject *", class_name, "_swiginit(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {\n", NIL); + Printv(f_wrappers, tab4, "return SWIG_Python_InitShadowInstance(args);\n", + "}\n\n",NIL); + String *cname = NewStringf("%s_swiginit", class_name); + add_method(cname, cname, 0); + Delete(cname); } - if (!have_repr) { /* Supply a repr method for this class */ String *rname = SwigType_namestr(real_classname); @@ -2974,31 +2991,22 @@ public: NIL); } - Printv(f_shadow, tab4, "def __init__(self, *args", - (allow_kwargs ? ", **kwargs" : ""),"):\n", NIL); + Printv(f_shadow, tab4, "def __init__(self, *args",(allow_kwargs ? ", **kwargs" : ""),"): \n", NIL); if ( have_docstring(n) ) - Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR, tab8), "\n", NIL); + Printv(f_shadow, tab8, docstring(n, AUTODOC_CTOR, tab8),"\n",NIL); if ( have_pythonprepend(n) ) Printv(f_shadow, tab8, pythonprepend(n), "\n", NIL); Printv(f_shadow, pass_self, NIL); - Printv(f_shadow, tab8, "this = ", funcCallHelper(Swig_name_construct(symname), allow_kwargs),"\n", NIL); - if (!modern) { - Printv(f_shadow, tab8, "try: self.this.append(this)\n", NIL); - Printv(f_shadow, tab8, "except: self.this = this\n", NIL); - Printv(f_shadow, -#ifdef USE_THISOWN - tab8, "_swig_setattr(self, ", rclassname, ", 'thisown', 1)\n", -#endif - NIL); - } else { - Printv(f_shadow, tab8, "try: self.this.append(this)\n", NIL); - Printv(f_shadow, tab8, "except: self.this = this\n", NIL); -#ifdef USE_THISOWN - Printv(f_shadow, tab8, "self.thisown = 1\n", NIL); - Printv(f_shadow, tab8, "del newobj.thisown\n", NIL); -#endif - } - if ( have_pythonappend(n) ) + if (fastinit) { + Printv(f_shadow, + tab8, module, ".", class_name, "_swiginit(self,", funcCallHelper(Swig_name_construct(symname), allow_kwargs),")\n", NIL); + } else { + Printv(f_shadow, + tab8, "this = ", funcCallHelper(Swig_name_construct(symname), allow_kwargs),"\n", + tab8, "try: self.this.append(this)\n", + tab8, "except: self.this = this\n", NIL); + } + if ( have_pythonappend(n) ) Printv(f_shadow, tab8, pythonappend(n), "\n\n", NIL); Delete(pass_self); }