def inject_types(source_path, types, type_map=default_type_map, mode='python'): """ Hack type declarations into source code file. @param mode is currently 'python', which means that the generated type declarations use pure Python syntax. """ col_and_types_by_line = dict( # {line: (column, scope_name or None, [(name, type)])} (k[-1][0], (k[-1][1], k[0], [(n, next(iter(t))) for (n, t) in v.items() if len(t) == 1])) for (k, v) in types.items()) lines = [u'import cython\n'] with open_source_file(source_path) as f: for line_no, line in enumerate(f, 1): if line_no in col_and_types_by_line: col, scope, types = col_and_types_by_line[line_no] if types: types = ', '.join( "%s='%s'" % (name, type_map.get(type_name, type_name)) for name, type_name in types) if scope is None: type_decl = u'{indent}cython.declare({types})\n' else: type_decl = u'{indent}@cython.locals({types})\n' lines.append( type_decl.format(indent=' ' * col, types=types)) lines.append(line) return lines
def inject_types(source_path, types, type_map=default_type_map, mode='python'): """ Hack type declarations into source code file. @param mode is currently 'python', which means that the generated type declarations use pure Python syntax. """ col_and_types_by_line = dict( # {line: (column, scope_name or None, [(name, type)])} (k[-1][0], (k[-1][1], k[0], [(n, next(iter(t))) for (n, t) in v.items() if len(t) == 1])) for (k, v) in types.items()) lines = [u'import cython\n'] with open_source_file(source_path) as f: for line_no, line in enumerate(f, 1): if line_no in col_and_types_by_line: col, scope, types = col_and_types_by_line[line_no] if types: types = ', '.join("%s='%s'" % (name, type_map.get(type_name, type_name)) for name, type_name in types) if scope is None: type_decl = u'{indent}cython.declare({types})\n' else: type_decl = u'{indent}@cython.locals({types})\n' lines.append(type_decl.format(indent=' '*col, types=types)) lines.append(line) return lines
def read_distribution(src_file): """ Parse ``src_file`` for a ``# sage_setup: distribution = PKG`` directive. INPUT: - ``src_file`` -- file name of a Python or Cython source file OUTPUT: - a string, the name of the distribution package (``PKG``); or the empty string if no directive was found. EXAMPLES:: sage: from sage.env import SAGE_SRC sage: from sage_setup.find import read_distribution sage: read_distribution(os.path.join(SAGE_SRC, 'sage', 'graphs', 'graph_decompositions', 'tdlib.pyx')) 'sage-tdlib' sage: read_distribution(os.path.join(SAGE_SRC, 'sage', 'graphs', 'graph_decompositions', 'modular_decomposition.py')) '' """ from Cython.Utils import open_source_file with open_source_file(src_file, error_handling='ignore') as fh: for line in fh: # Adapted from Cython's Build/Dependencies.py line = line.lstrip() if not line: continue if line[0] != '#': break line = line[1:].lstrip() kind = "sage_setup:" if line.startswith(kind): key, _, value = [ s.strip() for s in line[len(kind):].partition('=') ] if key == "distribution": return value return ''