6 from distutils.extension import Extension
8 from Cython import Utils
9 from Cython.Compiler.Main import Context, CompilationOptions, default_options
11 # Unfortunately, Python 2.3 doesn't support decorators.
13 cache_name = '__%s_cache' % f.__name__
14 def wrapper(self, *args):
15 cache = getattr(self, cache_name, None)
18 setattr(self, cache_name, cache)
21 res = cache[args] = f(self, *args)
27 if s[0] == '[' and s[-1] == ']':
32 s, literals = strip_string_literals(s)
34 literal = literal.strip()
36 return literals[literal[1:-1]]
40 return [unquote(item) for item in s.split(delimiter)]
42 transitive_str = object()
43 transitive_list = object()
45 distutils_settings = {
48 'define_macros': list,
50 'libraries': transitive_list,
51 'library_dirs': transitive_list,
52 'runtime_library_dirs': transitive_list,
53 'include_dirs': transitive_list,
54 'extra_objects': list,
55 'extra_compile_args': transitive_list,
56 'extra_link_args': transitive_list,
57 'export_symbols': list,
58 'depends': transitive_list,
59 'language': transitive_str,
62 def line_iter(source):
65 end = source.find('\n', start)
69 yield source[start:end]
72 class DistutilsInfo(object):
74 def __init__(self, source=None, exn=None):
76 if source is not None:
77 for line in line_iter(source):
79 if line != '' and line[0] != '#':
81 line = line[1:].strip()
82 if line[:10] == 'distutils:':
85 key = str(line[:ix].strip())
86 value = line[ix+1:].strip()
87 type = distutils_settings[key]
88 if type in (list, transitive_list):
89 value = parse_list(value)
90 if key == 'define_macros':
91 value = [tuple(macro.split('=')) for macro in value]
92 self.values[key] = value
94 for key in distutils_settings:
95 if key in ('name', 'sources'):
97 value = getattr(exn, key, None)
99 self.values[key] = value
101 def merge(self, other):
104 for key, value in other.values.items():
105 type = distutils_settings[key]
106 if type is transitive_str and key not in self.values:
107 self.values[key] = value
108 elif type is transitive_list:
109 if key in self.values:
110 all = self.values[key]
115 self.values[key] = value
118 def subs(self, aliases):
121 resolved = DistutilsInfo()
122 for key, value in self.values.items():
123 type = distutils_settings[key]
124 if type in [list, transitive_list]:
129 if isinstance(v, list):
132 new_value_list.append(v)
133 value = new_value_list
136 value = aliases[value]
137 resolved.values[key] = value
141 def strip_string_literals(code, prefix='__Pyx_L'):
143 Normalizes every string literal to be of the form '__Pyx_Lxxx',
144 returning the normalized code and a mapping of labels to
154 hash_mark = code.find('#', q)
155 single_q = code.find("'", q)
156 double_q = code.find('"', q)
157 q = min(single_q, double_q)
158 if q == -1: q = max(single_q, double_q)
161 if q == -1 and hash_mark == -1:
162 new_code.append(code[start:])
165 # Try to close the quote.
167 if code[q-1] == '\\' and not raw:
169 while q >= k and code[q-k] == '\\':
174 if code[q:q+len(in_quote)] == in_quote:
176 label = "%s%s_" % (prefix, counter)
177 literals[label] = code[start+len(in_quote):q]
178 new_code.append("%s%s%s" % (in_quote, label, in_quote))
186 elif -1 != hash_mark and (hash_mark < q or q == -1):
187 end = code.find('\n', hash_mark)
190 new_code.append(code[start:hash_mark+1])
192 label = "%s%s_" % (prefix, counter)
193 literals[label] = code[hash_mark+1:end]
194 new_code.append(label)
203 if len(code) >= q+3 and (code[q+1] == code[q] == code[q+2]):
208 while marker > 0 and code[marker-1] in 'rRbBuU':
209 if code[marker-1] in 'rR':
212 new_code.append(code[start:end])
216 return "".join(new_code), literals
219 def parse_dependencies(source_filename):
220 # Actual parsing is way to slow, so we use regular expressions.
221 # The only catch is that we must strip comments and string
222 # literals ahead of time.
223 fh = Utils.open_source_file(source_filename, "rU")
228 distutils_info = DistutilsInfo(source)
229 source, literals = strip_string_literals(source)
230 source = source.replace('\\\n', ' ')
232 source = source.replace('\t', ' ')
234 dependancy = re.compile(r"(cimport +([0-9a-zA-Z_.]+)\b)|(from +([0-9a-zA-Z_.]+) +cimport)|(include +'([^']+)')|(cdef +extern +from +'([^']+)')")
238 for m in dependancy.finditer(source):
241 cimports.append(groups[1])
243 cimports.append(groups[3])
245 includes.append(literals[groups[5]])
247 externs.append(literals[groups[7]])
248 return cimports, includes, externs, distutils_info
251 class DependencyTree(object):
253 def __init__(self, context):
254 self.context = context
255 self._transitive_cache = {}
258 def parse_dependencies(self, source_filename):
259 return parse_dependencies(source_filename)
260 parse_dependencies = cached_method(parse_dependencies)
263 def cimports_and_externs(self, filename):
264 cimports, includes, externs = self.parse_dependencies(filename)[:3]
265 cimports = set(cimports)
266 externs = set(externs)
267 for include in includes:
268 include_path = os.path.join(os.path.dirname(filename), include)
269 if not os.path.exists(include_path):
270 include_path = self.context.find_include_file(include, None)
272 a, b = self.cimports_and_externs(include_path)
276 print("Unable to locate '%s' referenced from '%s'" % (filename, include))
277 return tuple(cimports), tuple(externs)
278 cimports_and_externs = cached_method(cimports_and_externs)
280 def cimports(self, filename):
281 return self.cimports_and_externs(filename)[0]
284 def package(self, filename):
285 dir = os.path.dirname(filename)
286 if os.path.exists(os.path.join(dir, '__init__.py')):
287 return self.package(dir) + (os.path.basename(dir),)
290 package = cached_method(package)
293 def fully_qualifeid_name(self, filename):
294 module = os.path.splitext(os.path.basename(filename))[0]
295 return '.'.join(self.package(filename) + (module,))
296 fully_qualifeid_name = cached_method(fully_qualifeid_name)
298 def find_pxd(self, module, filename=None):
300 raise NotImplementedError("New relative imports.")
301 if filename is not None:
302 relative = '.'.join(self.package(filename) + tuple(module.split('.')))
303 pxd = self.context.find_pxd_file(relative, None)
306 return self.context.find_pxd_file(module, None)
307 find_pxd = cached_method(find_pxd)
310 def cimported_files(self, filename):
311 if filename[-4:] == '.pyx' and os.path.exists(filename[:-4] + '.pxd'):
312 self_pxd = [filename[:-4] + '.pxd']
315 a = self.cimports(filename)
316 b = filter(None, [self.find_pxd(m, filename) for m in self.cimports(filename)])
317 if len(a) - int('cython' in a) != len(b):
318 print("missing cimport", filename)
319 print("\n\t".join(a))
320 print("\n\t".join(b))
321 return tuple(self_pxd + filter(None, [self.find_pxd(m, filename) for m in self.cimports(filename)]))
322 cimported_files = cached_method(cimported_files)
324 def immediate_dependencies(self, filename):
325 all = list(self.cimported_files(filename))
326 for extern in sum(self.cimports_and_externs(filename), ()):
327 all.append(os.path.normpath(os.path.join(os.path.dirname(filename), extern)))
331 def timestamp(self, filename):
332 return os.path.getmtime(filename)
333 timestamp = cached_method(timestamp)
335 def extract_timestamp(self, filename):
336 # TODO: .h files from extern blocks
337 return self.timestamp(filename), filename
339 def newest_dependency(self, filename):
340 return self.transitive_merge(filename, self.extract_timestamp, max)
342 def distutils_info0(self, filename):
343 return self.parse_dependencies(filename)[3]
345 def distutils_info(self, filename, aliases=None, base=None):
346 return (self.transitive_merge(filename, self.distutils_info0, DistutilsInfo.merge)
350 def transitive_merge(self, node, extract, merge):
352 seen = self._transitive_cache[extract, merge]
354 seen = self._transitive_cache[extract, merge] = {}
355 return self.transitive_merge_helper(
356 node, extract, merge, seen, {}, self.cimported_files)[0]
358 def transitive_merge_helper(self, node, extract, merge, seen, stack, outgoing):
360 return seen[node], None
365 stack[node] = len(stack)
367 for next in outgoing(node):
368 sub_deps, sub_loop = self.transitive_merge_helper(next, extract, merge, seen, stack, outgoing)
369 if sub_loop is not None:
370 if loop is not None and stack[loop] < stack[sub_loop]:
374 deps = merge(deps, sub_deps)
384 def create_dependency_tree(ctx=None):
386 if _dep_tree is None:
388 ctx = Context(["."], CompilationOptions(default_options))
389 _dep_tree = DependencyTree(ctx)
392 # This may be useful for advanced users?
393 def create_extension_list(patterns, exclude=[], ctx=None, aliases=None):
395 deps = create_dependency_tree(ctx)
397 if not isinstance(exclude, list):
399 for pattern in exclude:
400 to_exclude.update(glob(pattern))
401 if not isinstance(patterns, list):
402 patterns = [patterns]
404 for pattern in patterns:
405 if isinstance(pattern, str):
406 filepattern = pattern
411 elif isinstance(pattern, Extension):
412 filepattern = pattern.sources[0]
413 if os.path.splitext(filepattern)[1] not in ('.py', '.pyx'):
414 # ignore non-cython modules
415 module_list.append(pattern)
419 base = DistutilsInfo(exn=template)
420 exn_type = template.__class__
422 raise TypeError(pattern)
423 for file in glob(filepattern):
424 if file in to_exclude:
426 pkg = deps.package(file)
428 module_name = deps.fully_qualifeid_name(file)
431 if module_name not in seen:
432 kwds = deps.distutils_info(file, aliases, base).values
434 for key, value in base.values.items():
438 if template is not None:
439 sources += template.sources[1:]
440 module_list.append(exn_type(
448 # This is the user-exposed entry point.
449 def cythonize(module_list, exclude=[], nthreads=0, aliases=None, quiet=False, **options):
450 if 'include_path' not in options:
451 options['include_path'] = ['.']
452 c_options = CompilationOptions(**options)
453 cpp_options = CompilationOptions(**options); cpp_options.cplus = True
454 ctx = c_options.create_context()
455 module_list = create_extension_list(
460 deps = create_dependency_tree(ctx)
462 for m in module_list:
464 for source in m.sources:
465 base, ext = os.path.splitext(source)
466 if ext in ('.pyx', '.py'):
467 if m.language == 'c++':
468 c_file = base + '.cpp'
469 options = cpp_options
473 if os.path.exists(c_file):
474 c_timestamp = os.path.getmtime(c_file)
477 # Priority goes first to modified files, second to direct
478 # dependents, and finally to indirect dependents.
479 if c_timestamp < deps.timestamp(source):
480 dep_timestamp, dep = deps.timestamp(source), source
483 dep_timestamp, dep = deps.newest_dependency(source)
484 priority = 2 - (dep in deps.immediate_dependencies(source))
485 if c_timestamp < dep_timestamp:
488 print("Compiling %s because it changed." % source)
490 print("Compiling %s because it depends on %s." % (source, dep))
491 to_compile.append((priority, source, c_file, options))
492 new_sources.append(c_file)
494 new_sources.append(source)
495 m.sources = new_sources
498 # Requires multiprocessing (or Python >= 2.6)
500 import multiprocessing
501 pool = multiprocessing.Pool(nthreads)
502 pool.map(cythonize_one_helper, to_compile)
504 print("multiprocessing required for parallel cythonization")
507 for priority, pyx_file, c_file, options in to_compile:
508 cythonize_one(pyx_file, c_file, options)
511 # TODO: Share context? Issue: pyx processing leaks into pxd module
512 def cythonize_one(pyx_file, c_file, options=None):
513 from Cython.Compiler.Main import compile, default_options
514 from Cython.Compiler.Errors import CompileError, PyrexError
517 options = CompilationOptions(default_options)
518 options.output_file = c_file
522 result = compile([pyx_file], options)
523 if result.num_errors > 0:
525 except (EnvironmentError, PyrexError), e:
526 sys.stderr.write(str(e) + '\n')
529 raise CompileError(None, pyx_file)
531 def cythonize_one_helper(m):
532 return cythonize_one(*m[1:])