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 source = Utils.open_source_file(source_filename, "rU").read()
224 distutils_info = DistutilsInfo(source)
225 source, literals = strip_string_literals(source)
226 source = source.replace('\\\n', ' ')
228 source = source.replace('\t', ' ')
230 dependancy = re.compile(r"(cimport +([0-9a-zA-Z_.]+)\b)|(from +([0-9a-zA-Z_.]+) +cimport)|(include +'([^']+)')|(cdef +extern +from +'([^']+)')")
234 for m in dependancy.finditer(source):
237 cimports.append(groups[1])
239 cimports.append(groups[3])
241 includes.append(literals[groups[5]])
243 externs.append(literals[groups[7]])
244 return cimports, includes, externs, distutils_info
247 class DependencyTree(object):
249 def __init__(self, context):
250 self.context = context
251 self._transitive_cache = {}
254 def parse_dependencies(self, source_filename):
255 return parse_dependencies(source_filename)
256 parse_dependencies = cached_method(parse_dependencies)
259 def cimports_and_externs(self, filename):
260 cimports, includes, externs = self.parse_dependencies(filename)[:3]
261 cimports = set(cimports)
262 externs = set(externs)
263 for include in includes:
264 include_path = os.path.join(os.path.dirname(filename), include)
265 if not os.path.exists(include_path):
266 include_path = self.context.find_include_file(include, None)
268 a, b = self.cimports_and_externs(include_path)
272 print("Unable to locate '%s' referenced from '%s'" % (filename, include))
273 return tuple(cimports), tuple(externs)
274 cimports_and_externs = cached_method(cimports_and_externs)
276 def cimports(self, filename):
277 return self.cimports_and_externs(filename)[0]
280 def package(self, filename):
281 dir = os.path.dirname(filename)
282 if os.path.exists(os.path.join(dir, '__init__.py')):
283 return self.package(dir) + (os.path.basename(dir),)
286 package = cached_method(package)
289 def fully_qualifeid_name(self, filename):
290 module = os.path.splitext(os.path.basename(filename))[0]
291 return '.'.join(self.package(filename) + (module,))
292 fully_qualifeid_name = cached_method(fully_qualifeid_name)
294 def find_pxd(self, module, filename=None):
296 raise NotImplementedError("New relative imports.")
297 if filename is not None:
298 relative = '.'.join(self.package(filename) + tuple(module.split('.')))
299 pxd = self.context.find_pxd_file(relative, None)
302 return self.context.find_pxd_file(module, None)
303 find_pxd = cached_method(find_pxd)
306 def cimported_files(self, filename):
307 if filename[-4:] == '.pyx' and os.path.exists(filename[:-4] + '.pxd'):
308 self_pxd = [filename[:-4] + '.pxd']
311 a = self.cimports(filename)
312 b = filter(None, [self.find_pxd(m, filename) for m in self.cimports(filename)])
313 if len(a) - int('cython' in a) != len(b):
314 print("missing cimport", filename)
315 print("\n\t".join(a))
316 print("\n\t".join(b))
317 return tuple(self_pxd + filter(None, [self.find_pxd(m, filename) for m in self.cimports(filename)]))
318 cimported_files = cached_method(cimported_files)
320 def immediate_dependencies(self, filename):
321 all = list(self.cimported_files(filename))
322 for extern in sum(self.cimports_and_externs(filename), ()):
323 all.append(os.path.normpath(os.path.join(os.path.dirname(filename), extern)))
327 def timestamp(self, filename):
328 return os.path.getmtime(filename)
329 timestamp = cached_method(timestamp)
331 def extract_timestamp(self, filename):
332 # TODO: .h files from extern blocks
333 return self.timestamp(filename), filename
335 def newest_dependency(self, filename):
336 return self.transitive_merge(filename, self.extract_timestamp, max)
338 def distutils_info0(self, filename):
339 return self.parse_dependencies(filename)[3]
341 def distutils_info(self, filename, aliases=None, base=None):
342 return (self.transitive_merge(filename, self.distutils_info0, DistutilsInfo.merge)
346 def transitive_merge(self, node, extract, merge):
348 seen = self._transitive_cache[extract, merge]
350 seen = self._transitive_cache[extract, merge] = {}
351 return self.transitive_merge_helper(
352 node, extract, merge, seen, {}, self.cimported_files)[0]
354 def transitive_merge_helper(self, node, extract, merge, seen, stack, outgoing):
356 return seen[node], None
361 stack[node] = len(stack)
363 for next in outgoing(node):
364 sub_deps, sub_loop = self.transitive_merge_helper(next, extract, merge, seen, stack, outgoing)
365 if sub_loop is not None:
366 if loop is not None and stack[loop] < stack[sub_loop]:
370 deps = merge(deps, sub_deps)
380 def create_dependency_tree(ctx=None):
382 if _dep_tree is None:
384 ctx = Context(["."], CompilationOptions(default_options))
385 _dep_tree = DependencyTree(ctx)
388 # This may be useful for advanced users?
389 def create_extension_list(patterns, exclude=[], ctx=None, aliases=None):
391 deps = create_dependency_tree(ctx)
393 if not isinstance(exclude, list):
395 for pattern in exclude:
396 to_exclude.update(glob(pattern))
397 if not isinstance(patterns, list):
398 patterns = [patterns]
400 for pattern in patterns:
401 if isinstance(pattern, str):
402 filepattern = pattern
407 elif isinstance(pattern, Extension):
408 filepattern = pattern.sources[0]
409 if os.path.splitext(filepattern)[1] not in ('.py', '.pyx'):
410 # ignore non-cython modules
411 module_list.append(pattern)
415 base = DistutilsInfo(exn=template)
416 exn_type = template.__class__
418 raise TypeError(pattern)
419 for file in glob(filepattern):
420 if file in to_exclude:
422 pkg = deps.package(file)
424 module_name = deps.fully_qualifeid_name(file)
427 if module_name not in seen:
428 kwds = deps.distutils_info(file, aliases, base).values
430 for key, value in base.values.items():
433 module_list.append(exn_type(
441 # This is the user-exposed entry point.
442 def cythonize(module_list, exclude=[], nthreads=0, aliases=None, quiet=False, **options):
443 if 'include_path' not in options:
444 options['include_path'] = ['.']
445 c_options = CompilationOptions(**options)
446 cpp_options = CompilationOptions(**options); cpp_options.cplus = True
447 ctx = c_options.create_context()
448 module_list = create_extension_list(
453 deps = create_dependency_tree(ctx)
455 for m in module_list:
457 for source in m.sources:
458 base, ext = os.path.splitext(source)
459 if ext in ('.pyx', '.py'):
460 if m.language == 'c++':
461 c_file = base + '.cpp'
462 options = cpp_options
466 if os.path.exists(c_file):
467 c_timestamp = os.path.getmtime(c_file)
470 # Priority goes first to modified files, second to direct
471 # dependents, and finally to indirect dependents.
472 if c_timestamp < deps.timestamp(source):
473 dep_timestamp, dep = deps.timestamp(source), source
476 dep_timestamp, dep = deps.newest_dependency(source)
477 priority = 2 - (dep in deps.immediate_dependencies(source))
478 if c_timestamp < dep_timestamp:
481 print("Compiling %s because it changed." % source)
483 print("Compiling %s because it depends on %s." % (source, dep))
484 to_compile.append((priority, source, c_file, options))
485 new_sources.append(c_file)
487 new_sources.append(source)
488 m.sources = new_sources
491 # Requires multiprocessing (or Python >= 2.6)
493 import multiprocessing
494 pool = multiprocessing.Pool(nthreads)
495 pool.map(cythonize_one_helper, to_compile)
497 print("multiprocessing required for parallel cythonization")
500 for priority, pyx_file, c_file, options in to_compile:
501 cythonize_one(pyx_file, c_file, options)
504 # TODO: Share context? Issue: pyx processing leaks into pxd module
505 def cythonize_one(pyx_file, c_file, options=None):
506 from Cython.Compiler.Main import compile, default_options
507 from Cython.Compiler.Errors import CompileError, PyrexError
510 options = CompilationOptions(default_options)
511 options.output_file = c_file
515 result = compile([pyx_file], options)
516 if result.num_errors > 0:
518 except (EnvironmentError, PyrexError), e:
519 sys.stderr.write(str(e) + '\n')
522 raise CompileError(None, pyx_file)
524 def cythonize_one_helper(m):
525 return cythonize_one(*m[1:])