Fix wrong ctypedef testcase via autotestdict
[cython.git] / Cython / Compiler / Options.py
1 #
2 #  Cython - Compilation-wide options and pragma declarations
3 #
4
5 cache_builtins = 1  #  Perform lookups on builtin names only once
6
7 embed_pos_in_docstring = 0
8 gcc_branch_hints = 1
9
10 pre_import = None
11 docstrings = True
12
13 # This is a SAGE-specific option that will 
14 # cause Cython to incref local variables before
15 # performing a binary operation on them, for 
16 # safe detection of inplace operators. 
17 incref_local_binop = 0
18
19 # Decref global variables in this module on exit for garbage collection. 
20 # 0: None, 1+: interned objects, 2+: cdef globals, 3+: types objects
21 # Mostly for reducing noise for Valgrind, only executes at process exit
22 # (when all memory will be reclaimed anyways). 
23 generate_cleanup_code = 0
24
25 annotate = 0
26
27 # This will convert statements of the form "for i in range(...)" 
28 # to "for i from ..." when i is a cdef'd integer type, and the direction
29 # (i.e. sign of step) can be determined. 
30 # WARNING: This may change the symantics if the range causes assignment to 
31 # i to overflow. Specifically, if this option is set, an error will be
32 # raised before the loop is entered, wheras without this option the loop
33 # will execute util a overflowing value is encountered. 
34 convert_range = 1
35
36 # Enable this to allow one to write your_module.foo = ... to overwrite the 
37 # definition if the cpdef function foo, at the cost of an extra dictionary 
38 # lookup on every call. 
39 # If this is 0 it simply creates a wrapper. 
40 lookup_module_cpdef = 0
41
42 # This will set local variables to None rather than NULL which may cause 
43 # surpress what would be an UnboundLocalError in pure Python but eliminates 
44 # checking for NULL on every use, and can decref rather than xdecref at the end. 
45 # WARNING: This is a work in progress, may currently segfault.
46 init_local_none = 1
47
48 # Append the c file and line number to the traceback for exceptions. 
49 c_line_in_traceback = 1
50
51 # Whether or not to embed the Python interpreter, for use in making a 
52 # standalone executable. This will provide a main() method which simply 
53 # executes the body of this module. 
54 embed = False
55
56
57 # Declare compiler directives
58 option_defaults = {
59     'boundscheck' : True,
60     'nonecheck' : False,
61     'embedsignature' : False,
62     'locals' : {},
63     'auto_cpdef': False,
64     'cdivision': False, # was True before 0.12
65     'cdivision_warnings': False,
66     'always_allow_keywords': False,
67     'wraparound' : True,
68     'c99_complex' : False, # Don't use macro wrappers for complex arith, not sure what to name this...
69     'callspec' : "",
70     'profile': False,
71     'autotestdict': True
72 }
73
74 # Override types possibilities above, if needed
75 option_types = {}
76
77 for key, val in option_defaults.items():
78     if key not in option_types:
79         option_types[key] = type(val)
80
81 option_scopes = { # defaults to available everywhere
82     # 'module', 'function', 'class', 'with statement'
83     'autotestdict' : ('module',)
84 }
85
86 def parse_option_value(name, value):
87     """
88     Parses value as an option value for the given name and returns
89     the interpreted value. None is returned if the option does not exist.    
90
91     >>> print parse_option_value('nonexisting', 'asdf asdfd')
92     None
93     >>> parse_option_value('boundscheck', 'True')
94     True
95     >>> parse_option_value('boundscheck', 'true')
96     Traceback (most recent call last):
97        ...
98     ValueError: boundscheck directive must be set to True or False
99     
100     """
101     type = option_types.get(name)
102     if not type: return None
103     if type is bool:
104         if value == "True": return True
105         elif value == "False": return False
106         else: raise ValueError("%s directive must be set to True or False" % name)
107     elif type is int:
108         try:
109             return int(value)
110         except ValueError:
111             raise ValueError("%s directive must be set to an integer" % name)
112     else:
113         assert False
114
115 def parse_option_list(s):
116     """
117     Parses a comma-seperated list of pragma options. Whitespace
118     is not considered.
119
120     >>> parse_option_list('      ')
121     {}
122     >>> (parse_option_list('boundscheck=True') ==
123     ... {'boundscheck': True})
124     True
125     >>> parse_option_list('  asdf')
126     Traceback (most recent call last):
127        ...
128     ValueError: Expected "=" in option "asdf"
129     >>> parse_option_list('boundscheck=hey')
130     Traceback (most recent call last):
131        ...
132     ValueError: Must pass a boolean value for option "boundscheck"
133     >>> parse_option_list('unknown=True')
134     Traceback (most recent call last):
135        ...
136     ValueError: Unknown option: "unknown"
137     """
138     result = {}
139     for item in s.split(','):
140         item = item.strip()
141         if not item: continue
142         if not '=' in item: raise ValueError('Expected "=" in option "%s"' % item)
143         name, value = item.strip().split('=')
144         try:
145             type = option_types[name]
146         except KeyError:
147             raise ValueError('Unknown option: "%s"' % name)
148         if type is bool:
149             value = value.lower()
150             if value in ('true', 'yes'):
151                 value = True
152             elif value in ('false', 'no'):
153                 value = False
154             else: raise ValueError('Must pass a boolean value for option "%s"' % name)
155             result[name] = value
156         else:
157             assert False
158     return result