compile_python_variable assumes that repr() of variable's value is a valid Python expression

Bug #349482 reported by James Henstridge
10
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Storm
Fix Released
Medium
James Henstridge

Bug Description

The compile_python handler for Variable objects is:

    @compile_python.when(Variable)
    def compile_python_variable(compile, variable, state):
        return repr(variable.get())

This assumes that the repr() of the value inside the variable will be a valid Python expression (and more concretely, a valid expression with storm.expr as the globals).

There are a number of ways this can cause problems:
1. Values that have a repr() like "<something at 0xNNNN>" will give a syntax error, even if there is a compile_python handler for its class.

2. For e.g. UUID objects, while the repr is a valid Python expression, it requires that the name "UUID" be bound:

    >>> import uuid
    >>> from storm.expr import compile_python
    >>> from storm.variables import UUIDVariable
    >>> var = UUIDVariable(uuid.uuid4())
    >>> matcher = compile_python.get_matcher(var)
    >>> matcher(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in match
    NameError: global name 'UUID' is not defined

    >>> import storm.expr
    >>> storm.expr.UUID = uuid.UUID
    >>> matcher(None)
    True

As e.g. ResultSet.set() is only checking for CompileErrors, this causes the method to fail when it would otherwise be able to succeed.

Suggested changes:

1. update compile_python_variable to delegate compilation to the value:

    @compile_python.when(Variable)
    def compile_python_variable(compile, variable, state):
        return compile(variable.get(), state)

2. define some way for compile_python handlers to inject names into the scope for the matcher function (probably via the state variable).

3. add compile_python handlers for any variable values that don't have them currently.

Tags: tech-debt

Related branches

Changed in storm:
importance: Undecided → Medium
status: New → In Progress
assignee: nobody → James Henstridge (jamesh)
Revision history for this message
James Henstridge (jamesh) wrote :

Fix merged to trunk in r307.

Changed in storm:
milestone: none → 0.15
status: In Progress → Fix Committed
Jamu Kakar (jkakar)
Changed in storm:
status: Fix Committed → Fix Released
Curtis Hovey (sinzui)
tags: added: tech-debt
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.