2 # Cython - Compilation-wide options and pragma declarations
5 # Perform lookups on builtin names only once, at module initialisation
6 # time. This will prevent the module from getting imported if a
7 # builtin name that it uses cannot be found during initialisation.
10 embed_pos_in_docstring
= False
11 gcc_branch_hints
= True
16 # Decref global variables in this module on exit for garbage collection.
17 # 0: None, 1+: interned objects, 2+: cdef globals, 3+: types objects
18 # Mostly for reducing noise for Valgrind, only executes at process exit
19 # (when all memory will be reclaimed anyways).
20 generate_cleanup_code
= False
24 # This will abort the compilation on the first error occured rather than trying
25 # to keep going and printing further error messages.
28 # Make all warnings into errors.
29 warning_errors
= False
31 # Make unknown names an error. Python raises a NameError when
32 # encountering unknown names at runtime, whereas this option makes
33 # them a compile time error. If you want full Python compatibility,
34 # you should disable this option and also 'cache_builtins'.
35 error_on_unknown_names
= True
37 # Make uninitialized local variable reference a compile time error.
38 # Python raises UnboundLocalError at runtime, whereas this option makes
39 # them a compile time error. Note that this option affects only variables
40 # of "python object" type.
41 error_on_uninitialized
= True
43 # This will convert statements of the form "for i in range(...)"
44 # to "for i from ..." when i is a cdef'd integer type, and the direction
45 # (i.e. sign of step) can be determined.
46 # WARNING: This may change the semantics if the range causes assignment to
47 # i to overflow. Specifically, if this option is set, an error will be
48 # raised before the loop is entered, wheras without this option the loop
49 # will execute until an overflowing value is encountered.
52 # Enable this to allow one to write your_module.foo = ... to overwrite the
53 # definition if the cpdef function foo, at the cost of an extra dictionary
54 # lookup on every call.
55 # If this is 0 it simply creates a wrapper.
56 lookup_module_cpdef
= False
58 # Whether or not to embed the Python interpreter, for use in making a
59 # standalone executable or calling from external libraries.
60 # This will provide a method which initalizes the interpreter and
61 # executes the body of this module.
64 # In previous iterations of Cython, globals() gave the first non-Cython module
65 # globals in the call stack. Sage relies on this behavior for variable injection.
66 old_style_globals
= False
68 # Allows cimporting from a pyx file without a pxd file.
69 cimport_from_pyx
= False
71 # max # of dims for buffers -- set lower than number of dimensions in numpy, as
72 # slices are passed by value and involve a lot of copying
75 # Number of function closure instances to keep in a freelist (0: no freelists)
76 closure_freelist_size
= 8
78 # Should tp_clear() set object fields to None instead of clearing them to NULL?
82 # Declare compiler directives
83 directive_defaults
= {
86 'initializedcheck' : True,
87 'embedsignature' : False,
90 'cdivision': False, # was True before 0.12
91 'cdivision_warnings': False,
92 'overflowcheck': False,
93 'overflowcheck.fold': True,
94 'always_allow_keywords': False,
95 'allow_none_for_extension_args': True,
97 'ccomplex' : False, # use C99/C++ for complex types and arith
102 'no_gc_clear': False,
105 'infer_types.verbose': False,
106 'autotestdict': True,
107 'autotestdict.cdef': False,
108 'autotestdict.all': False,
110 'fast_getattr': False, # Undocumented until we come up with a better way to handle this everywhere.
111 'py2_import': False, # For backward compatibility of Cython's source code in Py3 source mode
112 'c_string_type': 'bytes',
113 'c_string_encoding': '',
114 'type_version_tag': True, # enables Py_TPFLAGS_HAVE_VERSION_TAG on extension types
115 'unraisable_tracebacks': False,
117 # set __file__ and/or __path__ to known source/target path at import time (instead of not having them available)
118 'set_initial_path' : None, # SOURCEFILE or "/full/path/to/module"
121 'warn.undeclared': False,
122 'warn.unreachable': True,
123 'warn.maybe_uninitialized': False,
124 'warn.unused': False,
125 'warn.unused_arg': False,
126 'warn.unused_result': False,
127 'warn.multiple_declarators': True,
130 'optimize.inline_defnode_calls': True,
132 # remove unreachable code
133 'remove_unreachable': True,
135 # control flow debug directives
136 'control_flow.dot_output': "", # Graphviz output filename
137 'control_flow.dot_annotate_defs': False, # Annotate definitions
140 'test_assert_path_exists' : [],
141 'test_fail_if_path_exists' : [],
143 # experimental, subject to change
148 # Extra warning directives
150 'warn.maybe_uninitialized': True,
151 'warn.unreachable': True,
156 def validate(name
, value
):
157 if value
not in args
:
158 raise ValueError("%s directive must be one of %s, got '%s'" % (
165 def normalise_encoding_name(option_name
, encoding
):
167 >>> normalise_encoding_name('c_string_encoding', 'ascii')
169 >>> normalise_encoding_name('c_string_encoding', 'AsCIi')
171 >>> normalise_encoding_name('c_string_encoding', 'us-ascii')
173 >>> normalise_encoding_name('c_string_encoding', 'utF8')
175 >>> normalise_encoding_name('c_string_encoding', 'utF-8')
177 >>> normalise_encoding_name('c_string_encoding', 'deFAuLT')
179 >>> normalise_encoding_name('c_string_encoding', 'default')
181 >>> normalise_encoding_name('c_string_encoding', 'SeriousLyNoSuch--Encoding')
182 'SeriousLyNoSuch--Encoding'
186 if encoding
.lower() in ('default', 'ascii', 'utf8'):
187 return encoding
.lower()
190 decoder
= codecs
.getdecoder(encoding
)
192 return encoding
# may exists at runtime ...
193 for name
in ('ascii', 'utf8'):
194 if codecs
.getdecoder(name
) == decoder
:
199 # Override types possibilities above, if needed
201 'final' : bool, # final cdef classes and methods
202 'internal' : bool, # cdef class visibility in the module dict
203 'infer_types' : bool, # values can be True/None/False
205 'cfunc' : None, # decorators do not take directive value
209 'set_initial_path': str,
211 'c_string_type': one_of('bytes', 'bytearray', 'str', 'unicode'),
212 'c_string_encoding': normalise_encoding_name
,
215 for key
, val
in directive_defaults
.items():
216 if key
not in directive_types
:
217 directive_types
[key
] = type(val
)
219 directive_scopes
= { # defaults to available everywhere
220 # 'module', 'function', 'class', 'with statement'
221 'final' : ('cclass', 'function'),
222 'no_gc_clear' : ('cclass',),
223 'internal' : ('cclass',),
224 'autotestdict' : ('module',),
225 'autotestdict.all' : ('module',),
226 'autotestdict.cdef' : ('module',),
227 'set_initial_path' : ('module',),
228 'test_assert_path_exists' : ('function', 'class', 'cclass'),
229 'test_fail_if_path_exists' : ('function', 'class', 'cclass'),
230 'freelist': ('cclass',),
231 # Avoid scope-specific to/from_py_functions for c_string.
232 'c_string_type': ('module',),
233 'c_string_encoding': ('module',),
234 'type_version_tag': ('module', 'cclass'),
237 def parse_directive_value(name
, value
, relaxed_bool
=False):
239 Parses value as an option value for the given name and returns
240 the interpreted value. None is returned if the option does not exist.
242 >>> print parse_directive_value('nonexisting', 'asdf asdfd')
244 >>> parse_directive_value('boundscheck', 'True')
246 >>> parse_directive_value('boundscheck', 'true')
247 Traceback (most recent call last):
249 ValueError: boundscheck directive must be set to True or False, got 'true'
251 >>> parse_directive_value('c_string_encoding', 'us-ascii')
253 >>> parse_directive_value('c_string_type', 'str')
255 >>> parse_directive_value('c_string_type', 'bytes')
257 >>> parse_directive_value('c_string_type', 'bytearray')
259 >>> parse_directive_value('c_string_type', 'unicode')
261 >>> parse_directive_value('c_string_type', 'unnicode')
262 Traceback (most recent call last):
263 ValueError: c_string_type directive must be one of ('bytes', 'bytearray', 'str', 'unicode'), got 'unnicode'
265 type = directive_types
.get(name
)
266 if not type: return None
270 if value
== 'True': return True
271 if value
== 'False': return False
273 value
= value
.lower()
274 if value
in ("true", "yes"): return True
275 elif value
in ("false", "no"): return False
276 raise ValueError("%s directive must be set to True or False, got '%s'" % (
282 raise ValueError("%s directive must be set to an integer, got '%s'" % (
287 return type(name
, value
)
291 def parse_directive_list(s
, relaxed_bool
=False, ignore_unknown
=False,
292 current_settings
=None):
294 Parses a comma-separated list of pragma options. Whitespace
297 >>> parse_directive_list(' ')
299 >>> (parse_directive_list('boundscheck=True') ==
300 ... {'boundscheck': True})
302 >>> parse_directive_list(' asdf')
303 Traceback (most recent call last):
305 ValueError: Expected "=" in option "asdf"
306 >>> parse_directive_list('boundscheck=hey')
307 Traceback (most recent call last):
309 ValueError: boundscheck directive must be set to True or False, got 'hey'
310 >>> parse_directive_list('unknown=True')
311 Traceback (most recent call last):
313 ValueError: Unknown option: "unknown"
314 >>> warnings = parse_directive_list('warn.all=True')
315 >>> len(warnings) > 1
317 >>> sum(warnings.values()) == len(warnings) # all true.
320 if current_settings
is None:
323 result
= current_settings
324 for item
in s
.split(','):
326 if not item
: continue
327 if not '=' in item
: raise ValueError('Expected "=" in option "%s"' % item
)
328 name
, value
= [ s
.strip() for s
in item
.strip().split('=', 1) ]
329 if name
not in directive_defaults
:
331 if name
.endswith('.all'):
333 for directive
in directive_defaults
:
334 if directive
.startswith(prefix
):
336 parsed_value
= parse_directive_value(directive
, value
, relaxed_bool
=relaxed_bool
)
337 result
[directive
] = parsed_value
338 if not found
and not ignore_unknown
:
339 raise ValueError('Unknown option: "%s"' % name
)
341 parsed_value
= parse_directive_value(name
, value
, relaxed_bool
=relaxed_bool
)
342 result
[name
] = parsed_value