Author pekka.klarck
Recipients Martin Hosken, eamanu, pekka.klarck, terry.reedy
Date 2019-06-07.14:54:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1559919297.95.0.617559323573.issue36300@roundup.psfhosted.org>
In-reply-to
Content
More ways to be bitten by this strange behavior:

    >>> d = {'a': 1, 'b': 2}
    >>> eval('[x[k] for k in x]', {}, {'x': d})
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
      File "<string>", line 1, in <listcomp>
    NameError: name 'x' is not defined
    >>> 
    >>> def f():
    ...     x = {'a': 1, 'b': 2}
    ...     return eval('[x[k] for k in x]')
    ... 
    >>> f()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in f
      File "<string>", line 1, in <module>
      File "<string>", line 1, in <listcomp>
    NameError: name 'x' is not defined



In both of the above cases changing

    eval('[x[k] for k in x]')

to

    eval('[v for v in x.values()]')

avoids the problem. There are no problems when using

    [x[k] for k in x]

without `eval()` either. I'd prefer this to be changed, but there should at least be a note in the documentation of `eval()` about this.
History
Date User Action Args
2019-06-07 14:54:57pekka.klarcksetrecipients: + pekka.klarck, terry.reedy, eamanu, Martin Hosken
2019-06-07 14:54:57pekka.klarcksetmessageid: <1559919297.95.0.617559323573.issue36300@roundup.psfhosted.org>
2019-06-07 14:54:57pekka.klarcklinkissue36300 messages
2019-06-07 14:54:57pekka.klarckcreate