From c24431e94bc491f56001eb665b0a3b6689304595 Mon Sep 17 00:00:00 2001
From: "[6~" <inform@tiker.net>
Date: Sat, 23 May 2020 20:14:32 -0500
Subject: [PATCH] Import pyopencl.cltypes on main package import, to make sure
 dtype registry is fully populated

---
 pyopencl/__init__.py |  4 +++-
 pyopencl/tools.py    | 12 +++++++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/pyopencl/__init__.py b/pyopencl/__init__.py
index 0f302343..8b6a29b1 100644
--- a/pyopencl/__init__.py
+++ b/pyopencl/__init__.py
@@ -29,6 +29,9 @@ from six.moves import input, intern
 
 from pyopencl.version import VERSION, VERSION_STATUS, VERSION_TEXT  # noqa
 
+# must import, otherwise dtype registry will not be fully populated
+import pyopencl.cltypes  # noqa: F401
+
 import logging
 logger = logging.getLogger(__name__)
 
@@ -1811,7 +1814,6 @@ def image_from_array(ctx, ary, num_channels=None, mode="r", norm_int=False):
     dtype = ary.dtype
     if num_channels is None:
 
-        import pyopencl.cltypes
         try:
             dtype, num_channels = \
                     pyopencl.cltypes.vec_type_to_scalar_and_count[dtype]
diff --git a/pyopencl/tools.py b/pyopencl/tools.py
index 6e13479b..109e0be0 100644
--- a/pyopencl/tools.py
+++ b/pyopencl/tools.py
@@ -31,9 +31,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 import six
 from six.moves import zip, intern
 
+# Do not add a pyopencl import here: This will add an import cycle.
+
 import numpy as np
 from decorator import decorator
-import pyopencl as cl
 from pytools import memoize, memoize_method
 from pyopencl._cl import bitlog2  # noqa: F401
 from pytools.persistent_dict import KeyBuilder as KeyBuilderBase
@@ -177,6 +178,8 @@ def get_test_platforms_and_devices(plat_dev_string=None):
     :return: list of tuples (platform, [device, device, ...])
     """
 
+    import pyopencl as cl
+
     if plat_dev_string is None:
         import os
         plat_dev_string = os.environ.get("PYOPENCL_TEST", None)
@@ -227,6 +230,8 @@ def get_test_platforms_and_devices(plat_dev_string=None):
 
 
 def pytest_generate_tests_for_pyopencl(metafunc):
+    import pyopencl as cl
+
     class ContextFactory:
         def __init__(self, device):
             self.device = device
@@ -404,6 +409,8 @@ def get_arg_offset_adjuster_code(arg_types):
 
 
 def get_gl_sharing_context_properties():
+    import pyopencl as cl
+
     ctx_props = cl.context_properties
 
     from OpenGL import platform as gl_platform
@@ -545,6 +552,8 @@ def match_dtype_to_c_struct(device, name, dtype, context=None):
     function, not the original one.
     """
 
+    import pyopencl as cl
+
     fields = sorted(six.iteritems(dtype.fields),
             key=lambda name_dtype_offset: name_dtype_offset[1][1])
 
@@ -788,6 +797,7 @@ class _TemplateRenderer(object):
         return str(result)
 
     def get_rendered_kernel(self, txt, kernel_name):
+        import pyopencl as cl
         prg = cl.Program(self.context, self(txt)).build(self.options)
 
         kernel_name_prefix = self.var_dict.get("kernel_name_prefix")
-- 
GitLab