Commit 77bf40fa authored by Stefan Behnel's avatar Stefan Behnel

support xrange() in Py3 by mapping it to range()

parent 9e3febb3
...@@ -619,15 +619,30 @@ class GlobalState(object): ...@@ -619,15 +619,30 @@ class GlobalState(object):
def add_cached_builtin_decl(self, entry): def add_cached_builtin_decl(self, entry):
if Options.cache_builtins: if Options.cache_builtins:
if self.should_declare(entry.cname, entry): if self.should_declare(entry.cname, entry):
interned_cname = self.get_interned_identifier(entry.name).cname
self.put_pyobject_decl(entry) self.put_pyobject_decl(entry)
w = self.parts['cached_builtins'] w = self.parts['cached_builtins']
w.putln('%s = __Pyx_GetName(%s, %s); if (!%s) %s' % ( if entry.name == 'xrange':
entry.cname, # replaced by range() in Py3
Naming.builtins_cname, w.putln('#if PY_MAJOR_VERSION >= 3')
interned_cname, self.put_cached_builtin_init(
entry.cname, entry.pos, StringEncoding.EncodedString('range'),
w.error_goto(entry.pos))) entry.cname)
w.putln('#else')
self.put_cached_builtin_init(
entry.pos, StringEncoding.EncodedString(entry.name),
entry.cname)
if entry.name == 'xrange':
w.putln('#endif')
def put_cached_builtin_init(self, pos, name, cname):
w = self.parts['cached_builtins']
interned_cname = self.get_interned_identifier(name).cname
w.putln('%s = __Pyx_GetName(%s, %s); if (!%s) %s' % (
cname,
Naming.builtins_cname,
interned_cname,
cname,
w.error_goto(pos)))
def generate_const_declarations(self): def generate_const_declarations(self):
self.generate_string_constants() self.generate_string_constants()
......
...@@ -727,7 +727,8 @@ class ModuleScope(Scope): ...@@ -727,7 +727,8 @@ class ModuleScope(Scope):
return self return self
def declare_builtin(self, name, pos): def declare_builtin(self, name, pos):
if not hasattr(builtins, name): if not hasattr(builtins, name) and name != 'xrange':
# 'xrange' is special cased in Code.py
if self.has_import_star: if self.has_import_star:
entry = self.declare_var(name, py_object_type, pos) entry = self.declare_var(name, py_object_type, pos)
return entry return entry
......
__doc__ = u"""
>>> test_xrange()
0
1
2
>>> test_range()
0
1
2
>>> test_long() == 12
True
>>> test_int() == 12
True
"""
# the builtins 'xrange' and 'long' are not available in Py3, but they
# can safely be replaced by 'range' and 'int' on that platform
import sys
IS_PY3 = sys.version_info[0] >= 3
def test_xrange():
r = xrange(3)
assert type(r) is xrange
for i in r:
print i
def test_range():
r = range(3)
assert (type(r) is range) if IS_PY3 else (type(r) is list)
for i in r:
print i
def test_long():
long_val = long(12)
assert type(long_val) is long
return long_val
def test_int():
int_val = int(12)
assert type(int_val) is int
return int_val
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment