Commit 52f5548e authored by Stefan Behnel's avatar Stefan Behnel

make compile time DEF env less Py2/3 dependent

parent 9eac2bf3
......@@ -103,13 +103,17 @@ def initial_compile_time_env():
except ImportError:
import builtins
names = ('False', 'True',
'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes',
'chr', 'cmp', 'complex', 'dict', 'divmod', 'enumerate', 'filter',
'float', 'format', 'frozenset', 'hash', 'hex', 'int', 'len',
'list', 'long', 'map', 'max', 'min', 'oct', 'ord', 'pow', 'range',
'repr', 'reversed', 'round', 'set', 'slice', 'sorted', 'str',
'sum', 'tuple', 'xrange', 'zip')
names = (
'False', 'True',
'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes',
'chr', 'cmp', 'complex', 'dict', 'divmod', 'enumerate', 'filter',
'float', 'format', 'frozenset', 'hash', 'hex', 'int', 'len',
'list', 'map', 'max', 'min', 'oct', 'ord', 'pow', 'range',
'repr', 'reversed', 'round', 'set', 'slice', 'sorted', 'str',
'sum', 'tuple', 'zip',
### defined below in a platform independent way
# 'long', 'unicode', 'reduce', 'xrange'
)
for name in names:
try:
......@@ -117,6 +121,14 @@ def initial_compile_time_env():
except AttributeError:
# ignore, likely Py3
pass
# Py2/3 adaptations
from functools import reduce
benv.declare('reduce', reduce)
benv.declare('unicode', getattr(builtins, 'unicode', getattr(builtins, 'str')))
benv.declare('long', getattr(builtins, 'long', getattr(builtins, 'int')))
benv.declare('xrange', getattr(builtins, 'xrange', getattr(builtins, 'range')))
denv = CompileTimeScope(benv)
return denv
......
......@@ -569,7 +569,7 @@ Compile-Time Definitions
A compile-time constant can be defined using the DEF statement::
DEF FavouriteFood = "spam"
DEF FavouriteFood = u"spam"
DEF ArraySize = 42
DEF OtherArraySize = 2 * ArraySize + 17
......@@ -586,16 +586,21 @@ returned by :func:`os.uname`.
The following selection of builtin constants and functions are also available:
None, True, False,
abs, bool, chr, cmp, complex, dict, divmod, enumerate,
float, hash, hex, int, len, list, long, map, max, min,
oct, ord, pow, range, reduce, repr, round, slice, str,
sum, tuple, xrange, zip
abs, all, any, ascii, bin, bool, bytearray, bytes, chr, cmp, complex, dict,
divmod, enumerate, filter, float, format, frozenset, hash, hex, int, len,
list, long, map, max, min, oct, ord, pow, range, reduce, repr, reversed,
round, set, slice, sorted, str, sum, tuple, xrange, zip
Note that some of these builtins may not be available when compiling under
Python 2.x or 3.x, or may behave differently in both.
A name defined using ``DEF`` can be used anywhere an identifier can appear,
and it is replaced with its compile-time value as though it were written into
the source at that point as a literal. For this to work, the compile-time
expression must evaluate to a Python value of type ``int``, ``long``,
``float`` or ``str``.::
``float``, ``bytes`` or ``unicode`` (``str`` in Py3).
::
cdef int a1[ArraySize]
cdef int a2[OtherArraySize]
......
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