From 033b7f4883fe613292fac82a0328787270dea183 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Mon, 15 Jun 2020 13:06:09 -0500
Subject: [PATCH] Empty array: set base_data to None instead of allocating
 bogus 1-byte buffer

---
 pyopencl/array.py | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/pyopencl/array.py b/pyopencl/array.py
index bcc0770f..b25ffc57 100644
--- a/pyopencl/array.py
+++ b/pyopencl/array.py
@@ -483,23 +483,22 @@ class Array(object):
         self.allocator = allocator
 
         if data is None:
-            if alloc_nbytes <= 0:
-                if alloc_nbytes == 0:
-                    # Work around CL not allowing zero-sized buffers.
-                    alloc_nbytes = 1
+            if alloc_nbytes < 0:
+                raise ValueError("cannot allocate CL buffer with "
+                        "negative size")
 
-                else:
-                    raise ValueError("cannot allocate CL buffer with "
-                            "negative size")
-
-            if allocator is None:
-                if context is None and queue is not None:
-                    context = queue.context
+            elif alloc_nbytes == 0:
+                self.base_data = None
 
-                self.base_data = cl.Buffer(
-                        context, cl.mem_flags.READ_WRITE, alloc_nbytes)
             else:
-                self.base_data = self.allocator(alloc_nbytes)
+                if allocator is None:
+                    if context is None and queue is not None:
+                        context = queue.context
+
+                    self.base_data = cl.Buffer(
+                            context, cl.mem_flags.READ_WRITE, alloc_nbytes)
+                else:
+                    self.base_data = self.allocator(alloc_nbytes)
         else:
             self.base_data = data
 
-- 
GitLab