Page 1 of 1

dict and set comprehensions wrapping

Posted: Sat Mar 07, 2015 8:04 am
by rivvil
Hello.
Recently I found out that Python dict and set comprehensions used within .rpy scripts do not produce RevertableDict and RevertableSet objects. In contrast to this behavior list comprehensions create instances of RevertableList.
Personally I would prefer if all kinds of comprehensions will produce "revertable objects". It seems to be more consistent approach as well.
So, is this intentional difference between list and set/dict comprehensions?

EDIT:
For now, by analogy with existent wrappers I've added two methods into WrapNode class (renpy/python.py, line 329):

Code: Select all

def visit_DictComp(self, n):
    return ast.Call(
        func = ast.Name(
            id="__renpy__dict__",
            ctx=ast.Load()
            ),
        args = [ self.generic_visit(n) ],
        keywords = [ ],
        starargs = None,
        kwargs = None)

def visit_SetComp(self, n):
    return ast.Call(
        func = ast.Name(
            id="set",
            ctx=ast.Load()
            ),
        args = [ self.generic_visit(n) ],
        keywords = [ ],
        starargs = None,
        kwargs = None)
It seems to be working. But I'm not an expert in pythonic AST manipulations. So, I'm using this technique at my own risk :)

Re: dict and set comprehensions wrapping

Posted: Sat Mar 07, 2015 11:26 am
by PyTom
This is absolutely correct, and somewhat embarrassing oversight on my part. I've merged your code into Ren'Py, it will be in the next 6.99 prerelease.

Thank you!

Re: dict and set comprehensions wrapping

Posted: Sat Mar 07, 2015 2:35 pm
by rivvil
You're welcome. Glad to help :)