Skip to content

ast.literal_eval doesn't give information about node except the type of it #82577

Closed

Description

@isidentical
BPO 38396
Nosy @serhiy-storchaka, @ilevkivskyi, @isidentical
PRs
  • bpo-38396: dump(node) instead of repr(node) on ast.literal_eval #16620
  • Superseder
  • bpo-32888: Improve exception message in ast.literal_eval
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-10-07.19:47:55.659>
    created_at = <Date 2019-10-07.15:55:56.849>
    labels = ['library', '3.9']
    title = "ast.literal_eval doesn't give information about node except the type of it"
    updated_at = <Date 2019-10-07.19:47:55.658>
    user = 'https://github.com/isidentical'

    bugs.python.org fields:

    activity = <Date 2019-10-07.19:47:55.658>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-07.19:47:55.659>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2019-10-07.15:55:56.849>
    creator = 'BTaskaya'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38396
    keywords = ['patch']
    message_count = 3.0
    messages = ['354103', '354132', '354133']
    nosy_count = 3.0
    nosy_names = ['serhiy.storchaka', 'levkivskyi', 'BTaskaya']
    pr_nums = ['16620']
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '32888'
    type = None
    url = 'https://bugs.python.org/issue38396'
    versions = ['Python 3.9']

    Activity

    1. isidentical commented on Oct 7, 2019

      @isidentical
      SponsorMemberAuthor
      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())
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Oct 7, 2019
    3. ilevkivskyi commented on Oct 7, 2019

      @ilevkivskyi
      Member

      The downside however is that exception messages can become very long. So I am not sure we should change this.

    4. serhiy-storchaka commented on Oct 7, 2019

      @serhiy-storchaka
      Member

      I have same doubts.

      See also bpo-32888.

    5. transferred this issue fromon Apr 10, 2022
    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

      3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directory

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions