Skip to content

Error dumping Python source with assignment where value contains format directive #18

Description

@luser

This source:

import ast, meta
m = ast.parse("x = '{y}'")
meta.dump_python_source(m)

Produces this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/pysourcegen.py", line 873, in dump_python_source
    gen.visit(ast)
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/__init__.py", line 44, in visit
    return mehtod(node, *args, **kwargs)
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/pysourcegen.py", line 526, in visitModule
    self.visit(node)
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/__init__.py", line 44, in visit
    return mehtod(node, *args, **kwargs)
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/pysourcegen.py", line 578, in visitAssign
    self.print('{targets} = {value:node}\n', targets=' = '.join(targets), value=node.value)
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/pysourcegen.py", line 83, in print
    line = self.formatter.format(line, *args, **kwargs)
  File "/usr/lib/python2.7/string.py", line 545, in format
    return self.vformat(format_string, args, kwargs)
  File "/usr/lib/python2.7/string.py", line 549, in vformat
    result = self._vformat(format_string, args, kwargs, used_args, 2)
  File "/usr/lib/python2.7/string.py", line 582, in _vformat
    result.append(self.format_field(obj, format_spec))
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/pysourcegen.py", line 27, in format_field
    gen.visit(value)
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/__init__.py", line 44, in visit
    return mehtod(node, *args, **kwargs)
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/pysourcegen.py", line 229, in visitStr
    self.print(repr(node.s))
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/pysourcegen.py", line 83, in print
    line = self.formatter.format(line, *args, **kwargs)
  File "/usr/lib/python2.7/string.py", line 545, in format
    return self.vformat(format_string, args, kwargs)
  File "/usr/lib/python2.7/string.py", line 549, in vformat
    result = self._vformat(format_string, args, kwargs, used_args, 2)
  File "/usr/lib/python2.7/string.py", line 571, in _vformat
    obj, arg_used = self.get_field(field_name, args, kwargs)
  File "/usr/lib/python2.7/string.py", line 632, in get_field
    obj = self.get_value(first, args, kwargs)
  File "/build/autoconftopython/venv/local/lib/python2.7/site-packages/meta-development-py2.7.egg/meta/asttools/visitors/pysourcegen.py", line 42, in get_value
    key = int(key)
ValueError: invalid literal for int() with base 10: 'y'

This is using current git master 32eccb4

A simpler snippet works:

m = ast.parse("'{y}'")
meta.dump_python_source(m)

produces:

"\n'''{y}\n'''\n\n\n\n\n"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions