From f6fec7ad2fc4d0294bdb718adf266697338a135e Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Sat, 10 Mar 2012 05:56:40 +0000 Subject: [PATCH] fixed python GIL reacquire in case of exception --- modules/python/src2/cv2.cpp | 29 +++++++++++++---------------- modules/python/src2/gen2.py | 5 +---- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 8255265..dff5a35a 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -37,9 +37,22 @@ static int failmsg(const char *fmt, ...) return 0; } +class PyAllowThreads +{ +public: + PyAllowThreads() : _state(PyEval_SaveThread()) {} + ~PyAllowThreads() + { + PyEval_RestoreThread(_state); + } +private: + PyThreadState* _state; +}; + #define ERRWRAP2(expr) \ try \ { \ + PyAllowThreads allowThreads; \ expr; \ } \ catch (const cv::Exception &e) \ @@ -105,22 +118,6 @@ static inline int* refcountFromPyObject(const PyObject* obj) return (int*)((size_t)obj + REFCOUNT_OFFSET); } -class PyAllowThreads -{ -public: - PyAllowThreads() : _state(PyEval_SaveThread()) - { - //printf("+GIL\n"); - } - ~PyAllowThreads() - { - PyEval_RestoreThread(_state); - //printf("-GIL\n"); - } -private: - PyThreadState* _state; -}; - class NumpyAllocator : public MatAllocator { public: diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index 5eeaaa0..830c378 100644 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -19,10 +19,7 @@ gen_template_parse_args = Template("""const char* keywords[] = { $kw_list, NULL gen_template_func_body = Template("""$code_decl $code_parse { - { - PyAllowThreads allow; - $code_fcall; - } + $code_fcall; $code_ret; } """) -- 2.7.4