Commit 77196736 authored by Xavier Thompson's avatar Xavier Thompson

Derive unique name for synthesized 'cimport cython'

parent 1309790e
...@@ -155,13 +155,17 @@ class CypclassWrapperInjection(CythonTransform): ...@@ -155,13 +155,17 @@ class CypclassWrapperInjection(CythonTransform):
self.derive_names(node) self.derive_names(node)
self.collected_cypclasses.append(node) self.collected_cypclasses.append(node)
def create_unique_name(self, nested_name, suffix): def create_unique_name(self, name):
wrapper_name = "%s%s" % (nested_name, suffix) # output: name(_u_*)?
while wrapper_name in self.module_scope.entries: # guarantees:
# the end of the suffix must be modified to avoid collisions # - different inputs always result in different outputs
# between cypclasses having the same prefix, e.g. 'A' and 'A_'. # - the output is not in the module scope dictionary
wrapper_name = "%s_" % (wrapper_name, suffix) unique_name = name
return EncodedString(wrapper_name) if unique_name in self.module_scope.entries:
unique_name = "%s_u" % unique_name
while unique_name in self.module_scope.entries:
unique_name = "%s_" % unique_name
return EncodedString(unique_name)
def derive_names(self, node): def derive_names(self, node):
nested_names = [node.name for node in self.nesting_stack] nested_names = [node.name for node in self.nesting_stack]
...@@ -172,18 +176,20 @@ class CypclassWrapperInjection(CythonTransform): ...@@ -172,18 +176,20 @@ class CypclassWrapperInjection(CythonTransform):
nested_name = "_".join(nested_names) nested_name = "_".join(nested_names)
cclass_name = self.create_unique_name(nested_name, "__cyp_cclass_wrapper") cclass_name = self.create_unique_name("%s_cyp_cclass_wrapper" % nested_name)
pyclass_name = self.create_unique_name(nested_name, "__cyp_pyclass_wrapper") pyclass_name = self.create_unique_name("%s_cyp_pyclass_wrapper" % nested_name)
self.type_to_names[node.entry.type] = qualified_name, cclass_name, pyclass_name self.type_to_names[node.entry.type] = qualified_name, cclass_name, pyclass_name
def inject_cypclass_wrappers(self, module_node): def inject_cypclass_wrappers(self, module_node):
if self.cimport_cython: if self.cimport_cython:
# cimport cython to access the @cython.binding decorator # cimport cython to access the @cython.binding decorator
# use a unique name for "cimport cython as <name>" if necessary
as_name = self.create_unique_name("cython")
cimport_stmt = Nodes.CImportStatNode( cimport_stmt = Nodes.CImportStatNode(
module_node.pos, module_node.pos,
module_name="cython", module_name="cython",
as_name=None, as_name=None if as_name == "cython" else as_name,
is_absolute=True is_absolute=True
) )
self.wrappers.append(cimport_stmt) self.wrappers.append(cimport_stmt)
......
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