From 3ad1f427fc44bf2fcef978425ec5e86c32d7db8d Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Wed, 18 Nov 2020 12:41:45 -0600
Subject: [PATCH] Array: Handle no-queue in __str__, __repr__ more gracefully

---
 pyopencl/array.py  | 7 +++++++
 test/test_array.py | 9 +++++++++
 2 files changed, 16 insertions(+)

diff --git a/pyopencl/array.py b/pyopencl/array.py
index 531f2321..cd0b7fab 100644
--- a/pyopencl/array.py
+++ b/pyopencl/array.py
@@ -798,9 +798,16 @@ class Array:
         return result
 
     def __str__(self):
+        if self.queue is None:
+            return (f"<cl.Array without queue, call with_queue() to print>")
+
         return str(self.get())
 
     def __repr__(self):
+        if self.queue is None:
+            return (f"<cl.Array at {id(self):x} without queue, "
+                    "call with_queue() to print>")
+
         result = repr(self.get())
         if result[:5] == "array":
             result = "cl.Array" + result[5:]
diff --git a/test/test_array.py b/test/test_array.py
index b5e99273..162f5284 100644
--- a/test/test_array.py
+++ b/test/test_array.py
@@ -1477,6 +1477,15 @@ def test_zero_size_array(ctx_factory, empty_shape):
     cl_array.to_device(queue, c_host)
 
 
+def test_str_without_queue(ctx_factory):
+    context = ctx_factory()
+    queue = cl.CommandQueue(context)
+
+    a = cl_array.zeros(queue, 10, dtype=np.float32).with_queue(None)
+    print(str(a))
+    print(repr(a))
+
+
 if __name__ == "__main__":
     if len(sys.argv) > 1:
         exec(sys.argv[1])
-- 
GitLab