Commit b5ef664c authored by Robert Bradshaw's avatar Robert Bradshaw

Allow type objects in utility code context.

This avoids issues of having to inject/name them
(and any conversions, see e.g. Issue #1737).
parent 9b3dd8ec
...@@ -77,7 +77,13 @@ class CythonUtilityCode(Code.UtilityCodeBase): ...@@ -77,7 +77,13 @@ class CythonUtilityCode(Code.UtilityCodeBase):
# while the generated node trees can be altered in the compilation of a # while the generated node trees can be altered in the compilation of a
# single file. # single file.
# Hence, delay any processing until later. # Hence, delay any processing until later.
context_types = {}
if context is not None: if context is not None:
from .PyrexTypes import BaseType
for key, value in context.items():
if isinstance(value, BaseType):
context[key] = key
context_types[key] = value
impl = Code.sub_tempita(impl, context, file, name) impl = Code.sub_tempita(impl, context, file, name)
self.impl = impl self.impl = impl
self.name = name self.name = name
...@@ -87,6 +93,7 @@ class CythonUtilityCode(Code.UtilityCodeBase): ...@@ -87,6 +93,7 @@ class CythonUtilityCode(Code.UtilityCodeBase):
self.from_scope = from_scope self.from_scope = from_scope
self.outer_module_scope = outer_module_scope self.outer_module_scope = outer_module_scope
self.compiler_directives = compiler_directives self.compiler_directives = compiler_directives
self.context_types = context_types
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, CythonUtilityCode): if isinstance(other, CythonUtilityCode):
...@@ -163,6 +170,17 @@ class CythonUtilityCode(Code.UtilityCodeBase): ...@@ -163,6 +170,17 @@ class CythonUtilityCode(Code.UtilityCodeBase):
pipeline, scope_transform, pipeline, scope_transform,
before=ParseTreeTransforms.AnalyseDeclarationsTransform) before=ParseTreeTransforms.AnalyseDeclarationsTransform)
if self.context_types:
# inject types into module scope
def scope_transform(module_node):
for name, type in self.context_types.items():
module_node.scope.declare_type(name, type, None, visibility='extern')
return module_node
pipeline = Pipeline.insert_into_pipeline(
pipeline, scope_transform,
before=ParseTreeTransforms.AnalyseDeclarationsTransform)
(err, tree) = Pipeline.run_pipeline(pipeline, tree, printtree=False) (err, tree) = Pipeline.run_pipeline(pipeline, tree, printtree=False)
assert not err, err assert not err, err
self.tree = tree self.tree = tree
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment