GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — argparse has problem parsing option files containing empty rows · Issue #54732 · python/cpython

argparse has problem parsing option files containing empty rows #54732

New issue
Copy link
New issue
Copy link
Closed
#150980
Closed
argparse has problem parsing option files containing empty rows#54732
#150980
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc direasystdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory

Description

MichalPomorski
mannequin
opened on Nov 24, 2010
Issue body actions
BPO 10523
Files
  • argparse_example.py
  • argparse_blanklines.patch
  • 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 = None
    created_at = <Date 2010-11-24.21:47:39.851>
    labels = ['type-bug', 'library']
    title = 'argparse has problem parsing option files containing empty rows'
    updated_at = <Date 2018-07-11.07:35:29.997>
    user = 'https://bugs.python.org/MichalPomorski'

    bugs.python.org fields:

    activity = <Date 2018-07-11.07:35:29.997>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2010-11-24.21:47:39.851>
    creator = 'Michal.Pomorski'
    dependencies = []
    files = ['34860', '34875']
    hgrepos = []
    issue_num = 10523
    keywords = ['patch']
    message_count = 4.0
    messages = ['122314', '128297', '216249', '216306']
    nosy_count = 4.0
    nosy_names = ['bethard', 'Michal.Pomorski', 'paul.j3', 'math_foo']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue10523'
    versions = ['Python 2.7']

    Linked PRs

    • gh-54732: Fix argparse arg file parsing #134115
    • gh-54732: Make argparse error caused by empty rows in option files explicit #136795
    • [3.14] gh-54732: Make argparse error caused by empty rows in option files explicit (GH-136795) #136818
    • [3.13] gh-54732: Make argparse error caused by empty rows in option files explicit (GH-136795) #136819
    • GH-54732: Tweak wording around empty lines in argument files #150980
    • [3.15] GH-54732: Tweak wording around empty lines in argument files (GH-150980) #151165
    • [3.14] GH-54732: Tweak wording around empty lines in argument files (GH-150980) #151166
    • [3.13] GH-54732: Tweak wording around empty lines in argument files (GH-150980) #151167

    Activity

    Next
    1. MichalPomorski commented on Nov 24, 2010

      @MichalPomorski
      MichalPomorskimannequin
      on Nov 24, 2010
      MannequinAuthor
      More actions

      When using the argument file option, i.e @file_with_arguments the following problems arise:

      1. argparse crashes when the file contains an empty line (even if it is the last line) - arg_string[0] is done when arg_string is empty.
        This is caused by the use of splitlines() instead of strip().split() in the function _read_args_from_files(self, arg_strings)

      2. options separated by spaces in one row are passed as a single long option, meaning each option has to be on its own line.
        This is caused by the new function

         def convert_arg_line_to_args(self, arg_line):
              return [arg_line]
         which should be 
              return arg_line.split()

      Both problems are caused by a modification in

          def _read_args_from_files(self, arg_strings)

      The version from argparse 1.0.1 worked well and was correct, it should be sufficient to reverse the changes done from 1.0.1 to 1.1.

      Here is the old implementation:

          def _read_args_from_files(self, arg_strings):
              # expand arguments referencing files
              new_arg_strings = []
              for arg_string in arg_strings:
      
                  # for regular arguments, just add them back into the list
                  if arg_string[0] not in self.fromfile_prefix_chars:
                      new_arg_strings.append(arg_string)
      
                  # replace arguments referencing files with the file content
                  else:
                      try:
                          args_file = open(arg_string[1:])
                          try:
                              arg_strings = args_file.read().strip().split()
                              arg_strings = self._read_args_from_files(arg_strings)
                              
                              new_arg_strings.extend(arg_strings)
                          finally:
                              args_file.close()
                      except IOError:
                          err = _sys.exc_info()[1]
                          self.error(str(err))
      
              # return the modified argument list
              return new_arg_strings
    2. MichalPomorski
      added
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
      on Nov 24, 2010
    3. bethard commented on Feb 10, 2011

      @bethard
      bethardmannequin
      on Feb 10, 2011
      Mannequin
      More actions

      Crashing on an empty line is definitely a bug.

      Each line being a single option is documented behavior:

      http://docs.python.org/dev/library/argparse.html#fromfile-prefix-chars

    4. mathfoo commented on Apr 14, 2014

      @mathfoo
      mathfoomannequin
      on Apr 14, 2014
      Mannequin
      More actions

      The current behaviour takes empty lines and interprets them as empty strings.

      The attached demonstration script shows the error occurring. The first case is a simple example to illustrate what happens in the general case. The second case shows empty lines being interpreted as empty strings and assigned to arguments.

      The third case, despite being very similar to the first, results in argparse exiting with an error message. Internally, what is happening is, after taking the 'foo' and 'baz' arguments and assigning them 'bar' and 'quux' respectively, it reads in an argument "", which it does not recognize. And produces the following error message:

      "argparse_example.py: error: unrecognized arguments:"

      The error message, in it's current form, is kind of opaque.

      For the third case, if we move the blank line to between 'bar' and '-baz', the same error results, as again it tried to interpret the blank line as an argument. If we move the blank line to the start of the file, same thing again.

      If we move the blank line between '-foo' and 'bar', instead the error reads: "argparse_example.py: error: unrecognized arguments: bar" - which is at least somewhat comprehensible.

      The question is, how should blank lines be handled?

      Should they be accepted as possible values for arguments?

      If they fall into spaces where arguments (versus values for arguments) are expected, should we skip them?

      If the current handling is fine, I would propose updating the documentation to add the following after the paragraph that begins "Arguments read from a file ...":

      "By default, blank lines are interpreted as empty strings. An empty string is not an acceptable argument; but it is an acceptable value for an argument."

      And changing the way that the error from argparse is displayed so that it is more obvious what "argparse_example.py: error: unrecognized arguments:" means.

    5. mathfoo commented on Apr 15, 2014

      @mathfoo
      mathfoomannequin
      on Apr 15, 2014
      Mannequin
      More actions

      I've attached a patch making the changes I suggested, assuming that the current behaviour is desirable. It documents the behaviour of argparse on files with blank lines and changes the way the error message that argparse generates when encountering unrecognized arguments is generated.

      When a blank line is included at the end of a file, the resulting error message is now: "argparse_example.py: error: unrecognized arguments: ''". This also makes it obvious when the problem is white space, e.g. if an argument has trailing spaces, this also makes that obvious.

    6. serhiy-storchaka
      added
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      and removed
      type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
      on Jul 11, 2018
    7. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    8. erlend-aasland
      added this to Argparse issueson May 19, 2022
    9. erlend-aasland
      moved this to Bugs in Argparse issueson May 19, 2022
    10. serhiy-storchaka
      added
      docsDocumentation in the Doc dirDocumentation in the Doc dir
      and removed
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Sep 26, 2024
    11. serhiy-storchaka
      added
      type-featureA feature request or enhancementA feature request or enhancement
      on Oct 23, 2024
    12. serhiy-storchaka commented on Jul 3, 2025

      @serhiy-storchaka
      serhiy-storchaka
      on Jul 3, 2025
      Member
      More actions

      This is an expected behavior. How would you pass an empty string as argument?

      We can make this more explicit in the documentation. This is very easy issue.

      Other problem -- reporting empty (or containing only whitespaces, or containing control codes) strings as invalid arguments. Please open a separate issue for this.

    13. 17 remaining items

    14. serhiy-storchaka commented on Jul 21, 2025

      @serhiy-storchaka
      serhiy-storchaka
      on Jul 21, 2025
      Member
      More actions

      You can just add that empty lines are interpreted as empty strings. This is redundant, but a little redundancy would not harm if this is not obvious for some users, and while it is little. I would add more, for example the convert_arg_line_to_args example needs a note that that simple example makes impossible to specify strings containing spaces (and an empty line is still interpreted as an empty string), or that the only way to pass an argument that starts with fromfile_prefix_chars is to read it from a file.

      If you revert #136795, I'll provide my variant. Or I can include the reversion in my PR.

    15. taegyunkim
      added a commit that references this issue on Aug 4, 2025

      pythongh-54732: Make argparse error caused by empty rows in option fi…

      6b01dd5
    16. Agent-Hellboy
      added a commit that references this issue on Aug 19, 2025

      pythongh-54732: Make argparse error caused by empty rows in option fi…

      78c1d70
    17. kumaraditya303
      added a commit that references this issue on Sep 9, 2025

      [3.14] pythongh-54732: Make argparse error caused by empty rows in op…

      d695c73
    18. savannahostrowski
      linked a pull request that will close this issueGH-54732: Tweak wording around empty lines in argument files #150980on Jun 5, 2026
    19. savannahostrowski
      added 2 commits that reference this issue on Jun 5, 2026

      Merge branch 'main' into pythongh-54732-fromfile-empty-line-doc

      08fe6b9

      Merge branch 'main' into pythongh-54732-fromfile-empty-line-doc

      0f80836
    20. savannahostrowski
      closed this as completedin #150980on Jun 9, 2026
    21. savannahostrowski
      added a commit that references this issue on Jun 9, 2026

      GH-54732: Tweak wording around empty lines in argument files (#150980)

      528550e
    22. github-project-automation
      moved this from Bugs to Doc issues in Argparse issueson Jun 9, 2026
    23. savannahostrowski
      added 3 commits that reference this issue on Jun 9, 2026

      [3.13] GH-54732: Tweak wording around empty lines in argument files (G…

      b032f3f

      [3.14] GH-54732: Tweak wording around empty lines in argument files (G…

      9d92876

      [3.15] GH-54732: Tweak wording around empty lines in argument files (G…

      804be0a
    24. clin1234
      added a commit that references this issue on Jun 9, 2026

      pythonGH-54732: Tweak wording around empty lines in argument files (p…

      1bfa4fd
    25. philthompson10
      added a commit that references this issue on Jun 17, 2026

      pythonGH-54732: Tweak wording around empty lines in argument files (p…

      bd81be7
    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

      docsDocumentation in the Doc dirDocumentation in the Doc direasystdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory

      Projects

      • Argparse issues
        Status
        Doc issues
        Show more project fields
      • docs issues
        Status
        Todo
        Show more project fields

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.