Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileinput inplace does not work with pathlib.Path #75462

Closed
zmwangx mannequin opened this issue Aug 26, 2017 · 3 comments
Closed

fileinput inplace does not work with pathlib.Path #75462

zmwangx mannequin opened this issue Aug 26, 2017 · 3 comments
Assignees
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@zmwangx
Copy link
Mannequin

zmwangx mannequin commented Aug 26, 2017

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']

    @zmwangx
    Copy link
    Mannequin Author

    zmwangx mannequin commented Aug 26, 2017

    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])
    

    @zmwangx zmwangx mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 26, 2017
    @ericvsmith
    Copy link
    Member

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

    @ericvsmith
    Copy link
    Member

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

    @ericvsmith ericvsmith self-assigned this Sep 4, 2017
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant