Exemple #1
0
def source_to_dict(source, name=None):
    from Cython.Compiler.TreeFragment import parse_from_strings, StatListNode
    # Right now we don't collect errors, but leave the API compatible already.
    collected_errors = []

    try:

        # Note: we don't use TreeFragment because it formats the code removing empty lines
        # (which ends up creating an AST with wrong lines).
        if not name:
            name = "(tree fragment)"

        mod = t = parse_from_strings(name, source)
        t = t.body  # Make sure a StatListNode is at the top
        if not isinstance(t, StatListNode):
            t = StatListNode(pos=mod.pos, stats=[t])
        root = t
    except CompileError as e:
        return {
            'ast': None,
            'errors': [node_to_dict(e)]
        }
    except BaseException as e:
        as_dict = {
            'ast': None,
            'errors': [{
                '__node__': 'CompileError', 'line': 1, 'col': 1, 'message_only': str(e)
            }]
        }
        return as_dict

    result = {'ast': node_to_dict(root), 'errors': [node_to_dict(e) for e in collected_errors]}
    return result
Exemple #2
0
def unbound_symbols(code, context=None):
    if context is None:
        context = Context([], default_options)
    from Cython.Compiler.ParseTreeTransforms import AnalyseDeclarationsTransform
    tree = parse_from_strings('(tree fragment)', code)
    for phase in Pipeline.create_pipeline(context, 'pyx'):
        if phase is None:
            continue
        tree = phase(tree)
        if isinstance(phase, AnalyseDeclarationsTransform):
            break
    try:
        import builtins
    except ImportError:
        import __builtin__ as builtins
    return tuple(UnboundSymbols()(tree) - set(dir(builtins)))
Exemple #3
0
def parser(module):
    """ Read, parse a Cython code and generate an abstract syntaxique tree.
    
    Context: Compilation context: contains every pxd ever loaded, path information and the data related to the compilation.
    Class where it is implemented Cython parse method.
    
    options: To set Compilation Options as 
                language_level:     The source language level to use,
                formal_grammar:     to define if it will be used to Parse the file
                evaluate_tree_assertions:   To evaluate parse tree
                show_version :  To display version number
                use_listing_file:  Generate a .lis file
                errors_to_stderr:   Echo errors to stderr when using .lis
                include_path:    Directories to search for include files
                output_file:     Name of generated .c file
                generate_pxi:   Generate .pxi file for public declarations
                capi_reexport_cincludes:   Add cincluded headers to any auto-generated  header files.
                timestamps:   Only compile changed source files  
                verbose : Always print source names being compiled
                compiler_directives:    Overrides for pragma options (see Options.py)
                embedded_metadata:      Metadata to embed in the C file as json.
                evaluate_tree_assertions:  Test support: evaluate parse tree assertions
                cplus :     Compile as c++ code                
    
    Here default options were used except language level
    
    Scanning.FileSourceDescriptor: Represents a code source. Only file sources for Cython code supported
    """
    options = opt(**options_defaults)
    if isinstance(module, Path):
        context = Main.Context([os.path.dirname(module)], {},
                               cpp=False,
                               language_level=2,
                               options=options)
        scope = context.find_submodule(module)
        with open(module.encode('utf-8'), 'r') as f:
            source = f.read()
        source_desc = Scanning.FileSourceDescriptor(module, source)
        tree = context.parse(source_desc,
                             scope,
                             pxd=None,
                             full_module_name=module)
    else:
        from Cython.Compiler.TreeFragment import parse_from_strings
        if sys.version_info[0] < 3: module = unicode(module)
        tree = parse_from_strings("module", module)
    return tree
Exemple #4
0
def unbound_symbols(code, context=None):
    code = to_unicode(code)
    if context is None:
        context = Context([], default_options)
    from Cython.Compiler.ParseTreeTransforms import AnalyseDeclarationsTransform
    tree = parse_from_strings('(tree fragment)', code)
    for phase in Pipeline.create_pipeline(context, 'pyx'):
        if phase is None:
            continue
        tree = phase(tree)
        if isinstance(phase, AnalyseDeclarationsTransform):
            break
    try:
        import builtins
    except ImportError:
        import __builtin__ as builtins
    return UnboundSymbols()(tree) - set(dir(builtins))
Exemple #5
0
def extract(path, **kwargs):

    name = os.path.splitext(os.path.relpath(path))[0].replace('/', '.')

    options = CompilationOptions()
    options.include_path.append('include')
    options.language_level = 2
    options.compiler_directives = dict(
        c_string_type='str',
        c_string_encoding='ascii',
    )

    context = options.create_context()

    tree = parse_from_strings(name, open(path).read().decode('utf8'), context, **kwargs)

    extractor = Visitor({'file': path})
    extractor.visit(tree)
    return extractor.events
