Skip to content

fileinput inplace does not work with pathlib.Path #75462

Closed

Description

@zmwangx
mannequin
BPO 31281
Nosy @ericvsmith, @zmwangx
PRs
  • bpo-31281: Fix pathlib.Path incompatibility in fileinput #3208
  • 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 = 'https://github.com/ericvsmith'
    closed_at = <Date 2017-09-04.18:15:28.145>
    created_at = <Date 2017-08-26.01:50:52.589>
    labels = ['3.7', 'type-bug', 'library']
    title = 'fileinput inplace does not work with pathlib.Path'
    updated_at = <Date 2017-09-04.18:15:28.143>
    user = 'https://github.com/zmwangx'

    bugs.python.org fields:

    activity = <Date 2017-09-04.18:15:28.143>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2017-09-04.18:15:28.145>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2017-08-26.01:50:52.589>
    creator = 'zmwangx'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31281
    keywords = []
    message_count = 3.0
    messages = ['300860', '301231', '301237']
    nosy_count = 2.0
    nosy_names = ['eric.smith', 'zmwangx']
    pr_nums = ['3208']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31281'
    versions = ['Python 3.6', 'Python 3.7']

    Activity

    1. zmwangx commented on Aug 26, 2017

      zmwangxmannequin
      MannequinAuthor

      Consider

          import fileinput
          import pathlib
          with fileinput.input(files=(pathlib.Path('in.txt'),), inplace=True) as fp:
              for line in fp:
                  print(line, end='')

      which results in

          Traceback (most recent call last):
            File "./pathlib-fileinput.py", line 6, in <module>
              for line in fp:
            File "/Users/zmwang/.pyenv/versions/3.6.1/lib/python3.6/fileinput.py", line 250, in __next__
              line = self._readline()
            File "/Users/zmwang/.pyenv/versions/3.6.1/lib/python3.6/fileinput.py", line 331, in _readline
              self._filename + (self._backup or ".bak"))
          TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

      A trivial fix is converting the specified filename to str when assigning to self._filename:

      -        self._filename = self._files[0]
      +        self._filename = str(self._files[0])
      
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Aug 26, 2017
    3. ericvsmith commented on Sep 4, 2017

      @ericvsmith
      Member

      New changeset 06de1ae by ericvsmith (Zhiming Wang) in branch 'master':
      bpo-31281: Fix pathlib.Path incompatibility in fileinput (gh-3208)
      06de1ae

    4. ericvsmith commented on Sep 4, 2017

      @ericvsmith
      Member

      I did not backport this to 3.6, because it depends on other changes that themselves have not been backported.

    5. self-assigned this
      on Sep 4, 2017
    6. 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

    Labels

    3.7 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions