From c66b71417c42124f9b1e7669e3e893b7614e25fd Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Wed, 6 Jul 2005 21:02:22 +0000 Subject: [PATCH] add Scott's patches for directors: void return, directormap register, better error messages git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7324 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Lib/python/director.swg | 16 ++++++++++++---- SWIG/Source/Modules/lang.cxx | 10 +++------- SWIG/Source/Modules/python.cxx | 27 +++++++++++++++++---------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/SWIG/Lib/python/director.swg b/SWIG/Lib/python/director.swg index 995184aea..1d701083c 100644 --- a/SWIG/Lib/python/director.swg +++ b/SWIG/Lib/python/director.swg @@ -28,15 +28,23 @@ namespace Swig { try { throw; } catch (DirectorException& e) { - std::cerr << "Swig Director exception caught: "<< e.getMessage() << std::endl; + std::cerr << "Swig Director exception caught:" << std::endl + << e.getMessage() << std::endl; } catch (std::exception& e) { std::cerr << "std::exception caught: "<< e.what() << std::endl; } catch (...) { std::cerr << "Unknown exception caught." << std::endl; } - std::cerr << "This exception was caught by the SWIG unexpected exception handler." << std::endl; - std::cerr << "Try using %feature(\"director:except\") to avoid reaching this point." << std::endl; - std::cerr << "Now your program will probably be terminated, bye." << std::endl; + + std::cerr << std::endl + << "Python interpreter traceback:" << std::endl; + PyErr_Print(); + std::cerr << std::endl; + + std::cerr << "This exception was caught by the SWIG unexpected exception handler." << std::endl + << "Try using %feature(\"director:except\") to avoid reaching this point." << std::endl + << std::endl + << "Exception is being re-thrown, program will like abort/terminate." << std::endl; throw; } diff --git a/SWIG/Source/Modules/lang.cxx b/SWIG/Source/Modules/lang.cxx index eba33e8a5..e9996adf0 100644 --- a/SWIG/Source/Modules/lang.cxx +++ b/SWIG/Source/Modules/lang.cxx @@ -1884,19 +1884,15 @@ int Language::classDirector(Node *n) { "Director base class %s has no virtual destructor.\n", classtype); } - /* - since now %feature("nodirector") is working, we check - that the director is really not abstract. - */ Setattr(n, "vtable", vtable); + if (directormap != 0) { + Setattr(directormap, classtype, n); + } classDirectorInit(n); classDirectorConstructors(n); classDirectorMethods(n); classDirectorEnd(n); - if (directormap != 0) { - Setattr(directormap, classtype, n); - } } Delete(vtable); return SWIG_OK; diff --git a/SWIG/Source/Modules/python.cxx b/SWIG/Source/Modules/python.cxx index d7a06916a..8a5728e20 100644 --- a/SWIG/Source/Modules/python.cxx +++ b/SWIG/Source/Modules/python.cxx @@ -2621,8 +2621,6 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { Swig_typemap_attach_parms("directorargout", l, w); Parm* p; - int num_arguments = emit_num_arguments(l); - int i; char source[256]; wrap_args = NewString(""); @@ -2630,8 +2628,9 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { if (!is_void) outputs++; /* build argument list and type conversion string */ - for (i=0, idx=0, p = l; i < num_arguments && p != 0; i++) { - + idx = 0; + p = l; + while (p != NULL) { if (checkAttribute(p,"tmap:in:numinputs","0")) { p = Getattr(p,"tmap:in:next"); continue; @@ -2752,7 +2751,9 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { Wrapper_add_localv(w, "c_result", SwigType_lstr(return_type, "c_result"), NIL); } /* declare Python return value */ - Wrapper_add_local(w, "result", "PyObject *result"); + if (!is_void) { + Wrapper_add_local(w, "result", "PyObject *result"); + } /* direct call to superclass if _up is set */ Printf(w->code, "if (swig_get_up()) {\n"); @@ -2782,11 +2783,13 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { Printf(w->code, " Swig::DirectorException::raise(\"'self' unitialized, maybe you forgot to call %s.__init__.\");\n", classname); Printf(w->code, "}\n"); if (Len(parse_args) > 0) { - Printf(w->code, "result = PyObject_CallMethod(swig_get_self(), (char *)\"%s\", (char *)\"(%s)\" %s);\n", pyname, parse_args, arglist); + Printf(w->code, "%s PyObject_CallMethod(swig_get_self(), (char *)\"%s\", (char *)\"(%s)\" %s);\n", (!is_void ? "result =" : "(void)"), pyname, parse_args, arglist); } else { - Printf(w->code, "result = PyObject_CallMethod(swig_get_self(), (char *)\"%s\", NULL);\n", pyname); + Printf(w->code, "%s PyObject_CallMethod(swig_get_self(), (char *)\"%s\", NULL);\n", (!is_void ? "result =" : "(void)"), pyname); + } + if (!is_void) { + Printv(xdecref, "Py_XDECREF(result);\n", NULL); } - Printv(xdecref, "Py_XDECREF(result);\n", NULL); if (dirprot_mode() && !is_public(n)) Printf(w->code, "swig_set_inner(\"%s\", false);\n", name); @@ -2795,7 +2798,9 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { if (!tm) { tm = Getattr(n, "feature:director:except"); } - Printf(w->code, "if (result == NULL) {\n"); + if (!is_void) { + Printf(w->code, "if (result == NULL) {\n"); + } Printf(w->code, " PyObject *error = PyErr_Occurred();\n"); if ((tm) && Len(tm) && (Strcmp(tm, "1") != 0)) { Replaceall(tm, "$error", "error"); @@ -2807,7 +2812,9 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { Printf(w->code, " }\n"); } - Printf(w->code, "}\n"); + if (!is_void) { + Printf(w->code, "}\n"); + } /* * Python method may return a simple object, or a tuple.