From 0961b9a758592b667495e9155267c6b337b8b214 Mon Sep 17 00:00:00 2001
From: Stefan Behnel <stefan_ml@behnel.de>
Date: Sun, 11 Feb 2018 22:23:40 +0100
Subject: [PATCH] Disallow memory views as struct members since structs do not
 allow safe buffer reference management.

---
 Cython/Compiler/ExprNodes.py | 1 +
 Cython/Compiler/Symtab.py    | 9 +++++----
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
index fefd76a8f..ff662558d 100644
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -9170,6 +9170,7 @@ class PyCFunctionNode(ExprNode, ModuleNameMixin):
                 if not arg.default.is_literal:
                     arg.is_dynamic = True
                     if arg.type.is_pyobject:
+                        # FIXME: should we include memory views here?
                         nonliteral_objects.append(arg)
                     else:
                         nonliteral_other.append(arg)
diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py
index 05e8ecfd7..fac61eb6f 100644
--- a/Cython/Compiler/Symtab.py
+++ b/Cython/Compiler/Symtab.py
@@ -1874,11 +1874,12 @@ class StructOrUnionScope(Scope):
         entry.is_variable = 1
         self.var_entries.append(entry)
         if type.is_pyobject and not allow_pyobject:
-            error(pos,
-                  "C struct/union member cannot be a Python object")
+            error(pos, "C struct/union member cannot be a Python object")
+        elif type.is_memoryviewslice and not allow_pyobject:
+            # Memory views wrap their buffer owner as a Python object.
+            error(pos, "C struct/union member cannot be a memory view")
         if visibility != 'private':
-            error(pos,
-                  "C struct/union member cannot be declared %s" % visibility)
+            error(pos, "C struct/union member cannot be declared %s" % visibility)
         return entry
 
     def declare_cfunction(self, name, type, pos,
-- 
2.30.9