cython

Форк
0
/
UtilityCode.py 
274 строки · 11.0 Кб
1
from .TreeFragment import parse_from_strings, StringParseContext
2
from . import Symtab
3
from . import Naming
4
from . import Code
5

6
import re
7

8

9
class NonManglingModuleScope(Symtab.ModuleScope):
10

11
    def __init__(self, prefix, *args, **kw):
12
        self.prefix = prefix
13
        self.cython_scope = None
14
        self.cpp = kw.pop('cpp', False)
15
        Symtab.ModuleScope.__init__(self, *args, **kw)
16

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

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

31

32
class CythonUtilityCodeContext(StringParseContext):
33
    scope = None
34

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

44
        if self.scope is None:
45
            self.scope = NonManglingModuleScope(
46
                self.prefix, module_name, parent_module=None, context=self, cpp=self.cpp)
47

48
        return self.scope
49

50

51
class CythonUtilityCode(Code.UtilityCodeBase):
52
    """
53
    Utility code written in the Cython language itself.
54

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

58
    For cdef classes the rules are as follows:
59
        obj struct      -> <cname>_obj
60
        obj type ptr    -> <cname>_type
61
        methods         -> <class_cname>_<method_cname>
62

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

68
    is_cython_utility = True
69

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

97
    def __eq__(self, other):
98
        if isinstance(other, CythonUtilityCode):
99
            return self._equality_params() == other._equality_params()
100
        else:
101
            return False
102

103
    def _equality_params(self):
104
        outer_scope = self.outer_module_scope
105
        while isinstance(outer_scope, NonManglingModuleScope):
106
            outer_scope = outer_scope.outer_scope
107
        return self.impl, outer_scope, self.compiler_directives
108

109
    def __hash__(self):
110
        return hash(self.impl)
111

112
    def get_tree(self, entries_only=False, cython_scope=None):
113
        from .AnalysedTreeTransforms import AutoTestDictTransform
114
        # The AutoTestDictTransform creates the statement "__test__ = {}",
115
        # which when copied into the main ModuleNode overwrites
116
        # any __test__ in user code; not desired
117
        excludes = [AutoTestDictTransform]
118

119
        from . import Pipeline, ParseTreeTransforms
120
        context = CythonUtilityCodeContext(
121
            self.name, compiler_directives=self.compiler_directives,
122
            cpp=cython_scope.is_cpp() if cython_scope else False)
123
        context.prefix = self.prefix
124
        context.cython_scope = cython_scope
125
        #context = StringParseContext(self.name)
126
        tree = parse_from_strings(
127
            self.name, self.impl, context=context, allow_struct_enum_decorator=True,
128
            in_utility_code=True)
129
        pipeline = Pipeline.create_pipeline(context, 'pyx', exclude_classes=excludes)
130

131
        if entries_only:
132
            p = []
133
            for t in pipeline:
134
                p.append(t)
135
                if isinstance(t, ParseTreeTransforms.AnalyseDeclarationsTransform):
136
                    break
137

138
            pipeline = p
139

140
        transform = ParseTreeTransforms.CnameDirectivesTransform(context)
141
        # InterpretCompilerDirectives already does a cdef declarator check
142
        #before = ParseTreeTransforms.DecoratorTransform
143
        before = ParseTreeTransforms.InterpretCompilerDirectives
144
        pipeline = Pipeline.insert_into_pipeline(pipeline, transform,
145
                                                 before=before)
146

147
        def merge_scope(scope):
148
            def merge_scope_transform(module_node):
149
                module_node.scope.merge_in(scope)
150
                return module_node
151
            return merge_scope_transform
152

153
        if self.from_scope:
154
            pipeline = Pipeline.insert_into_pipeline(
155
                pipeline, merge_scope(self.from_scope),
156
                before=ParseTreeTransforms.AnalyseDeclarationsTransform)
157

158
        for dep in self.requires:
159
            if isinstance(dep, CythonUtilityCode) and hasattr(dep, 'tree') and not cython_scope:
160
                pipeline = Pipeline.insert_into_pipeline(
161
                    pipeline, merge_scope(dep.tree.scope),
162
                    before=ParseTreeTransforms.AnalyseDeclarationsTransform)
163

164
        if self.outer_module_scope:
165
            # inject outer module between utility code module and builtin module
166
            def scope_transform(module_node):
167
                module_node.scope.outer_scope = self.outer_module_scope
168
                return module_node
169

170
            pipeline = Pipeline.insert_into_pipeline(
171
                pipeline, scope_transform,
172
                before=ParseTreeTransforms.AnalyseDeclarationsTransform)
173

174
        if self.context_types:
175
            # inject types into module scope
176
            def scope_transform(module_node):
177
                dummy_entry = object()
178
                for name, type in self.context_types.items():
179
                    # Restore the old type entry after declaring the type.
180
                    # We need to access types in the scope, but this shouldn't alter the entry
181
                    # that is visible from everywhere else
182
                    old_type_entry = getattr(type, "entry", dummy_entry)
183
                    entry = module_node.scope.declare_type(name, type, None, visibility='extern')
184
                    if old_type_entry is not dummy_entry:
185
                        type.entry = old_type_entry
186
                    entry.in_cinclude = True
187
                return module_node
188

189
            pipeline = Pipeline.insert_into_pipeline(
190
                pipeline, scope_transform,
191
                before=ParseTreeTransforms.AnalyseDeclarationsTransform)
192

193
        (err, tree) = Pipeline.run_pipeline(pipeline, tree, printtree=False)
194
        assert not err, err
195
        self.tree = tree
196
        return tree
197

198
    def put_code(self, output):
199
        pass
200

201
    @classmethod
202
    def load(cls, util_code_name, from_file, **kwargs):
203
        if re.search("[.]c(pp)?::", util_code_name):
204
            # We're trying to load a C/C++ utility code.
205
            # For now, just handle the simple case with no tempita
206
            return Code.UtilityCode.load_cached(util_code_name, from_file)
207
        return super().load(util_code_name, from_file, **kwargs)
208

209
    @classmethod
210
    def load_as_string(cls, util_code_name, from_file=None, **kwargs):
211
        """
