classification
Title: ast.literal_eval doesn't give information about node except the type of it
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Improve exception message in ast.literal_eval
View: 32888
Assigned To: Nosy List: BTaskaya, levkivskyi, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-10-07 15:55 by BTaskaya, last changed 2019-10-07 19:47 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16620 closed BTaskaya, 2019-10-07 16:02
Messages (3)
msg354103 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-10-07 15:55
def _convert_num(node):
        if isinstance(node, Constant):
            if type(node.value) in (int, float, complex):
                return node.value
>       raise ValueError('malformed node or string: ' + repr(node))
E       ValueError: malformed node or string: <_ast.Name object at 0x7f0e7ebab320>

When a malformed node passes into literal_eval, it raises ValueError with repr of node (which is just a memory address and type). I think using dump(node) at there is a better option, like

node = <_ast.Name object at 0x7fa8279847d0>

    def _convert_num(node):
        if isinstance(node, Constant):
            if type(node.value) in (int, float, complex):
                return node.value
>       raise ValueError('malformed node or string: ' + dump(node))
E       ValueError: malformed node or string: Name(id='a', ctx=Load())
msg354132 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-10-07 19:34
The downside however is that exception messages can become very long. So I am not sure we should change this.
msg354133 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-07 19:47
I have same doubts.

See also issue32888.
History
Date User Action Args
2019-10-07 19:47:55serhiy.storchakasetstatus: open -> closed
superseder: Improve exception message in ast.literal_eval
messages: + msg354133

resolution: duplicate
stage: patch review -> resolved
2019-10-07 19:34:50levkivskyisetnosy: + serhiy.storchaka
messages: + msg354132
2019-10-07 16:03:10xtreaksetnosy: + levkivskyi
2019-10-07 16:02:35BTaskayasetkeywords: + patch
stage: patch review
pull_requests: + pull_request16208
2019-10-07 15:55:56BTaskayacreate