Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
77bf40fa
Commit
77bf40fa
authored
Jan 13, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support xrange() in Py3 by mapping it to range()
parent
9e3febb3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
8 deletions
+68
-8
Cython/Compiler/Code.py
Cython/Compiler/Code.py
+22
-7
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+2
-1
tests/run/builtin_py3.pyx
tests/run/builtin_py3.pyx
+44
-0
No files found.
Cython/Compiler/Code.py
View file @
77bf40fa
...
...
@@ -619,15 +619,30 @@ class GlobalState(object):
def add_cached_builtin_decl(self, entry):
if Options.cache_builtins:
if self.should_declare(entry.cname, entry):
interned_cname = self.get_interned_identifier(entry.name).cname
self.put_pyobject_decl(entry)
w = self.parts['cached_builtins']
w.putln('%s = __Pyx_GetName(%s, %s); if (!%s) %s' % (
entry.cname,
Naming.builtins_cname,
interned_cname,
entry.cname,
w.error_goto(entry.pos)))
if entry.name == 'xrange':
# replaced by range() in Py3
w.putln('#if PY_MAJOR_VERSION >= 3')
self.put_cached_builtin_init(
entry.pos, StringEncoding.EncodedString('range'),
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):
self.generate_string_constants()
...
...
Cython/Compiler/Symtab.py
View file @
77bf40fa
...
...
@@ -727,7 +727,8 @@ class ModuleScope(Scope):
return self
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:
entry = self.declare_var(name, py_object_type, pos)
return entry
...
...
tests/run/builtin_py3.pyx
0 → 100644
View file @
77bf40fa
__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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment