Blob Blame History Raw
From e5e34262c0ee9b6b4f3eda371fd0a206a05da2d0 Mon Sep 17 00:00:00 2001
From: Scott Talbert <swt@techie.net>
Date: Fri, 30 Oct 2020 20:32:52 -0400
Subject: [PATCH] Avoid releasing the GIL in wxCustomDataObject.GetData

This avoids a crash because GetData calls i_wxPyMakeBuffer which calls
Python C API, for which the GIL needs to be held.
---
 etg/dataobj.py            |  2 +-
 etgtools/sip_generator.py | 12 ++++++++----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/etg/dataobj.py b/etg/dataobj.py
index e59632df..a8adc980 100644
--- a/etg/dataobj.py
+++ b/etg/dataobj.py
@@ -335,7 +335,7 @@ def run():
     c.find('TakeData').ignore()
 
     c.find('GetData').ignore()
-    c.addCppMethod('PyObject*', 'GetData', '()', isConst=True,
+    c.addCppMethod('PyObject*', 'GetData', '()', isConst=True, pyHoldGIL=True,
         doc="Returns a reference to the data buffer.",
         body="return wxPyMakeBuffer(self->GetData(), self->GetSize());")
 
diff --git a/etgtools/sip_generator.py b/etgtools/sip_generator.py
index 4fd119ac..778642c8 100644
--- a/etgtools/sip_generator.py
+++ b/etgtools/sip_generator.py
@@ -863,7 +863,8 @@ from .%s import *
 
         else:
             stream.write('PyErr_Clear();\n')
-            stream.write('%sPy_BEGIN_ALLOW_THREADS\n' % (indent+' '*4))
+            if not method.pyHoldGIL:
+                stream.write('%sPy_BEGIN_ALLOW_THREADS\n' % (indent+' '*4))
             stream.write(indent+' '*4)
             if method.type != 'void':
                 stream.write('sipRes = ')
@@ -882,7 +883,8 @@ from .%s import *
                     stream.write('%s(%s%s);\n' % (fname, argname, pnames))
             else:
                 stream.write('%s(%s);\n' % (fname, pnames))
-            stream.write('%sPy_END_ALLOW_THREADS\n' % (indent+' '*4))
+            if not method.pyHoldGIL:
+                stream.write('%sPy_END_ALLOW_THREADS\n' % (indent+' '*4))
             stream.write('%sif (PyErr_Occurred()) sipIsErr = 1;\n' % (indent+' '*4))
         stream.write('%s%%End\n' % indent)
 
-- 
2.26.2