[Cython] analyse_types() refactoring

Stefan Behnel stefan_ml at behnel.de
Thu Feb 7 18:32:59 CET 2013


Hi,

I finally found the time to refactor the analysis phase.

https://github.com/cython/cython/commit/f9c385e08401ed96b5b0afb8411480037dc772b9

The methods now return a node, which allows them to replace themselves with
a different implementation.

Note that the relatively large code impact of this change also means that
you might easily run into merge conflicts with your own local changes, so
here's how to fix them. The transformation pattern is pretty straight
forward. The "analyse_types()" method returns "self", unless it wants to
replace itself, i.e. this

    def analyse_types(self, env):
        self.index.analyse_types(env)

becomes

    def analyse_types(self, env):
        self.index = self.index.analyse_types(env)
        return self

The "analyse_target_types()" method works the same, but because it calls
"analyse_types()" internally in most cases, it's more likely to look like this:

    def analyse_target_types(self, env):
        self.analyse_types(env)
        if self.type.is_pyobject:
            self.type = py_object_type

which now turns into this:

    def analyse_target_types(self, env):
        node = self.analyse_types(env)
        if node.type.is_pyobject:
            node.type = py_object_type
        return node

The same pattern obviously applies in the cases where the node needs to be
replaced in "analyse_types()". It would simply build and return a different
node. This also allows for in-place coercions of the current node, for example.

With this change in place, we can now start to clean up old hacks like the
"__class__" replacement in AttributeNode. If anyone wants to give it a try,
please go ahead. :)

Stefan


More information about the cython-devel mailing list