Cython has moved to github.

cython-devel

view Cython/Compiler/Options.py @ 2811:6f8fc01ddab1

Verbose type inference directive.
author Robert Bradshaw <robertwb@math.washington.edu>
date Thu Jan 21 16:41:28 2010 -0800 (2 years ago)
parents d0b5d678cb9c
children 67516f2c4b1e 1beb45ddad19
line source
1 #
2 # Cython - Compilation-wide options and pragma declarations
3 #
5 cache_builtins = 1 # Perform lookups on builtin names only once
7 embed_pos_in_docstring = 0
8 gcc_branch_hints = 1
10 pre_import = None
11 docstrings = True
13 # Decref global variables in this module on exit for garbage collection.
14 # 0: None, 1+: interned objects, 2+: cdef globals, 3+: types objects
15 # Mostly for reducing noise for Valgrind, only executes at process exit
16 # (when all memory will be reclaimed anyways).
17 generate_cleanup_code = 0
19 annotate = 0
21 # This will convert statements of the form "for i in range(...)"
22 # to "for i from ..." when i is a cdef'd integer type, and the direction
23 # (i.e. sign of step) can be determined.
24 # WARNING: This may change the symantics if the range causes assignment to
25 # i to overflow. Specifically, if this option is set, an error will be
26 # raised before the loop is entered, wheras without this option the loop
27 # will execute util a overflowing value is encountered.
28 convert_range = 1
30 # Enable this to allow one to write your_module.foo = ... to overwrite the
31 # definition if the cpdef function foo, at the cost of an extra dictionary
32 # lookup on every call.
33 # If this is 0 it simply creates a wrapper.
34 lookup_module_cpdef = 0
36 # This will set local variables to None rather than NULL which may cause
37 # surpress what would be an UnboundLocalError in pure Python but eliminates
38 # checking for NULL on every use, and can decref rather than xdecref at the end.
39 # WARNING: This is a work in progress, may currently segfault.
40 init_local_none = 1
42 # Append the c file and line number to the traceback for exceptions.
43 c_line_in_traceback = 1
45 # Whether or not to embed the Python interpreter, for use in making a
46 # standalone executable. This will provide a main() method which simply
47 # executes the body of this module.
48 embed = False
51 # Declare compiler directives
52 directive_defaults = {
53 'boundscheck' : True,
54 'nonecheck' : False,
55 'embedsignature' : False,
56 'locals' : {},
57 'auto_cpdef': False,
58 'cdivision': False, # was True before 0.12
59 'cdivision_warnings': False,
60 'always_allow_keywords': False,
61 'wraparound' : True,
62 'ccomplex' : False, # use C99/C++ for complex types and arith
63 'callspec' : "",
64 'profile': False,
65 'infer_types': False,
66 'infer_types.verbose': False,
67 'autotestdict': True,
69 'warn': None,
70 'warn.undeclared': False,
72 # test support
73 'test_assert_path_exists' : [],
74 'test_fail_if_path_exists' : [],
75 }
77 # Override types possibilities above, if needed
78 directive_types = {
79 'infer_types' : bool, # values can be True/None/False
80 }
82 for key, val in directive_defaults.items():
83 if key not in directive_types:
84 directive_types[key] = type(val)
86 directive_scopes = { # defaults to available everywhere
87 # 'module', 'function', 'class', 'with statement'
88 'autotestdict' : ('module',),
89 'test_assert_path_exists' : ('function',),
90 'test_fail_if_path_exists' : ('function',),
91 }
93 def parse_directive_value(name, value, relaxed_bool=False):
94 """
95 Parses value as an option value for the given name and returns
96 the interpreted value. None is returned if the option does not exist.
98 >>> print parse_directive_value('nonexisting', 'asdf asdfd')
99 None
100 >>> parse_directive_value('boundscheck', 'True')
101 True
102 >>> parse_directive_value('boundscheck', 'true')
103 Traceback (most recent call last):
104 ...
105 ValueError: boundscheck directive must be set to True or False
107 """
108 type = directive_types.get(name)
109 if not type: return None
110 if type is bool:
111 value = str(value)
112 if value == 'True': return True
113 if value == 'False': return False
114 if relaxed_bool:
115 value = value.lower()
116 if value in ("true", "yes"): return True
117 elif value in ("false", "no"): return False
118 raise ValueError("%s directive must be set to True or False" % name)
119 elif type is int:
120 try:
121 return int(value)
122 except ValueError:
123 raise ValueError("%s directive must be set to an integer" % name)
124 elif type is str:
125 return str(value)
126 else:
127 assert False
129 def parse_directive_list(s, relaxed_bool=False, ignore_unknown=False):
130 """
131 Parses a comma-seperated list of pragma options. Whitespace
132 is not considered.
134 >>> parse_directive_list(' ')
135 {}
136 >>> (parse_directive_list('boundscheck=True') ==
137 ... {'boundscheck': True})
138 True
139 >>> parse_directive_list(' asdf')
140 Traceback (most recent call last):
141 ...
142 ValueError: Expected "=" in option "asdf"
143 >>> parse_directive_list('boundscheck=hey')
144 Traceback (most recent call last):
145 ...
146 ValueError: boundscheck directive must be set to True or False
147 >>> parse_directive_list('unknown=True')
148 Traceback (most recent call last):
149 ...
150 ValueError: Unknown option: "unknown"
151 """
152 result = {}
153 for item in s.split(','):
154 item = item.strip()
155 if not item: continue
156 if not '=' in item: raise ValueError('Expected "=" in option "%s"' % item)
157 name, value = [ s.strip() for s in item.strip().split('=', 1) ]
158 parsed_value = parse_directive_value(name, value, relaxed_bool=relaxed_bool)
159 if parsed_value is None:
160 if not ignore_unknown:
161 raise ValueError('Unknown option: "%s"' % name)
162 else:
163 result[name] = parsed_value
164 return result