Repository: numba/numba

Issue:
LoweringError when passing kwargs to print()
- [* ] I am using the latest released version of Numba (most recent is visible in
 the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [* ] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

I tried to pass `flush=True` to `print()` in `nopython` mode, and got the error below, so I am duly reporting it. It occurs for any keyword argument passed to `print()`. I'll open a separate feature request for the ability to flush STDOUT. 

This is with numba 0.40.1, using Python 3.6 installed via macports on OS X 10.11. numba is installed using pip.

Minimal reproducer:

```
import numba as nb

@nb.njit
def test():
	print("hello", foo=True)

test()

```

resulting error:

```
numba.errors.LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
unsupported keyword arguments when calling Function(<built-in function print>)

File "minimal_reproducer.py", line 5:
def test():
	print("hello", foo=True)
 ^

[1] During: lowering "$0.5 = call $0.1($const0.2, func=$0.1, args=[Var($const0.2, min_nwa.py (5))], kws=[('foo', Var($const0.3, min_nwa.py (5)))], vararg=None)" at min_nwa.py (5)
-------------------------------------------------------------------------------
This should not have happened, a problem has occurred in Numba's internals.

Please report the error message and traceback, along with a minimal reproducer
at: https://github.com/numba/numba/issues/new

If more help is needed please feel free to speak to the Numba core developers
directly at: https://gitter.im/numba/numba

Thanks in advance for your help in improving Numba!
```


Discussion:

Resolving patch:
diff --git a/numba/rewrites/ir_print.py b/numba/rewrites/ir_print.py
--- a/numba/rewrites/ir_print.py
+++ b/numba/rewrites/ir_print.py
@@ -17,14 +17,16 @@ def match(self, func_ir, block, typemap, calltypes):
         for inst in block.find_insts(ir.Assign):
             if isinstance(inst.value, ir.Expr) and inst.value.op == 'call':
                 expr = inst.value
-                if expr.kws:
-                    # Only positional args are supported
-                    continue
                 try:
                     callee = func_ir.infer_constant(expr.func)
                 except errors.ConstantInferenceError:
                     continue
                 if callee is print:
+                    if expr.kws:
+                        # Only positional args are supported
+                        msg = ("Numba's print() function implementation does not "
+                            "support keyword arguments.")
+                        raise errors.UnsupportedError(msg, inst.loc)
                     prints[inst] = expr
         return len(prints) > 0
 


Test patch:
diff --git a/numba/tests/test_print.py b/numba/tests/test_print.py
--- a/numba/tests/test_print.py
+++ b/numba/tests/test_print.py
@@ -6,7 +6,7 @@
 
 import numba.unittest_support as unittest
 from numba.compiler import compile_isolated, Flags
-from numba import jit, types
+from numba import jit, types, errors
 from .support import captured_stdout, tag, TestCase
 
 
@@ -38,7 +38,6 @@ def print_vararg(a, b, c):
 def print_string_vararg(a, b, c):
     print(a, "hop!", b, *c)
 
-
 def make_print_closure(x):
     def print_closure():
         return x
@@ -179,6 +178,16 @@ def bar(x):
             bar(x)
             self.assertEqual(sys.stdout.getvalue(), '[0 1 2 3 4]\nhello\n')
 
+    def test_print_w_kwarg_raises(self):
+        @jit(nopython=True)
+        def print_kwarg():
+            print('x', flush=True)
+
+        with self.assertRaises(errors.UnsupportedError) as raises:
+            print_kwarg()
+        expected = ("Numba's print() function implementation does not support "
+                    "keyword arguments.")
+        self.assertIn(raises.exception.msg, expected)
 
 if __name__ == '__main__':
     unittest.main()