diff --git a/SWIG/Lib/python/pythreads.swg b/SWIG/Lib/python/pythreads.swg index 01522544f..4660aea57 100644 --- a/SWIG/Lib/python/pythreads.swg +++ b/SWIG/Lib/python/pythreads.swg @@ -23,12 +23,10 @@ ~SWIG_Python_Thread_Block() { end(); } }; class SWIG_Python_Thread_Allow { - bool status; PyThreadState *save; public: - void end() { if (status) { PyEval_RestoreThread(save); status = false; }} - SWIG_Python_Thread_Allow() : status(true), save(PyEval_SaveThread()) {} - ~SWIG_Python_Thread_Allow() { end(); } + SWIG_Python_Thread_Allow() : save(PyEval_SaveThread()) {} + ~SWIG_Python_Thread_Allow() { PyEval_RestoreThread(save); } }; # if !defined(SWIG_PYTHON_THREAD_BEGIN_BLOCK) # define SWIG_PYTHON_THREAD_BEGIN_BLOCK SWIG_Python_Thread_Block _swig_thread_block @@ -37,16 +35,16 @@ # define SWIG_PYTHON_THREAD_END_BLOCK _swig_thread_block.end() # endif # if !defined(SWIG_PYTHON_THREAD_BEGIN_ALLOW) -# define SWIG_PYTHON_THREAD_BEGIN_ALLOW SWIG_Python_Thread_Allow _swig_thread_allow +# define SWIG_PYTHON_THREAD_BEGIN_ALLOW { SWIG_Python_Thread_Allow _swig_thread_allow # endif # if !defined(SWIG_PYTHON_THREAD_END_ALLOW) -# define SWIG_PYTHON_THREAD_END_ALLOW +# define SWIG_PYTHON_THREAD_END_ALLOW } # endif # else /* C code */ # define SWIG_PYTHON_THREAD_BEGIN_BLOCK PyGILState_STATE _swig_thread_block = PyGILState_Ensure() # define SWIG_PYTHON_THREAD_END_BLOCK PyGILState_Release(_swig_thread_block) -# define SWIG_PYTHON_THREAD_BEGIN_ALLOW PyThreadState *_swig_thread_allow = PyEval_SaveThread() -# define SWIG_PYTHON_THREAD_END_ALLOW PyEval_RestoreThread(_swig_thread_allow) +# define SWIG_PYTHON_THREAD_BEGIN_ALLOW { PyThreadState *_swig_thread_allow = PyEval_SaveThread() +# define SWIG_PYTHON_THREAD_END_ALLOW PyEval_RestoreThread(_swig_thread_allow); } # endif # else /* Old thread way, not implemented, user must provide it */ # if !defined(SWIG_PYTHON_INITIALIZE_THREADS) diff --git a/SWIG/Source/Modules/python.cxx b/SWIG/Source/Modules/python.cxx index a1f908876..5cb8ba378 100644 --- a/SWIG/Source/Modules/python.cxx +++ b/SWIG/Source/Modules/python.cxx @@ -2082,6 +2082,8 @@ public: * ------------------------------------------------------------ */ int classDirectorEnd(Node *n) { + String *classname = Swig_class_name(n); + if (dirprot_mode()) { /* This implementation uses a std::map. @@ -2113,9 +2115,14 @@ public: Printf(f_directors_h," PyObject *swig_get_method(size_t method_index, const char *method_name) const {\n"); Printf(f_directors_h," PyObject *method = vtable[method_index];\n"); Printf(f_directors_h," if (!method) {\n"); - Printf(f_directors_h," swig::PyObject_var name = PyString_FromString(method_name);\n"); - Printf(f_directors_h," vtable[method_index] = method = PyObject_GetAttr(swig_get_self(), name);\n"); - Printf(f_directors_h," Py_DECREF(swig_get_self());\n"); + Printf(f_directors_h," swig::PyObject_var name = PyString_FromString(method_name);\n"); + Printf(f_directors_h," method = PyObject_GetAttr(swig_get_self(), name);\n"); + Printf(f_directors_h," if (method == NULL) {\n"); + Printf(f_directors_h," std::string msg = \"Method in class %s doesn't exist, undefined \";\n", classname); + Printf(f_directors_h," msg += method_name;\n"); + Printf(f_directors_h," Swig::DirectorMethodException::raise(msg.c_str());\n"); + Printf(f_directors_h," }\n"); + Printf(f_directors_h," vtable[method_index] = method;\n"); Printf(f_directors_h," };\n"); Printf(f_directors_h," return method;\n"); Printf(f_directors_h," }\n"); @@ -2681,6 +2688,7 @@ public: } else { Printv(f_shadow, tab4, "__swig_destroy__ = ", module, ".", Swig_name_destroy(symname), "\n", NIL); if (!have_pythonprepend(n) && !have_pythonappend(n)) { + Printv(f_shadow, tab4, "__del__ = lambda self : None;\n", NIL); return SWIG_OK; } Printv(f_shadow, tab4, "def __del__(self):\n", NIL); @@ -3085,7 +3093,6 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { if (allow_thread) thread_end_allow(n, w->code); } Printf(w->code, "}\n"); - Printf(w->code, "{\n"); /* declare method return value * if the return value is a reference or const reference, a specialized typemap must @@ -3096,13 +3103,14 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { Printf(w->code, "%s;\n", cres); Delete(cres); } - if (allow_thread) thread_begin_block(n, w->code); - + if (allow_thread) { + thread_begin_block(n, w->code); + Printf(w->code, "{\n"); + } /* wrap complex arguments to PyObjects */ Printv(w->code, wrap_args, NIL); - /* pass the method call on to the Python object */ if (dirprot_mode() && !is_public(n)) { Printf(w->code, "swig_set_inner(\"%s\", true);\n", name); @@ -3117,9 +3125,6 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { Printf(w->code, "const char * const swig_method_name = \"%s\";\n", pyname); Printf(w->code, "PyObject* method = swig_get_method(swig_method_index, swig_method_name);\n"); - Printf(w->code, "if (method == NULL) {\n"); - Printf(w->code, " Swig::DirectorMethodException::raise(\"Method '%s.%s' doesn't exist\");\n", classname, pyname); - Printf(w->code, "}\n"); if (Len(parse_args) > 0) { if (use_parse) { Printf(w->code, "swig::PyObject_var result = PyObject_CallFunction(method, (char *)\"(%s)\" %s);\n", parse_args, arglist); @@ -3154,7 +3159,7 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { Printv(w->code, Str(tm), "\n", NIL); } else { Printf(w->code, " if (error != NULL) {\n"); - Printf(w->code, " Swig::DirectorMethodException::raise(\"Error detected when calling %s.%s\");\n", + Printf(w->code, " Swig::DirectorMethodException::raise(\"Error detected when calling '%s.%s'\");\n", classname, pyname); Printf(w->code, " }\n"); } @@ -3245,7 +3250,11 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { } /* any existing helper functions to handle this? */ - if (allow_thread) thread_end_block(n, w->code); + if (allow_thread) { + Printf(w->code, "}\n"); + thread_end_block(n, w->code); + } + if (!is_void) { String* rettype = SwigType_str(return_type, 0); if (!SwigType_isreference(return_type)) { @@ -3256,7 +3265,6 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { Delete(rettype); } Printf(w->code, "}\n"); - Printf(w->code, "}\n"); /* emit the director method */ if (status == SWIG_OK) {