Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-31281: Fix pathlib.Path incompatibility in fileinput#3208

Merged
ericvsmith merged 1 commit into
python:masterpython/cpython:masterfrom
zmwangx:fileinput-pathlib-inplacezmwangx/cpython:fileinput-pathlib-inplaceCopy head branch name to clipboard
Sep 4, 2017
Merged

bpo-31281: Fix pathlib.Path incompatibility in fileinput#3208
ericvsmith merged 1 commit into
python:masterpython/cpython:masterfrom
zmwangx:fileinput-pathlib-inplacezmwangx/cpython:fileinput-pathlib-inplaceCopy head branch name to clipboard

Conversation

@zmwangx

@zmwangx zmwangx commented Aug 26, 2017

Copy link
Copy Markdown
Contributor

fileinput otherwise works fine with pathlib.Path filenames, but when inplace is set to True, the Path object needs to be converted to a str first, or appending a str suffix with + would fail:

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

=>

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'

https://bugs.python.org/issue31281

Comment thread Lib/fileinput.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.fspath is preferred over str().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericvsmith Updated.

@ericvsmith

Copy link
Copy Markdown
Member

This also needs tests. I don't know what the existing tests look like.

fileinput otherwise works fine with pathlib.Path filenames, but when
inplace is set to True, the Path object needs to be converted to a str
first, or appending a str suffix with + would fail.
@zmwangx zmwangx force-pushed the fileinput-pathlib-inplace branch from 10792d1 to 590e228 Compare August 28, 2017 23:15
@zmwangx

zmwangx commented Aug 28, 2017

Copy link
Copy Markdown
Contributor Author

@ericvsmith I added a test.

@ericvsmith ericvsmith merged commit 06de1ae into python:master Sep 4, 2017
jimmylai pushed a commit to jimmylai/cpython that referenced this pull request Sep 4, 2017
* 'master' of https://github.com/python/cpython: (601 commits)
  remove check for bug last seem in Solaris 9 (python#3285)
  Change code owners for hashlib and ssl to the crypto team (python#3284)
  bpo-31281: Fix pathlib.Path incompatibility in fileinput (pythongh-3208)
  remove autoconf check for select() (python#3283)
  remove configure check for 'volatile' (python#3281)
  Add missing _sha3 module to Setup.dist (python#2395)
  bpo-12383: Also ignore __PYVENV_LAUNCHER__ (python#3278)
  bpo-9146: add the missing NEWS entry. (python#3275)
  Fix a c.f.as_completed() refleak previously introduced in bpo-27144 (python#3270)
  bpo-31185: Fixed miscellaneous errors in asyncio speedup module. (python#3076)
  remove a redundant lower in urllib.parse.urlsplit (python#3008)
  bpo-31323: Fix reference leak in test_ssl (python#3263)
  bpo-31250, test_asyncio: fix EventLoopTestsMixin.tearDown() (python#3264)
  bpo-31326: ProcessPoolExecutor waits for the call queue thread (python#3265)
  bpo-27144: concurrent.futures as_complete and map iterators do not keep reference to returned object (python#1560)
  bpo-31250, test_asyncio: fix dangling threads (python#3252)
  bpo-31217: Fix regrtest -R for small integer (python#3260)
  bpo-30096: Use ABC in abc reference examples (python#1220)
  bpo-30737: Update DevGuide links to new URL (pythonGH-3228)
  [Trivial] Remove now redundant assert (python#3245)
  ...
@zmwangx zmwangx deleted the fileinput-pathlib-inplace branch September 5, 2017 00:33
GadgetSteve pushed a commit to GadgetSteve/cpython that referenced this pull request Sep 10, 2017
Fix fileinput with inplace=True to accept pathlib.Path objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.