Repository: numba/numba

Issue:
xrange does not work on Python 2.7
From mailing list:

"I recently encountered a problem when upgrading from numba0.22.1 to numba0.23.0 : the function xrange is not recognize.

This error arises for instance in the simple code below:"

``` python
import numba

@numba.jit(nopython=True)
def test():
    s = 0
    for i in xrange(2):
        s += i
    return(s)

if __name__=='__main__':
    print test()
```



Discussion:
+1

```
  File "/usr/local/lib/python2.7/site-packages/numba/dispatcher.py", line 171, in _compile_for_args
    return self.compile(sig)
  File "/usr/local/lib/python2.7/site-packages/numba/dispatcher.py", line 349, in compile
    flags=flags, locals=self.locals)
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 665, in compile_extra
    return pipeline.compile_extra(func)
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 366, in compile_extra
    return self.compile_bytecode(bc, func_attr=self.func_attr)
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 375, in compile_bytecode
    return self._compile_bytecode()
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 652, in _compile_bytecode
    return pm.run(self.status)
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 251, in run
    raise patched_exception
TypingError: Caused By:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 243, in run
    res = stage()
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 463, in stage_nopython_frontend
    self.locals)
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 779, in type_inference_stage
    infer.build_constraint()
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 549, in build_constraint
    self.constrain_statement(inst)
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 672, in constrain_statement
    self.typeof_assign(inst)
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 722, in typeof_assign
    self.typeof_global(inst, inst.target, value)
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 788, in typeof_global
    loc=inst.loc)
TypingError: Untyped global name 'xrange'

```


Resolving patch:
diff --git a/numba/typing/builtins.py b/numba/typing/builtins.py
--- a/numba/typing/builtins.py
+++ b/numba/typing/builtins.py
@@ -61,7 +61,6 @@ class Slice(ConcreteTemplate):
     ]
 
 
-@infer_global(range)
 class Range(ConcreteTemplate):
     cases = [
         signature(types.range_state32_type, types.int32),
@@ -78,6 +77,9 @@ class Range(ConcreteTemplate):
                   types.uint64),
     ]
 
+for func in RANGE_ITER_OBJECTS:
+    infer_global(func, typing_key=range)(Range)
+
 
 @infer
 class GetIter(AbstractTemplate):


Test patch:
diff --git a/numba/tests/test_range.py b/numba/tests/test_range.py
--- a/numba/tests/test_range.py
+++ b/numba/tests/test_range.py
@@ -1,6 +1,11 @@
 from __future__ import print_function
+
 import numba.unittest_support as unittest
+
+import sys
+
 import numpy
+
 from numba.compiler import compile_isolated
 from numba import types
 
@@ -50,7 +55,15 @@ def range3_writeout(a, b, c, out):
     return i
 
 
+def xrange_usecase(n):
+    s = 0
+    for i in xrange(n):
+        s += i
+    return s
+
+
 class TestRange(unittest.TestCase):
+
     def test_loop1_int16(self):
         pyfunc = loop1
         cres = compile_isolated(pyfunc, [types.int16])
@@ -148,6 +161,13 @@ def test_range3_writeout_uint32(self):
         self.assertTrue(cfunc(a, b, c, got), pyfunc(a, b, c, expected))
         numpy.testing.assert_equal(expected, got)
 
+    @unittest.skipIf(sys.version_info >= (3,), "test is Python 2-specific")
+    def test_xrange(self):
+        pyfunc = xrange_usecase
+        cres = compile_isolated(pyfunc, (types.int32,))
+        cfunc = cres.entry_point
+        self.assertEqual(cfunc(5), pyfunc(5))
+
 
 if __name__ == '__main__':
     unittest.main()