212
        Load a utility code as a string. Returns (proto, implementation)
213
        """
214
        util = cls.load(util_code_name, from_file, **kwargs)
215
        return util.proto, util.impl  # keep line numbers => no lstrip()
216

217
    def declare_in_scope(self, dest_scope, used=False, cython_scope=None,
218
                         allowlist=None):
219
        """
220
        Declare all entries from the utility code in dest_scope. Code will only
221
        be included for used entries. If module_name is given, declare the
222
        type entries with that name.
223
        """
224
        tree = self.get_tree(entries_only=True, cython_scope=cython_scope)
225

226
        entries = tree.scope.entries
227
        entries.pop('__name__')
228
        entries.pop('__file__')
229
        entries.pop('__builtins__')
230
        entries.pop('__doc__')
231

232
        for entry in entries.values():
233
            entry.utility_code_definition = self
234
            entry.used = used
235

236
        original_scope = tree.scope
237
        dest_scope.merge_in(original_scope, merge_unused=True, allowlist=allowlist)
238
        tree.scope = dest_scope
239

240
        for dep in self.requires:
241
            if dep.is_cython_utility:
242
                dep.declare_in_scope(dest_scope, cython_scope=cython_scope)
243

244
        return original_scope
245

246
    @staticmethod
247
    def filter_inherited_directives(current_directives):
248
        """
249
        Cython utility code should usually only pick up a few directives from the
250
        environment (those that intentionally control its function) and ignore most
251
        other compiler directives. This function provides a sensible default list
252
        of directives to copy.
253
        """
254
        from .Options import _directive_defaults
255
        utility_code_directives = dict(_directive_defaults)
256
        inherited_directive_names = (
257
            'binding', 'always_allow_keywords', 'allow_none_for_extension_args',
258
            'auto_pickle', 'ccomplex',
259
            'c_string_type', 'c_string_encoding',
260
            'optimize.inline_defnode_calls', 'optimize.unpack_method_calls',
261
            'optimize.unpack_method_calls_in_pyinit', 'optimize.use_switch')
262
        for name in inherited_directive_names:
263
            if name in current_directives:
264
                utility_code_directives[name] = current_directives[name]
265
        return utility_code_directives
266

267

268
def declare_declarations_in_scope(declaration_string, env, private_type=True,
269
                                  *args, **kwargs):
270
    """
271
    Declare some declarations given as Cython code in declaration_string
272
    in scope env.
273
    """
274
    CythonUtilityCode(declaration_string, *args, **kwargs).declare_in_scope(env)
275

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.