UtilityCode.py 8.01 KB
Newer Older
1 2 3 4 5 6 7
from __future__ import absolute_import

from .TreeFragment import parse_from_strings, StringParseContext
from . import Symtab
from . import Naming
from . import Code

8

9 10
class NonManglingModuleScope(Symtab.ModuleScope):

11 12
    cpp = False

13 14
    def __init__(self, prefix, *args, **kw):
        self.prefix = prefix
15
        self.cython_scope = None
16 17
        Symtab.ModuleScope.__init__(self, *args, **kw)

18 19 20 21
    def add_imported_entry(self, name, entry, pos):
        entry.used = True
        return super(NonManglingModuleScope, self).add_imported_entry(
                                                        name, entry, pos)
22

23 24
    def mangle(self, prefix, name=None):
        if name:
25
            if prefix in (Naming.typeobj_prefix, Naming.func_prefix, Naming.var_prefix, Naming.pyfunc_prefix):
26 27 28 29 30
                # Functions, classes etc. gets a manually defined prefix easily
                # manually callable instead (the one passed to CythonUtilityCode)
                prefix = self.prefix
            return "%s%s" % (prefix, name)
        else:
31
            return Symtab.ModuleScope.mangle(self, prefix)
32

Stefan Behnel's avatar
Stefan Behnel committed
33

34 35 36
class CythonUtilityCodeContext(StringParseContext):
    scope = None

37 38 39
    def find_module(self, module_name, relative_to=None, pos=None, need_pxd=True, absolute_fallback=True):
        if relative_to:
            raise AssertionError("Relative imports not supported in utility code.")
40
        if module_name != self.module_name:
41 42 43 44
            if module_name not in self.modules:
                raise AssertionError("Only the cython cimport is supported.")
            else:
                return self.modules[module_name]
45 46

        if self.scope is None:
Stefan Behnel's avatar
Stefan Behnel committed
47 48
            self.scope = NonManglingModuleScope(
                self.prefix, module_name, parent_module=None, context=self)
49 50 51

        return self.scope

52

53
class CythonUtilityCode(Code.UtilityCodeBase):
54 55
    """
    Utility code written in the Cython language itself.
56 57 58 59 60

    The @cname decorator can set the cname for a function, method of cdef class.
    Functions decorated with @cname('c_func_name') get the given cname.

    For cdef classes the rules are as follows:
61 62
        obj struct      -> <cname>_obj
        obj type ptr    -> <cname>_type
63 64 65 66 67
        methods         -> <class_cname>_<method_cname>

    For methods the cname decorator is optional, but without the decorator the
    methods will not be prototyped. See Cython.Compiler.CythonScope and
    tests/run/cythonscope.pyx for examples.
68 69
    """

70 71
    is_cython_utility = True

72
    def __init__(self, impl, name="__pyxutil", prefix="", requires=None,
73 74
                 file=None, from_scope=None, context=None, compiler_directives=None,
                 outer_module_scope=None):
75 76 77 78 79 80
        # 1) We need to delay the parsing/processing, so that all modules can be
        #    imported without import loops
        # 2) The same utility code object can be used for multiple source files;
        #    while the generated node trees can be altered in the compilation of a
        #    single file.
        # Hence, delay any processing until later.
81 82
        if context is not None:
            impl = Code.sub_tempita(impl, context, file, name)
83
        self.impl = impl
84
        self.name = name
85
        self.file = file
86
        self.prefix = prefix
87
        self.requires = requires or []
88
        self.from_scope = from_scope
89
        self.outer_module_scope = outer_module_scope
90
        self.compiler_directives = compiler_directives
91

92 93 94 95
    def __eq__(self, other):
        if isinstance(other, CythonUtilityCode):
            return self._equality_params() == other._equality_params()
        else:
Stefan Behnel's avatar
Stefan Behnel committed
96
            return False
97 98

    def _equality_params(self):
99 100 101 102
        outer_scope = self.outer_module_scope
        while isinstance(outer_scope, NonManglingModuleScope):
            outer_scope = outer_scope.outer_scope
        return self.impl, outer_scope, self.compiler_directives
103 104 105 106

    def __hash__(self):
        return hash(self.impl)

107
    def get_tree(self, entries_only=False, cython_scope=None):
108
        from .AnalysedTreeTransforms import AutoTestDictTransform
