yahoo/panoptes

Python 3 eval() difference with list comprehension

Closed this issue · 1 comments

https://bugs.python.org/issue5242

class Test:

  def __init__(self):
    self.item = 'item'

    statement = """[item for item in ['this', 'test'] if item == self.item]"""
    print(eval(statement))


Test()

Causes the following error.

 print(eval(statement))
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <listcomp>
NameError: name 'self' is not defined

The change python3 made to the eval function causes an issue with certain plugins that reference theself object inside of a list comprehension.

https://github.com/yahoo/panoptes/blob/master/yahoo_panoptes/plugins/polling/generic/snmp/plugin_polling_generic_snmp.py#L307

The fix is to bring the self object in as local scope.
From The Above Example print(eval(statement, {'self':self}))
Internal Codebase

  • transform = eval(target_map[u'transform'], {'self': self})
  • eval(parsed_expression, {'self': self, 'index': index})

Thank you Git Gods.