On Sat, Feb 2, 2013 at 11:38 AM, Hua Lu <gotoalanlu@gmail.com> wrote:
>>> import ast
>>> ast.literal_eval("{ 'key': 'val' }")
{'key': 'val'}
>>> ast.literal_eval("{ ('key',): 'val' }")
{('key',): 'val'}
>>> ast.literal_eval("{ frozenset({'key'}): 'val' }")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/ast.py", line 86, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python3.3/ast.py", line 63, in _convert
    in zip(node.keys, node.values))
  File "/usr/lib/python3.3/ast.py", line 62, in <genexpr>
    return dict((_convert(k), _convert(v)) for k, v
  File "/usr/lib/python3.3/ast.py", line 85, in _convert
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Call object at 0x7f865a8c1450>

It looks like we can special-case usage of set literal as a key for a dict or a member for another set and create a frozenset constant instead.

So that adict[{1, 2, 3}] should be interpreted as adict[frozenset([1, 2, 3])] and { {'b', 'a', 'r'}, 'foo' } as { frozenset('bar'), 'foo' }

This will provide a minimal change to the interpreter while making it possible to use any literal-parsing with frozensets while keeping method calls out of literal_eval.


Kind regards, Yuriy.