1
from .TreeFragment import parse_from_strings, StringParseContext
9
class NonManglingModuleScope(Symtab.ModuleScope):
11
def __init__(self, prefix, *args, **kw):
13
self.cython_scope = None
14
self.cpp = kw.pop('cpp', False)
15
Symtab.ModuleScope.__init__(self, *args, **kw)
17
def add_imported_entry(self, name, entry, pos):
19
return super().add_imported_entry(name, entry, pos)
21
def mangle(self, prefix, name=None):
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)
27
return "%s%s" % (prefix, name)
29
return Symtab.ModuleScope.mangle(self, prefix)
32
class CythonUtilityCodeContext(StringParseContext):
35
def find_module(self, module_name, from_module=None, pos=None, need_pxd=True, absolute_fallback=True, relative_import=False):
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.")
42
return self.modules[module_name]
44
if self.scope is None:
45
self.scope = NonManglingModuleScope(
46
self.prefix, module_name, parent_module=None, context=self, cpp=self.cpp)
51
class CythonUtilityCode(Code.UtilityCodeBase):
53
Utility code written in the Cython language itself.
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.
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>
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.
68
is_cython_utility = True
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
78
# Hence, delay any processing until later.
80
if context is not None:
81
from .PyrexTypes import BaseType
82
for key, value in context.items():
83
if isinstance(value, BaseType):
85
context_types[key] = value
86
impl = Code.sub_tempita(impl, context, file, name)
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
97
def __eq__(self, other):
98
if isinstance(other, CythonUtilityCode):
99
return self._equality_params() == other._equality_params()
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
110
return hash(self.impl)
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]
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)
135
if isinstance(t, ParseTreeTransforms.AnalyseDeclarationsTransform):
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,
147
def merge_scope(scope):
148
def merge_scope_transform(module_node):
149
module_node.scope.merge_in(scope)
151
return merge_scope_transform
154
pipeline = Pipeline.insert_into_pipeline(
155
pipeline, merge_scope(self.from_scope),
156
before=ParseTreeTransforms.AnalyseDeclarationsTransform)
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)
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
170
pipeline = Pipeline.insert_into_pipeline(
171
pipeline, scope_transform,
172
before=ParseTreeTransforms.AnalyseDeclarationsTransform)
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
189
pipeline = Pipeline.insert_into_pipeline(
190
pipeline, scope_transform,
191
before=ParseTreeTransforms.AnalyseDeclarationsTransform)
193
(err, tree) = Pipeline.run_pipeline(pipeline, tree, printtree=False)
198
def put_code(self, output):
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)
210
def load_as_string(cls, util_code_name, from_file=None, **kwargs):
212
Load a utility code as a string. Returns (proto, implementation)
214
util = cls.load(util_code_name, from_file, **kwargs)
215
return util.proto, util.impl # keep line numbers => no lstrip()
217
def declare_in_scope(self, dest_scope, used=False, cython_scope=None,
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.
224
tree = self.get_tree(entries_only=True, cython_scope=cython_scope)
226
entries = tree.scope.entries
227
entries.pop('__name__')
228
entries.pop('__file__')
229
entries.pop('__builtins__')
230
entries.pop('__doc__')
232
for entry in entries.values():
233
entry.utility_code_definition = self
236
original_scope = tree.scope
237
dest_scope.merge_in(original_scope, merge_unused=True, allowlist=allowlist)
238
tree.scope = dest_scope
240
for dep in self.requires:
241
if dep.is_cython_utility:
242
dep.declare_in_scope(dest_scope, cython_scope=cython_scope)
244
return original_scope
247
def filter_inherited_directives(current_directives):
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.
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
268
def declare_declarations_in_scope(declaration_string, env, private_type=True,
271
Declare some declarations given as Cython code in declaration_string
274
CythonUtilityCode(declaration_string, *args, **kwargs).declare_in_scope(env)