Repository: numba/numba

Issue:
Example 1.7.3 in Numba docs doesn't work with Numba 0.26.0 on Mac OS X
This code:

```
from numba import cfunc, types, carray

c_sig = types.void(types.CPointer(types.double),
                   types.CPointer(types.double),
                   types.intc, types.intc)

@cfunc(c_sig)
def my_callback(in_, out, m, n):
    in_array = carray(in_, (m, n))
    out_array = carray(out, (m, n))
    for i in range(m):
        for j in range(n):
            out_array[i, j] = 2 * in_array[i, j]
```

in file, foo.py, and then executing `python foo.py` results in an error (output below).

Conda info:

```
Current conda install:

             platform : osx-64
        conda version : 4.0.7
  conda-build version : 1.20.3
       python version : 3.5.1.final.0
```

Numba and llvmlite versions:

```
numba                     0.26.0              np111py35_0
llvmlite                  0.11.0                   py35_0
```

Error message:

```
Traceback (most recent call last):
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 196, in lower_block
    self.lower_inst(inst)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 238, in lower_inst
    val = self.lower_assign(ty, inst)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 380, in lower_assign
    return self.lower_expr(ty, value)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 622, in lower_expr
    res = self.lower_call(resty, expr)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 588, in lower_call
    res = impl(self.builder, argvals)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/targets/base.py", line 1105, in __call__
    return self._imp(self._context, builder, self._sig, args)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/targets/arrayobj.py", line 3226, in np_cfarray
    off = builder.mul(off, s)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/llvmlite/ir/builder.py", line 24, in wrapped
    % (lhs.type, rhs.type))
ValueError: Operands must be the same type, got (i64, i32)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ftest.py", line 7, in <module>
    @cfunc(c_sig)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/decorators.py", line 225, in wrapper
    res.compile()
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/ccallback.py", line 65, in compile
    cres = self._compile_uncached()
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/ccallback.py", line 78, in _compile_uncached
    cres = self._compiler.compile(sig.args, sig.return_type)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/dispatcher.py", line 76, in compile
    flags=flags, locals=self.locals)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 696, in compile_extra
    return pipeline.compile_extra(func)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 369, in compile_extra
    return self.compile_bytecode(bc, func_attr=self.func_attr)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 378, in compile_bytecode
    return self._compile_bytecode()
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 662, in _compile_bytecode
    res = pm.run(self.status)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 257, in run
    raise patched_exception
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 249, in run
    stage()
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 607, in stage_nopython_backend
    self._backend(lowerfn, objectmode=False)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 562, in _backend
    lowered = lowerfn()
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 549, in backend_nopython_mode
    self.flags)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/compiler.py", line 824, in native_lowering_stage
    lower.lower()
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 123, in lower
    self.lower_normal_function(self.fndesc)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 158, in lower_normal_function
    entry_block_tail = self.lower_function_body()
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 183, in lower_function_body
    self.lower_block(block)
  File "/Users/talumbau/miniconda3/envs/webdemo/lib/python3.5/site-packages/numba/lowering.py", line 201, in lower_block
    raise LoweringError(msg, inst.loc)
numba.errors.LoweringError: Failed at nopython (nopython mode backend)
Internal error:
ValueError: Operands must be the same type, got (i64, i32)
```



Discussion:
also broken on Linux64 with this Python:

```
ubuntu@ip-172-31-60-154:~$ python
Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec  7 2015, 11:16:01)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
```

and these packages:

```
llvmlite                  0.11.0                   py35_0
mkl                       11.3.3                        0
numba                     0.26.0              np111py35_0
numpy                     1.11.0                   py35_1
```

@pitrou: Can you take a look at this tomorrow?  We want to figure this out ASAP.  Thanks!


Resolving patch:
diff --git a/numba/targets/arrayobj.py b/numba/targets/arrayobj.py
--- a/numba/targets/arrayobj.py
+++ b/numba/targets/arrayobj.py
@@ -3212,7 +3212,10 @@ def np_cfarray(context, builder, sig, args):
     if isinstance(shapety, types.BaseTuple):
         shapes = cgutils.unpack_tuple(builder, shape)
     else:
+        shapety = (shapety,)
         shapes = (shape,)
+    shapes = [context.cast(builder, value, fromty, types.intp)
+              for fromty, value in zip(shapety, shapes)]
 
     off = ll_itemsize
     strides = []


Test patch:
diff --git a/numba/tests/test_cfunc.py b/numba/tests/test_cfunc.py
--- a/numba/tests/test_cfunc.py
+++ b/numba/tests/test_cfunc.py
@@ -13,8 +13,6 @@
 
 from numba import unittest_support as unittest
 from numba import cfunc, carray, farray, types, typing, utils
-from numba.types.abstract import _typecache
-from numba import jit, numpy_support
 from .support import TestCase, tag, captured_stderr
 from .test_dispatcher import BaseCacheTest
 
@@ -84,7 +82,6 @@ def cfarray_usecase(in_ptr, out_ptr, m, n):
 carray_dtype_usecase = make_cfarray_dtype_usecase(carray)
 farray_dtype_usecase = make_cfarray_dtype_usecase(farray)
 
-
 carray_float32_usecase_sig = types.void(types.CPointer(types.float32),
                                         types.CPointer(types.float32),
                                         types.intp, types.intp)
@@ -302,16 +299,29 @@ def test_farray(self):
         """
         self.check_carray_farray(farray, 'F')
 
+    def make_carray_sigs(self, formal_sig):
+        """
+        Generate a bunch of concrete signatures by varying the width
+        and signedness of size arguments (see issue #1923).
+        """
+        for actual_size in (types.intp, types.int32, types.intc,
+                            types.uintp, types.uint32, types.uintc):
+            args = tuple(actual_size if a == types.intp else a
+                         for a in formal_sig.args)
+            yield formal_sig.return_type(*args)
+
     def check_numba_carray_farray(self, usecase, dtype_usecase):
         # With typed pointers and implicit dtype
         pyfunc = usecase
-        f = cfunc(carray_float32_usecase_sig)(pyfunc)
-        self.check_carray_usecase(self.make_float32_pointer, pyfunc, f.ctypes)
+        for sig in self.make_carray_sigs(carray_float32_usecase_sig):
+            f = cfunc(sig)(pyfunc)
+            self.check_carray_usecase(self.make_float32_pointer, pyfunc, f.ctypes)
 
         # With typed pointers and explicit (matching) dtype
         pyfunc = dtype_usecase
-        f = cfunc(carray_float32_usecase_sig)(pyfunc)
-        self.check_carray_usecase(self.make_float32_pointer, pyfunc, f.ctypes)
+        for sig in self.make_carray_sigs(carray_float32_usecase_sig):
+            f = cfunc(sig)(pyfunc)
+            self.check_carray_usecase(self.make_float32_pointer, pyfunc, f.ctypes)
         # With typed pointers and mismatching dtype
         with self.assertTypingError() as raises:
             f = cfunc(carray_float64_usecase_sig)(pyfunc)
@@ -320,8 +330,9 @@ def check_numba_carray_farray(self, usecase, dtype_usecase):
 
         # With voidptr
         pyfunc = dtype_usecase
-        f = cfunc(carray_voidptr_usecase_sig)(pyfunc)
-        self.check_carray_usecase(self.make_float32_pointer, pyfunc, f.ctypes)
+        for sig in self.make_carray_sigs(carray_voidptr_usecase_sig):
+            f = cfunc(sig)(pyfunc)
+            self.check_carray_usecase(self.make_float32_pointer, pyfunc, f.ctypes)
 
     @tag('important')
     def test_numba_carray(self):