Options.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #
  2. # Cython - Compilation-wide options and pragma declarations
  3. #
  4. from __future__ import absolute_import
  5. class ShouldBeFromDirective(object):
  6. known_directives = []
  7. def __init__(self, options_name, directive_name=None, disallow=False):
  8. self.options_name = options_name
  9. self.directive_name = directive_name or options_name
  10. self.disallow = disallow
  11. self.known_directives.append(self)
  12. def __nonzero__(self):
  13. self._bad_access()
  14. def __int__(self):
  15. self._bad_access()
  16. def _bad_access(self):
  17. raise RuntimeError(repr(self))
  18. def __repr__(self):
  19. return (
  20. "Illegal access of '%s' from Options module rather than directive '%s'"
  21. % (self.options_name, self.directive_name))
  22. """
  23. The members of this module are documented using autodata in
  24. Cython/docs/src/reference/compilation.rst.
  25. See http://www.sphinx-doc.org/en/master/ext/autodoc.html#directive-autoattribute
  26. for how autodata works.
  27. Descriptions of those members should start with a #:
  28. Donc forget to keep the docs in sync by removing and adding
  29. the members in both this file and the .rst file.
  30. """
  31. #: Whether or not to include docstring in the Python extension. If False, the binary size
  32. #: will be smaller, but the ``__doc__`` attribute of any class or function will be an
  33. #: empty string.
  34. docstrings = True
  35. #: Embed the source code position in the docstrings of functions and classes.
  36. embed_pos_in_docstring = False
  37. #: Copy the original source code line by line into C code comments
  38. #: in the generated code file to help with understanding the output.
  39. #: This is also required for coverage analysis.
  40. emit_code_comments = True
  41. # undocumented
  42. pre_import = None
  43. #: Decref global variables in each module on exit for garbage collection.
  44. #: 0: None, 1+: interned objects, 2+: cdef globals, 3+: types objects
  45. #: Mostly for reducing noise in Valgrind as it typically executes at process exit
  46. #: (when all memory will be reclaimed anyways).
  47. #: Note that directly or indirectly executed cleanup code that makes use of global
  48. #: variables or types may no longer be safe when enabling the respective level since
  49. #: there is no guaranteed order in which the (reference counted) objects will
  50. #: be cleaned up. The order can change due to live references and reference cycles.
  51. generate_cleanup_code = False
  52. #: Should tp_clear() set object fields to None instead of clearing them to NULL?
  53. clear_to_none = True
  54. #: Generate an annotated HTML version of the input source files for debugging and optimisation purposes.
  55. #: This has the same effect as the ``annotate`` argument in :func:`cythonize`.
  56. annotate = False
  57. # When annotating source files in HTML, include coverage information from
  58. # this file.
  59. annotate_coverage_xml = None
  60. #: This will abort the compilation on the first error occurred rather than trying
  61. #: to keep going and printing further error messages.
  62. fast_fail = False
  63. #: Turn all warnings into errors.
  64. warning_errors = False
  65. #: Make unknown names an error. Python raises a NameError when
  66. #: encountering unknown names at runtime, whereas this option makes
  67. #: them a compile time error. If you want full Python compatibility,
  68. #: you should disable this option and also 'cache_builtins'.
  69. error_on_unknown_names = True
  70. #: Make uninitialized local variable reference a compile time error.
  71. #: Python raises UnboundLocalError at runtime, whereas this option makes
  72. #: them a compile time error. Note that this option affects only variables
  73. #: of "python object" type.
  74. error_on_uninitialized = True
  75. #: This will convert statements of the form ``for i in range(...)``
  76. #: to ``for i from ...`` when ``i`` is a C integer type, and the direction
  77. #: (i.e. sign of step) can be determined.
  78. #: WARNING: This may change the semantics if the range causes assignment to
  79. #: i to overflow. Specifically, if this option is set, an error will be
  80. #: raised before the loop is entered, whereas without this option the loop
  81. #: will execute until an overflowing value is encountered.
  82. convert_range = True
  83. #: Perform lookups on builtin names only once, at module initialisation
  84. #: time. This will prevent the module from getting imported if a
  85. #: builtin name that it uses cannot be found during initialisation.
  86. #: Default is True.
  87. #: Note that some legacy builtins are automatically remapped
  88. #: from their Python 2 names to their Python 3 names by Cython
  89. #: when building in Python 3.x,
  90. #: so that they do not get in the way even if this option is enabled.
  91. cache_builtins = True
  92. #: Generate branch prediction hints to speed up error handling etc.
  93. gcc_branch_hints = True
  94. #: Enable this to allow one to write ``your_module.foo = ...`` to overwrite the
  95. #: definition if the cpdef function foo, at the cost of an extra dictionary
  96. #: lookup on every call.
  97. #: If this is false it generates only the Python wrapper and no override check.
  98. lookup_module_cpdef = False
  99. #: Whether or not to embed the Python interpreter, for use in making a
  100. #: standalone executable or calling from external libraries.
  101. #: This will provide a C function which initialises the interpreter and
  102. #: executes the body of this module.
  103. #: See `this demo <https://github.com/cython/cython/tree/master/Demos/embed>`_
  104. #: for a concrete example.
  105. #: If true, the initialisation function is the C main() function, but
  106. #: this option can also be set to a non-empty string to provide a function name explicitly.
  107. #: Default is False.
  108. embed = None
  109. # In previous iterations of Cython, globals() gave the first non-Cython module
  110. # globals in the call stack. Sage relies on this behavior for variable injection.
  111. old_style_globals = ShouldBeFromDirective('old_style_globals')
  112. #: Allows cimporting from a pyx file without a pxd file.
  113. cimport_from_pyx = False
  114. #: Maximum number of dimensions for buffers -- set lower than number of
  115. #: dimensions in numpy, as
  116. #: slices are passed by value and involve a lot of copying.
  117. buffer_max_dims = 8
  118. #: Number of function closure instances to keep in a freelist (0: no freelists)
  119. closure_freelist_size = 8
  120. def get_directive_defaults():
  121. # To add an item to this list, all accesses should be changed to use the new
  122. # directive, and the global option itself should be set to an instance of
  123. # ShouldBeFromDirective.
  124. for old_option in ShouldBeFromDirective.known_directives:
  125. value = globals().get(old_option.options_name)
  126. assert old_option.directive_name in _directive_defaults
  127. if not isinstance(value, ShouldBeFromDirective):
  128. if old_option.disallow:
  129. raise RuntimeError(
  130. "Option '%s' must be set from directive '%s'" % (
  131. old_option.option_name, old_option.directive_name))
  132. else:
  133. # Warn?
  134. _directive_defaults[old_option.directive_name] = value
  135. return _directive_defaults
  136. # Declare compiler directives
  137. _directive_defaults = {
  138. 'boundscheck' : True,
  139. 'nonecheck' : False,
  140. 'initializedcheck' : True,
  141. 'embedsignature' : False,
  142. 'auto_cpdef': False,
  143. 'auto_pickle': None,
  144. 'cdivision': False, # was True before 0.12
  145. 'cdivision_warnings': False,
  146. 'c_api_binop_methods': True,
  147. 'overflowcheck': False,
  148. 'overflowcheck.fold': True,
  149. 'always_allow_keywords': False,
  150. 'allow_none_for_extension_args': True,
  151. 'wraparound' : True,
  152. 'ccomplex' : False, # use C99/C++ for complex types and arith
  153. 'callspec' : "",
  154. 'nogil' : False,
  155. 'profile': False,
  156. 'linetrace': False,
  157. 'emit_code_comments': True, # copy original source code into C code comments
  158. 'annotation_typing': True, # read type declarations from Python function annotations
  159. 'infer_types': None,
  160. 'infer_types.verbose': False,
  161. 'autotestdict': True,
  162. 'autotestdict.cdef': False,
  163. 'autotestdict.all': False,
  164. 'language_level': None,
  165. 'fast_getattr': False, # Undocumented until we come up with a better way to handle this everywhere.
  166. 'py2_import': False, # For backward compatibility of Cython's source code in Py3 source mode
  167. 'preliminary_late_includes_cy28': False, # Temporary directive in 0.28, to be removed in a later version (see GH#2079).
  168. 'iterable_coroutine': False, # Make async coroutines backwards compatible with the old asyncio yield-from syntax.
  169. 'c_string_type': 'bytes',
  170. 'c_string_encoding': '',
  171. 'type_version_tag': True, # enables Py_TPFLAGS_HAVE_VERSION_TAG on extension types
  172. 'unraisable_tracebacks': True,
  173. 'old_style_globals': False,
  174. 'np_pythran': False,
  175. 'fast_gil': False,
  176. # set __file__ and/or __path__ to known source/target path at import time (instead of not having them available)
  177. 'set_initial_path' : None, # SOURCEFILE or "/full/path/to/module"
  178. 'warn': None,
  179. 'warn.undeclared': False,
  180. 'warn.unreachable': True,
  181. 'warn.maybe_uninitialized': False,
  182. 'warn.unused': False,
  183. 'warn.unused_arg': False,
  184. 'warn.unused_result': False,
  185. 'warn.multiple_declarators': True,
  186. # optimizations
  187. 'optimize.inline_defnode_calls': True,
  188. 'optimize.unpack_method_calls': True, # increases code size when True
  189. 'optimize.unpack_method_calls_in_pyinit': False, # uselessly increases code size when True
  190. 'optimize.use_switch': True,
  191. # remove unreachable code
  192. 'remove_unreachable': True,
  193. # control flow debug directives
  194. 'control_flow.dot_output': "", # Graphviz output filename
  195. 'control_flow.dot_annotate_defs': False, # Annotate definitions
  196. # test support
  197. 'test_assert_path_exists' : [],
  198. 'test_fail_if_path_exists' : [],
  199. # experimental, subject to change
  200. 'binding': None,
  201. 'formal_grammar': False,
  202. }
  203. # Extra warning directives
  204. extra_warnings = {
  205. 'warn.maybe_uninitialized': True,
  206. 'warn.unreachable': True,
  207. 'warn.unused': True,
  208. }
  209. def one_of(*args):
  210. def validate(name, value):
  211. if value not in args:
  212. raise ValueError("%s directive must be one of %s, got '%s'" % (
  213. name, args, value))
  214. else:
  215. return value
  216. return validate
  217. def normalise_encoding_name(option_name, encoding):
  218. """
  219. >>> normalise_encoding_name('c_string_encoding', 'ascii')
  220. 'ascii'
  221. >>> normalise_encoding_name('c_string_encoding', 'AsCIi')
  222. 'ascii'
  223. >>> normalise_encoding_name('c_string_encoding', 'us-ascii')
  224. 'ascii'
  225. >>> normalise_encoding_name('c_string_encoding', 'utF8')
  226. 'utf8'
  227. >>> normalise_encoding_name('c_string_encoding', 'utF-8')
  228. 'utf8'
  229. >>> normalise_encoding_name('c_string_encoding', 'deFAuLT')
  230. 'default'
  231. >>> normalise_encoding_name('c_string_encoding', 'default')
  232. 'default'
  233. >>> normalise_encoding_name('c_string_encoding', 'SeriousLyNoSuch--Encoding')
  234. 'SeriousLyNoSuch--Encoding'
  235. """
  236. if not encoding:
  237. return ''
  238. if encoding.lower() in ('default', 'ascii', 'utf8'):
  239. return encoding.lower()
  240. import codecs
  241. try:
  242. decoder = codecs.getdecoder(encoding)
  243. except LookupError:
  244. return encoding # may exists at runtime ...
  245. for name in ('ascii', 'utf8'):
  246. if codecs.getdecoder(name) == decoder:
  247. return name
  248. return encoding
  249. # Override types possibilities above, if needed
  250. directive_types = {
  251. 'language_level': str, # values can be None/2/3/'3str', where None == 2+warning
  252. 'auto_pickle': bool,
  253. 'locals': dict,
  254. 'final' : bool, # final cdef classes and methods
  255. 'nogil' : bool,
  256. 'internal' : bool, # cdef class visibility in the module dict
  257. 'infer_types' : bool, # values can be True/None/False
  258. 'binding' : bool,
  259. 'cfunc' : None, # decorators do not take directive value
  260. 'ccall' : None,
  261. 'inline' : None,
  262. 'staticmethod' : None,
  263. 'cclass' : None,
  264. 'no_gc_clear' : bool,
  265. 'no_gc' : bool,
  266. 'returns' : type,
  267. 'exceptval': type, # actually (type, check=True/False), but has its own parser
  268. 'set_initial_path': str,
  269. 'freelist': int,
  270. 'c_string_type': one_of('bytes', 'bytearray', 'str', 'unicode'),
  271. 'c_string_encoding': normalise_encoding_name,
  272. }
  273. for key, val in _directive_defaults.items():
  274. if key not in directive_types:
  275. directive_types[key] = type(val)
  276. directive_scopes = { # defaults to available everywhere
  277. # 'module', 'function', 'class', 'with statement'
  278. 'auto_pickle': ('module', 'cclass'),
  279. 'final' : ('cclass', 'function'),
  280. 'nogil' : ('function', 'with statement'),
  281. 'inline' : ('function',),
  282. 'cfunc' : ('function', 'with statement'),
  283. 'ccall' : ('function', 'with statement'),
  284. 'returns' : ('function',),
  285. 'exceptval' : ('function',),
  286. 'locals' : ('function',),
  287. 'staticmethod' : ('function',), # FIXME: analysis currently lacks more specific function scope
  288. 'no_gc_clear' : ('cclass',),
  289. 'no_gc' : ('cclass',),
  290. 'internal' : ('cclass',),
  291. 'cclass' : ('class', 'cclass', 'with statement'),
  292. 'autotestdict' : ('module',),
  293. 'autotestdict.all' : ('module',),
  294. 'autotestdict.cdef' : ('module',),
  295. 'set_initial_path' : ('module',),
  296. 'test_assert_path_exists' : ('function', 'class', 'cclass'),
  297. 'test_fail_if_path_exists' : ('function', 'class', 'cclass'),
  298. 'freelist': ('cclass',),
  299. 'emit_code_comments': ('module',),
  300. 'annotation_typing': ('module',), # FIXME: analysis currently lacks more specific function scope
  301. # Avoid scope-specific to/from_py_functions for c_string.
  302. 'c_string_type': ('module',),
  303. 'c_string_encoding': ('module',),
  304. 'type_version_tag': ('module', 'cclass'),
  305. 'language_level': ('module',),
  306. # globals() could conceivably be controlled at a finer granularity,
  307. # but that would complicate the implementation
  308. 'old_style_globals': ('module',),
  309. 'np_pythran': ('module',),
  310. 'fast_gil': ('module',),
  311. 'iterable_coroutine': ('module', 'function'),
  312. }
  313. def parse_directive_value(name, value, relaxed_bool=False):
  314. """
  315. Parses value as an option value for the given name and returns
  316. the interpreted value. None is returned if the option does not exist.
  317. >>> print(parse_directive_value('nonexisting', 'asdf asdfd'))
  318. None
  319. >>> parse_directive_value('boundscheck', 'True')
  320. True
  321. >>> parse_directive_value('boundscheck', 'true')
  322. Traceback (most recent call last):
  323. ...
  324. ValueError: boundscheck directive must be set to True or False, got 'true'
  325. >>> parse_directive_value('c_string_encoding', 'us-ascii')
  326. 'ascii'
  327. >>> parse_directive_value('c_string_type', 'str')
  328. 'str'
  329. >>> parse_directive_value('c_string_type', 'bytes')
  330. 'bytes'
  331. >>> parse_directive_value('c_string_type', 'bytearray')
  332. 'bytearray'
  333. >>> parse_directive_value('c_string_type', 'unicode')
  334. 'unicode'
  335. >>> parse_directive_value('c_string_type', 'unnicode')
  336. Traceback (most recent call last):
  337. ValueError: c_string_type directive must be one of ('bytes', 'bytearray', 'str', 'unicode'), got 'unnicode'
  338. """
  339. type = directive_types.get(name)
  340. if not type:
  341. return None
  342. orig_value = value
  343. if type is bool:
  344. value = str(value)
  345. if value == 'True':
  346. return True
  347. if value == 'False':
  348. return False
  349. if relaxed_bool:
  350. value = value.lower()
  351. if value in ("true", "yes"):
  352. return True
  353. elif value in ("false", "no"):
  354. return False
  355. raise ValueError("%s directive must be set to True or False, got '%s'" % (
  356. name, orig_value))
  357. elif type is int:
  358. try:
  359. return int(value)
  360. except ValueError:
  361. raise ValueError("%s directive must be set to an integer, got '%s'" % (
  362. name, orig_value))
  363. elif type is str:
  364. return str(value)
  365. elif callable(type):
  366. return type(name, value)
  367. else:
  368. assert False
  369. def parse_directive_list(s, relaxed_bool=False, ignore_unknown=False,
  370. current_settings=None):
  371. """
  372. Parses a comma-separated list of pragma options. Whitespace
  373. is not considered.
  374. >>> parse_directive_list(' ')
  375. {}
  376. >>> (parse_directive_list('boundscheck=True') ==
  377. ... {'boundscheck': True})
  378. True
  379. >>> parse_directive_list(' asdf')
  380. Traceback (most recent call last):
  381. ...
  382. ValueError: Expected "=" in option "asdf"
  383. >>> parse_directive_list('boundscheck=hey')
  384. Traceback (most recent call last):
  385. ...
  386. ValueError: boundscheck directive must be set to True or False, got 'hey'
  387. >>> parse_directive_list('unknown=True')
  388. Traceback (most recent call last):
  389. ...
  390. ValueError: Unknown option: "unknown"
  391. >>> warnings = parse_directive_list('warn.all=True')
  392. >>> len(warnings) > 1
  393. True
  394. >>> sum(warnings.values()) == len(warnings) # all true.
  395. True
  396. """
  397. if current_settings is None:
  398. result = {}
  399. else:
  400. result = current_settings
  401. for item in s.split(','):
  402. item = item.strip()
  403. if not item:
  404. continue
  405. if '=' not in item:
  406. raise ValueError('Expected "=" in option "%s"' % item)
  407. name, value = [s.strip() for s in item.strip().split('=', 1)]
  408. if name not in _directive_defaults:
  409. found = False
  410. if name.endswith('.all'):
  411. prefix = name[:-3]
  412. for directive in _directive_defaults:
  413. if directive.startswith(prefix):
  414. found = True
  415. parsed_value = parse_directive_value(directive, value, relaxed_bool=relaxed_bool)
  416. result[directive] = parsed_value
  417. if not found and not ignore_unknown:
  418. raise ValueError('Unknown option: "%s"' % name)
  419. else:
  420. parsed_value = parse_directive_value(name, value, relaxed_bool=relaxed_bool)
  421. result[name] = parsed_value
  422. return result
  423. def parse_variable_value(value):
  424. """
  425. Parses value as an option value for the given name and returns
  426. the interpreted value.
  427. >>> parse_variable_value('True')
  428. True
  429. >>> parse_variable_value('true')
  430. 'true'
  431. >>> parse_variable_value('us-ascii')
  432. 'us-ascii'
  433. >>> parse_variable_value('str')
  434. 'str'
  435. >>> parse_variable_value('123')
  436. 123
  437. >>> parse_variable_value('1.23')
  438. 1.23
  439. """
  440. if value == "True":
  441. return True
  442. elif value == "False":
  443. return False
  444. elif value == "None":
  445. return None
  446. elif value.isdigit():
  447. return int(value)
  448. else:
  449. try:
  450. value = float(value)
  451. except Exception:
  452. # Not a float
  453. pass
  454. return value
  455. def parse_compile_time_env(s, current_settings=None):
  456. """
  457. Parses a comma-separated list of pragma options. Whitespace
  458. is not considered.
  459. >>> parse_compile_time_env(' ')
  460. {}
  461. >>> (parse_compile_time_env('HAVE_OPENMP=True') ==
  462. ... {'HAVE_OPENMP': True})
  463. True
  464. >>> parse_compile_time_env(' asdf')
  465. Traceback (most recent call last):
  466. ...
  467. ValueError: Expected "=" in option "asdf"
  468. >>> parse_compile_time_env('NUM_THREADS=4') == {'NUM_THREADS': 4}
  469. True
  470. >>> parse_compile_time_env('unknown=anything') == {'unknown': 'anything'}
  471. True
  472. """
  473. if current_settings is None:
  474. result = {}
  475. else:
  476. result = current_settings
  477. for item in s.split(','):
  478. item = item.strip()
  479. if not item:
  480. continue
  481. if '=' not in item:
  482. raise ValueError('Expected "=" in option "%s"' % item)
  483. name, value = [s.strip() for s in item.split('=', 1)]
  484. result[name] = parse_variable_value(value)
  485. return result