From 5cf7be8ff63c5e89b1d0613e7371684dece3c4f0 Mon Sep 17 00:00:00 2001
From: Robert Bradshaw <robertwb@gmail.com>
Date: Tue, 7 Nov 2017 22:16:14 -0800
Subject: [PATCH] Require GIL-holding constructor if base classes require GIL.

This fixes #1986.
---
 Cython/Compiler/Symtab.py             |  6 ++++++
 tests/errors/cpp_class_gil_GH1986.pyx | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 tests/errors/cpp_class_gil_GH1986.pyx

diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py
index 0b12c7b6c..c61892de0 100644
--- a/Cython/Compiler/Symtab.py
+++ b/Cython/Compiler/Symtab.py
@@ -2342,6 +2342,12 @@ class CppClassScope(Scope):
             cname = "%s__dealloc__%s" % (Naming.func_prefix, class_name)
             name = '<del>'
             type.return_type = PyrexTypes.CVoidType()
+        if name in ('<init>', '<del>') and type.nogil:
+            for base in self.type.base_classes:
+                base_entry = base.scope.lookup(name)
+                if base_entry and not base_entry.type.nogil:
+                    error(pos, "Constructor cannot be called without GIL unless all base constructors can also be called without GIL")
+                    error(base_entry.pos, "Base constructor defined here.")
         prev_entry = self.lookup_here(name)
         entry = self.declare_var(name, type, pos,
                                  defining=defining,
diff --git a/tests/errors/cpp_class_gil_GH1986.pyx b/tests/errors/cpp_class_gil_GH1986.pyx
new file mode 100644
index 000000000..b4d26ec2b
--- /dev/null
+++ b/tests/errors/cpp_class_gil_GH1986.pyx
@@ -0,0 +1,20 @@
+# tag: cpp
+# mode: error
+
+
+cdef cppclass Base:
+    __init__() nogil:
+        pass
+
+cdef cppclass Sub1(Base):
+    __init__(): # implicit requires GIL
+        pass
+
+cdef cppclass Sub2(Sub1):
+    __init__() nogil:
+        pass
+
+_ERRORS = u"""
+10:4: Base constructor defined here.
+14:4: Constructor cannot be called without GIL unless all base constructors can also be called without GIL
+"""
-- 
2.30.9