109 110 111
        # The AutoTestDictTransform creates the statement "__test__ = {}",
        # which when copied into the main ModuleNode overwrites
        # any __test__ in user code; not desired
112
        excludes = [AutoTestDictTransform]
Vitja Makarov's avatar
Vitja Makarov committed
113

114
        from . import Pipeline, ParseTreeTransforms
115 116
        context = CythonUtilityCodeContext(
            self.name, compiler_directives=self.compiler_directives)
117
        context.prefix = self.prefix
118
        context.cython_scope = cython_scope
119
        #context = StringParseContext(self.name)
Stefan Behnel's avatar
Stefan Behnel committed
120 121
        tree = parse_from_strings(
            self.name, self.impl, context=context, allow_struct_enum_decorator=True)
122
        pipeline = Pipeline.create_pipeline(context, 'pyx', exclude_classes=excludes)
123

Mark Florisson's avatar
Mark Florisson committed
124 125 126 127 128 129 130 131 132
        if entries_only:
            p = []
            for t in pipeline:
                p.append(t)
                if isinstance(p, ParseTreeTransforms.AnalyseDeclarationsTransform):
                    break

            pipeline = p

133 134 135 136 137 138 139
        transform = ParseTreeTransforms.CnameDirectivesTransform(context)
        # InterpretCompilerDirectives already does a cdef declarator check
        #before = ParseTreeTransforms.DecoratorTransform
        before = ParseTreeTransforms.InterpretCompilerDirectives
        pipeline = Pipeline.insert_into_pipeline(pipeline, transform,
                                                 before=before)

140 141 142 143 144 145
        if self.from_scope:
            def scope_transform(module_node):
                module_node.scope.merge_in(self.from_scope)
                return module_node

            transform = ParseTreeTransforms.AnalyseDeclarationsTransform
146 147 148 149 150 151 152 153 154 155
            pipeline = Pipeline.insert_into_pipeline(pipeline, scope_transform,
                                                     before=transform)

        if self.outer_module_scope:
            # inject outer module between utility code module and builtin module
            def scope_transform(module_node):
                module_node.scope.outer_scope = self.outer_module_scope
                return module_node

            transform = ParseTreeTransforms.AnalyseDeclarationsTransform
156 157
            pipeline = Pipeline.insert_into_pipeline(pipeline, scope_transform,
                                                     before=transform)
158

159
        (err, tree) = Pipeline.run_pipeline(pipeline, tree, printtree=False)
160
        assert not err, err
161 162 163 164
        return tree

    def put_code(self, output):
        pass
165

166 167 168 169 170 171 172 173
    @classmethod
    def load_as_string(cls, util_code_name, from_file=None, **kwargs):
        """
        Load a utility code as a string. Returns (proto, implementation)
        """
        util = cls.load(util_code_name, from_file, **kwargs)
        return util.proto, util.impl # keep line numbers => no lstrip()

174 175
    def declare_in_scope(self, dest_scope, used=False, cython_scope=None,
                         whitelist=None):
176 177
        """
        Declare all entries from the utility code in dest_scope. Code will only
178 179
        be included for used entries. If module_name is given, declare the
        type entries with that name.
180
        """
181
        tree = self.get_tree(entries_only=True, cython_scope=cython_scope)
182

Mark Florisson's avatar
Mark Florisson committed
183
        entries = tree.scope.entries
184 185 186 187 188 189 190
        entries.pop('__name__')
        entries.pop('__file__')
        entries.pop('__builtins__')
        entries.pop('__doc__')

        for name, entry in entries.iteritems():
            entry.utility_code_definition = self
191
            entry.used = used
192

193
        original_scope = tree.scope
194 195
        dest_scope.merge_in(original_scope, merge_unused=True,
                            whitelist=whitelist)
Mark Florisson's avatar
Mark Florisson committed
196
        tree.scope = dest_scope
197 198 199 200

        for dep in self.requires:
            if dep.is_cython_utility:
                dep.declare_in_scope(dest_scope)
201 202

        return original_scope
203 204 205 206 207 208 209 210

def declare_declarations_in_scope(declaration_string, env, private_type=True,
                                  *args, **kwargs):
    """
    Declare some declarations given as Cython code in declaration_string
    in scope env.
    """
    CythonUtilityCode(declaration_string, *args, **kwargs).declare_in_scope(env)