Allow cppclasses to be declared nogil.
authorJohn Stumpo <stump@jstump.com>
Sun, 14 Apr 2013 18:52:07 +0000 (20:52 +0200)
committerJohn Stumpo <stump@jstump.com>
Sun, 14 Apr 2013 18:52:07 +0000 (20:52 +0200)
Doing so just makes all methods implicitly nogil. This also makes nogil
on a cdef extern from statement do the right thing to cppclasses (and
their methods) contained inside.
---
 Cython/Compiler/Parsing.py  |    3 ++-
 tests/compile/cpp_nogil.h   |    9 +++++++++
 tests/compile/cpp_nogil.pyx |   18 ++++++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 tests/compile/cpp_nogil.h
 create mode 100644 tests/compile/cpp_nogil.pyx

Cython/Compiler/Parsing.py
tests/compile/cpp_nogil.h [new file with mode: 0644]
tests/compile/cpp_nogil.pyx [new file with mode: 0644]

index 86006d0..ccdc8c7 100644 (file)
@@ -3128,12 +3128,13 @@ def p_cpp_class_definition(s, pos,  ctx):
         base_classes = []
     if s.sy == '[':
         error(s.position(), "Name options not allowed for C++ class")
+    nogil = p_nogil(s)
     if s.sy == ':':
         s.next()
         s.expect('NEWLINE')
         s.expect_indent()
         attributes = []
-        body_ctx = Ctx(visibility = ctx.visibility, level='cpp_class')
+        body_ctx = Ctx(visibility = ctx.visibility, level='cpp_class', nogil=nogil or ctx.nogil)
         body_ctx.templates = templates
         while s.sy != 'DEDENT':
             if s.systring == 'cppclass':
diff --git a/tests/compile/cpp_nogil.h b/tests/compile/cpp_nogil.h
new file mode 100644 (file)
index 0000000..101b9c8
--- /dev/null
@@ -0,0 +1,9 @@
+struct NoGilTest1 {
+  NoGilTest1() { }
+  void doSomething() { }
+};
+
+struct NoGilTest2 {
+  NoGilTest2() { }
+  void doSomething() { }
+};
diff --git a/tests/compile/cpp_nogil.pyx b/tests/compile/cpp_nogil.pyx
new file mode 100644 (file)
index 0000000..e712980
--- /dev/null
@@ -0,0 +1,18 @@
+# tag: cpp
+# mode: compile
+
+cdef extern from "cpp_nogil.h" nogil:
+    cdef cppclass NoGilTest1:
+        NoGilTest1()
+        void doSomething()
+
+# This is declared in cpp_nogil.h, but here we're testing
+# that we can put nogil directly on the cppclass.
+cdef extern from *:
+    cdef cppclass NoGilTest2 nogil:
+        NoGilTest2()
+        void doSomething()
+
+with nogil:
+    NoGilTest1().doSomething()
+    NoGilTest2().doSomething()