minor fix for threads

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7967 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-10 10:11:14 +00:00
commit cdff8ff3e2
2 changed files with 27 additions and 21 deletions

View file

@ -23,12 +23,10 @@
~SWIG_Python_Thread_Block() { end(); } ~SWIG_Python_Thread_Block() { end(); }
}; };
class SWIG_Python_Thread_Allow { class SWIG_Python_Thread_Allow {
bool status;
PyThreadState *save; PyThreadState *save;
public: public:
void end() { if (status) { PyEval_RestoreThread(save); status = false; }} SWIG_Python_Thread_Allow() : save(PyEval_SaveThread()) {}
SWIG_Python_Thread_Allow() : status(true), save(PyEval_SaveThread()) {} ~SWIG_Python_Thread_Allow() { PyEval_RestoreThread(save); }
~SWIG_Python_Thread_Allow() { end(); }
}; };
# if !defined(SWIG_PYTHON_THREAD_BEGIN_BLOCK) # if !defined(SWIG_PYTHON_THREAD_BEGIN_BLOCK)
# define SWIG_PYTHON_THREAD_BEGIN_BLOCK SWIG_Python_Thread_Block _swig_thread_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() # define SWIG_PYTHON_THREAD_END_BLOCK _swig_thread_block.end()
# endif # endif
# if !defined(SWIG_PYTHON_THREAD_BEGIN_ALLOW) # 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 # endif
# if !defined(SWIG_PYTHON_THREAD_END_ALLOW) # if !defined(SWIG_PYTHON_THREAD_END_ALLOW)
# define SWIG_PYTHON_THREAD_END_ALLOW # define SWIG_PYTHON_THREAD_END_ALLOW }
# endif # endif
# else /* C code */ # else /* C code */
# define SWIG_PYTHON_THREAD_BEGIN_BLOCK PyGILState_STATE _swig_thread_block = PyGILState_Ensure() # 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_END_BLOCK PyGILState_Release(_swig_thread_block)
# define SWIG_PYTHON_THREAD_BEGIN_ALLOW PyThreadState *_swig_thread_allow = PyEval_SaveThread() # 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_END_ALLOW PyEval_RestoreThread(_swig_thread_allow); }
# endif # endif
# else /* Old thread way, not implemented, user must provide it */ # else /* Old thread way, not implemented, user must provide it */
# if !defined(SWIG_PYTHON_INITIALIZE_THREADS) # if !defined(SWIG_PYTHON_INITIALIZE_THREADS)

View file

@ -2082,6 +2082,8 @@ public:
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
int classDirectorEnd(Node *n) { int classDirectorEnd(Node *n) {
String *classname = Swig_class_name(n);
if (dirprot_mode()) { if (dirprot_mode()) {
/* /*
This implementation uses a std::map<std::string,int>. This implementation uses a std::map<std::string,int>.
@ -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 *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," PyObject *method = vtable[method_index];\n");
Printf(f_directors_h," if (!method) {\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," 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," method = PyObject_GetAttr(swig_get_self(), name);\n");
Printf(f_directors_h," Py_DECREF(swig_get_self());\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," };\n");
Printf(f_directors_h," return method;\n"); Printf(f_directors_h," return method;\n");
Printf(f_directors_h," }\n"); Printf(f_directors_h," }\n");
@ -2681,6 +2688,7 @@ public:
} else { } else {
Printv(f_shadow, tab4, "__swig_destroy__ = ", module, ".", Swig_name_destroy(symname), "\n", NIL); Printv(f_shadow, tab4, "__swig_destroy__ = ", module, ".", Swig_name_destroy(symname), "\n", NIL);
if (!have_pythonprepend(n) && !have_pythonappend(n)) { if (!have_pythonprepend(n) && !have_pythonappend(n)) {
Printv(f_shadow, tab4, "__del__ = lambda self : None;\n", NIL);
return SWIG_OK; return SWIG_OK;
} }
Printv(f_shadow, tab4, "def __del__(self):\n", NIL); 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); if (allow_thread) thread_end_allow(n, w->code);
} }
Printf(w->code, "}\n"); Printf(w->code, "}\n");
Printf(w->code, "{\n");
/* declare method return value /* declare method return value
* if the return value is a reference or const reference, a specialized typemap must * 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); Printf(w->code, "%s;\n", cres);
Delete(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 */ /* wrap complex arguments to PyObjects */
Printv(w->code, wrap_args, NIL); Printv(w->code, wrap_args, NIL);
/* pass the method call on to the Python object */ /* pass the method call on to the Python object */
if (dirprot_mode() && !is_public(n)) { if (dirprot_mode() && !is_public(n)) {
Printf(w->code, "swig_set_inner(\"%s\", true);\n", name); 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, "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, "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 (Len(parse_args) > 0) {
if (use_parse) { if (use_parse) {
Printf(w->code, "swig::PyObject_var result = PyObject_CallFunction(method, (char *)\"(%s)\" %s);\n", parse_args, arglist); 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); Printv(w->code, Str(tm), "\n", NIL);
} else { } else {
Printf(w->code, " if (error != NULL) {\n"); 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); classname, pyname);
Printf(w->code, " }\n"); Printf(w->code, " }\n");
} }
@ -3245,7 +3250,11 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) {
} }
/* any existing helper functions to handle this? */ /* 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) { if (!is_void) {
String* rettype = SwigType_str(return_type, 0); String* rettype = SwigType_str(return_type, 0);
if (!SwigType_isreference(return_type)) { if (!SwigType_isreference(return_type)) {
@ -3256,7 +3265,6 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) {
Delete(rettype); Delete(rettype);
} }
Printf(w->code, "}\n"); Printf(w->code, "}\n");
Printf(w->code, "}\n");
/* emit the director method */ /* emit the director method */
if (status == SWIG_OK) { if (status == SWIG_OK) {