Exemple #6
0
def unbound_symbols(code, context=None):
    code = to_unicode(code)
    if context is None:
        context = Context([], default_options)
    from Cython.Compiler.ParseTreeTransforms import AnalyseDeclarationsTransform
    tree = parse_from_strings('(tree fragment)', code)
    for phase in context.create_pipeline(pxd=False):
        if phase is None:
            continue
        tree = phase(tree)
        if isinstance(phase, AnalyseDeclarationsTransform):
            break
    symbol_collector = AllSymbols()
    symbol_collector(tree)
    unbound = []
    import __builtin__
    for name in symbol_collector.names:
        if not tree.scope.lookup(name) and not hasattr(__builtin__, name):
            unbound.append(name)
    return unbound
Exemple #7
0
def extract(path, **kwargs):

    name = os.path.splitext(os.path.relpath(path))[0].replace('/', '.')

    options = CompilationOptions()
    options.include_path.append('include')
    options.language_level = 2
    options.compiler_directives = dict(
        c_string_type='str',
        c_string_encoding='ascii',
    )

    context = options.create_context()

    tree = parse_from_strings(name,
                              open(path).read().decode('utf8'), context,
                              **kwargs)

    extractor = Visitor({'file': path})
    extractor.visit(tree)
    return extractor.events
Exemple #8
0
def extract(path, **kwargs):

    name = os.path.splitext(os.path.relpath(path))[0].replace("/", ".")

    options = CompilationOptions()
    options.include_path.append("include")
    options.language_level = 2
    options.compiler_directives = dict(c_string_type="str",
                                       c_string_encoding="ascii")

    context = options.create_context()

    tree = parse_from_strings(
        name,
        open(path).read(),
        context,
        level="module_pxd" if path.endswith(".pxd") else None,
        **kwargs)

    extractor = Visitor({"file": path})
    extractor.visit(tree)
    return extractor.events
Exemple #9
0
def unbound_symbols(code, context=None):
    code = to_unicode(code)
    if context is None:
        context = Context([], default_options)
    from Cython.Compiler.ParseTreeTransforms import AnalyseDeclarationsTransform
    tree = parse_from_strings('(tree fragment)', code)
    for phase in Pipeline.create_pipeline(context, 'pyx'):
        if phase is None:
            continue
        tree = phase(tree)
        if isinstance(phase, AnalyseDeclarationsTransform):
            break
    symbol_collector = AllSymbols()
    symbol_collector(tree)
    unbound = []
    try:
        import builtins
    except ImportError:
        import __builtin__ as builtins
    for name in symbol_collector.names:
        if not tree.scope.lookup(name) and not hasattr(builtins, name):
            unbound.append(name)
    return unbound
Exemple #10
0
from Cython.Compiler.Visitor import TreeVisitor
from Cython.Compiler import Nodes
from Cython.Compiler.AutoDocTransforms import EmbedSignature

options = CompilationOptions()
options.include_path.append('include')
options.language_level = 2
options.compiler_directives = dict(
    c_string_type='str',
    c_string_encoding='ascii',
)

ctx = options.create_context()

tree = parse_from_strings('include.libavutil.avutil',
                          open('scratchpad/test.pxd').read().decode('utf8'),
                          ctx)


class Visitor(TreeVisitor):
    def __init__(self, state=None):
        super(Visitor, self).__init__()
        self.state = dict(state or {})
        self.events = []

    def record_event(self, node, **kw):
        state = self.state.copy()
        state.update(**kw)
        state['pos'] = node.pos
        state['end_pos'] = node.end_pos()
        self.events.append(state)
Exemple #11
0
from Cython.Compiler.TreeFragment import parse_from_strings
from Cython.Compiler.Visitor import TreeVisitor
from Cython.Compiler import Nodes
from Cython.Compiler.AutoDocTransforms import EmbedSignature

options = CompilationOptions()
options.include_path.append('include')
options.language_level = 2
options.compiler_directives = dict(
    c_string_type='str',
    c_string_encoding='ascii',
)

ctx = options.create_context()

tree = parse_from_strings('include.libavutil.avutil', open('scratchpad/test.pxd').read().decode('utf8'), ctx)

class Visitor(TreeVisitor):

    def __init__(self, state=None):
        super(Visitor, self).__init__()
        self.state = dict(state or {})
        self.events = []

    def record_event(self, node, **kw):
        state = self.state.copy()
        state.update(**kw)
        state['pos'] = node.pos
        state['end_pos'] = node.end_pos()
        self.events.append(state)