From c8f173444dd87f7e5892aed9840a3f92acece2e3 Mon Sep 17 00:00:00 2001 From: Dave Beazley Date: Tue, 15 Feb 2000 19:32:45 +0000 Subject: [PATCH] Optimizations git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@238 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Source/Modules1.1/pycpp.cxx | 19 ++++++++++++---- Source/Modules1.1/python.cxx | 43 +++++++++++++++++++++++++++++++++++- Source/Modules1.1/python.h | 1 + 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/Source/Modules1.1/pycpp.cxx b/Source/Modules1.1/pycpp.cxx index 11ba1ae72..2e7ecd3fc 100644 --- a/Source/Modules1.1/pycpp.cxx +++ b/Source/Modules1.1/pycpp.cxx @@ -31,6 +31,7 @@ static char cvsroot[] = "$Header$"; static String *setattr; static String *getattr; static String *pyclass; +static String *imethod; static String *construct; static String *cinit; static String *additional; @@ -39,7 +40,7 @@ static int have_destructor; static int have_getattr; static int have_setattr; static int have_repr; -static char *class_name; +//static char *class_name; static char *class_type; static char *real_classname; static String *base_class; @@ -65,6 +66,7 @@ void PYTHON::cpp_open_class(char *classname, char *rname, char *ctype, int strip setattr = new String(); getattr = new String(); pyclass = new String(); + imethod = new String(); construct = new String(); cinit = new String(); additional= new String(); @@ -141,8 +143,14 @@ void PYTHON::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) if (strcmp(realname,"__repr__") == 0) have_repr = 1; - // Now add it to the class + if (!((hash.lookup(t->name)) && (t->is_pointer <=1))) { + *imethod << class_name << "." << realname << " = new.instancemethod(" << module << "." << name_member(realname,class_name) << ", None, " << class_name << ")\n"; + /* *pyclass << tab4 << realname << " = " << module << ".__shadow__." << name_member(realname,class_name) << "\n"; */ + } else { + + // Now add it to the class + if (use_kw) *pyclass << tab4 << "def " << realname << "(*args, **kwargs):\n"; else @@ -168,8 +176,9 @@ void PYTHON::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) } } } - emitAddPragmas(*pyclass, realname, tab8); - *pyclass << tab8 << "return val\n"; + } + // emitAddPragmas(*pyclass, realname, tab8); + // *pyclass << tab8 << "return val\n"; } } @@ -331,7 +340,9 @@ void PYTHON::cpp_close_class() { << tab8 << "self.__class__ = " << class_name << "\n" << "\n" << *additional << "\n"; + classes << *imethod << "\n"; delete pyclass; + delete imethod; delete setattr; delete getattr; delete additional; diff --git a/Source/Modules1.1/python.cxx b/Source/Modules1.1/python.cxx index 2a65e36f8..d6492d2c4 100644 --- a/Source/Modules1.1/python.cxx +++ b/Source/Modules1.1/python.cxx @@ -28,6 +28,7 @@ static char cvsroot[] = "$Header$"; #include "python.h" static String const_code; +static String shadow_methods; static char *usage = "\ Python Options (available with -python)\n\ @@ -295,6 +296,7 @@ void PYTHON::initialize(void) } fprintf(f_shadow,"# This file was created automatically by SWIG.\n"); fprintf(f_shadow,"import %s\n", module); + fprintf(f_shadow,"import new\n"); } // Dump out external module declarations @@ -363,6 +365,14 @@ void PYTHON::initialize_cmodule(void) << tab4 << "}\n"; fprintf(f_init,"%s", init.get()); +#ifdef SHADOW_METHODS + if (shadow) { + shadow_methods << "static struct { \n" + << tab4 << "char *name;\n" + << tab4 << "char *classname;\n" + << "} _swig_shadow_methods[] = {\n"; + } +#endif } @@ -408,7 +418,13 @@ void PYTHON::close_cmodule(void) const_code << "{0}};\n"; fprintf(f_wrappers,"%s\n",const_code.get()); - +#ifdef SHADOW_METHODS + if (shadow) { + shadow_methods << tab4 << "{0, 0}\n" + << "};\n"; + fprintf(f_wrappers,"%s\n", shadow_methods.get()); + } +#endif String cinit; cinit << tab4 << "{\n" << tab8 << "int i;\n" @@ -432,6 +448,25 @@ void PYTHON::close_cmodule(void) << tab8 << "}\n" << tab4 << "}\n"; +#ifdef SHADOW_METHODS + // Not done yet. If doing shadows, create a bunch of instancemethod objects for use + if (shadow) { + cinit << tab4 << "{\n" + << tab8 << "PyObject *sd, *im, *co, *sclass;\n" + << tab8 << "int i;\n" + << tab8 << "sd = PyDict_New();\n" + << tab8 << "sclass = PyClass_New(NULL, sd, PyString_FromString(\"__shadow__\"));\n" + << tab8 << "for (i = 0; _swig_shadow_methods[i].name; i++) {\n" + << tab8 << tab4 << "char *name;\n" + << tab8 << tab4 << "name = _swig_shadow_methods[i].name;\n" + << tab8 << tab4 << "co = PyDict_GetItemString(d,name);\n" + << tab8 << tab4 << "im = PyMethod_New(co, NULL, sclass);\n" + << tab8 << tab4 << "PyDict_SetItemString(sd,name,im);\n" + << tab8 << tab4 << "}\n" + << tab8 << "PyDict_SetItemString(d,\"__shadow__\", sclass);\n" + << tab4 << "}\n"; + } +#endif fprintf(f_init,"%s\n", cinit.get()); fprintf(f_init,"}\n"); } @@ -946,6 +981,12 @@ void PYTHON::create_function(char *name, char *iname, DataType *d, ParmList *l) add_method(iname, wname); +#ifdef SHADOW_METHODS + if (shadow && (shadow & PYSHADOW_MEMBER)) { + shadow_methods << tab4 << "{ \"" << iname << "\", \"" << class_name << "\" },\n"; + } +#endif + // --------------------------------------------------------------------------- // Create a shadow for this function (if enabled and not in a member function) // --------------------------------------------------------------------------- diff --git a/Source/Modules1.1/python.h b/Source/Modules1.1/python.h index c82f2e8ed..3a1291779 100644 --- a/Source/Modules1.1/python.h +++ b/Source/Modules1.1/python.h @@ -48,6 +48,7 @@ protected: String modextern; char *import_file; + char *class_name; void add_method(char *name, char *function); void print_methods(); char *usage_var(char *, DataType *);