This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ast.literal_eval() fails on docstrings
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Maxime Chambreuil, SilentGhost, benjamin.peterson, georg.brandl, r.david.murray
Priority: normal Keywords:

Created on 2015-11-13 20:30 by Maxime Chambreuil, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
__openerp__.py Maxime Chambreuil, 2015-11-13 20:30
Messages (4)
msg254619 - (view) Author: Maxime Chambreuil (Maxime Chambreuil) Date: 2015-11-13 20:30
ast.literal_eval fails when there is docstrings in the file.

I used the file attached and this code in our CI server:

def is_installable_module(path):
    """return False if the path doesn't contain an installable odoo module,
    and the full path to the module manifest otherwise"""
    manifest_path = is_module(path)
    if manifest_path:
        manifest = ast.literal_eval(open(manifest_path).read())
        if manifest.get('installable', True):
            return manifest_path
    return False

I get the following error message:
Traceback (most recent call last):

  File "/home/travis/maintainer-quality-tools/travis/test_flake8", line 14, in <module>

    for addon in get_modules(os.path.abspath('.')):

  File "/home/travis/maintainer-quality-tools/travis/getaddons.py", line 53, in get_modules

    if is_installable_module(os.path.join(path, x))]

  File "/home/travis/maintainer-quality-tools/travis/getaddons.py", line 38, in is_installable_module

    manifest = ast.literal_eval(open(manifest_path).read())

  File "/usr/lib/python2.7/ast.py", line 49, in literal_eval

    node_or_string = parse(node_or_string, mode='eval')

  File "/usr/lib/python2.7/ast.py", line 37, in parse

    return compile(source, filename, mode, PyCF_ONLY_AST)

  File "<unknown>", line 22

    {

    ^

SyntaxError: invalid syntax


https://github.com/OCA/knowledge/pull/75
https://github.com/OCA/maintainer-quality-tools/blob/master/travis/getaddons.py#L33
https://travis-ci.org/OCA/knowledge/jobs/90332842
msg254620 - (view) Author: Maxime Chambreuil (Maxime Chambreuil) Date: 2015-11-13 20:37
If I remove the docstrings on the first line, ast.literal_eval() does not fail.
msg254621 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2015-11-13 20:41
It doesn't seem docstring-specific, docstring is a regular Python object and ast.literal_eval would fail if you have more than one literals of any kind. Put two integers in your file and the literal_eval will fail.
msg254622 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-11-13 20:50
Yes, literal_eval only evaluates a single literal, as is documented.
History
Date User Action Args
2022-04-11 14:58:23adminsetgithub: 69807
2015-11-13 20:50:24r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg254622

resolution: not a bug
stage: resolved
2015-11-13 20:41:38SilentGhostsetnosy: + georg.brandl, SilentGhost, benjamin.peterson
messages: + msg254621
2015-11-13 20:37:26Maxime Chambreuilsetmessages: + msg254620
2015-11-13 20:30:48Maxime Chambreuilcreate