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

Accept Pathlike objects (eg. pathlib.Path) in Py3.6+ #990

Merged
merged 1 commit into from
Mar 24, 2020

Conversation

rcoup
Copy link
Contributor

@rcoup rcoup commented Mar 18, 2020

PEP-519 (Py3.6+ / PyPy 7.3+) adds a [file system path protocol[(https://www.python.org/dev/peps/pep-0519/) via Object.__fspath__(), and is supported by pathlib/os/open()/etc. This PR adds support for accepting pathlib.Path objects natively in Pygit2.

  • Where we take paths in Python, check hasattr(value, '__fspath__') to provide Py3.5 compatibility, then call it to get a str/bytes representation.
  • In C, use PyUnicode_FSConverter when parsing function arguments (it handles Pathlike objects), or PyOS_FSPath() when available (ie. not Py3.5).
  • Implementation-wise, this approach is basically called for all string arguments, but since os.fspath()/PyOS_FSPath() return a str/bytes argument unchanged, behaviour-wise it's the same. Cleaning this up might sit with Consistent interface to get text and bytes #895?

Adds a pile of *_aspath() tests under Py3.6+ to wherever we seem to accept paths. I'm sure I've missed some but we can sort that along the way.

@jdavid
Copy link
Member

jdavid commented Mar 21, 2020

AppVeyor is failing with a C syntax error. Travis is failing as well, but now it's under maintenance and cannot see the details.

I think this is the right time to drop support for Python 3.5 and simplify the PR a bit.

@jdavid jdavid mentioned this pull request Mar 21, 2020
@jdavid
Copy link
Member

jdavid commented Mar 24, 2020

So I dropped 3.5 support. Could you please rebase and update the PR? Thanks.

@rcoup
Copy link
Contributor Author

rcoup commented Mar 24, 2020

yup, will do

PEP-519 (Py3.6+ / PyPy 7.3+) adds a file system path protocol via Object.__fspath__(), and is supported by pathlib/os/open/etc. https://www.python.org/dev/peps/pep-0519/

Wherever we take paths in Python, check for value.__fspath__ to provide compatibility, then call it to get a str/bytes representation. In C, use PyUnicode_FSConverter when parsing function arguments, and PyOS_FSPath() when available.

Adds *_aspath() tests to wherever we seem to accept paths.
@rcoup
Copy link
Contributor Author

rcoup commented Mar 24, 2020

Everything was still needed for CI's version of PyPy, so I left it in for the moment to check tests.

@jdavid
Copy link
Member

jdavid commented Mar 24, 2020

Ok.. I see this would require pypy 7.3.0 while in Travis the latest version is 7.1.1

@jdavid jdavid merged commit 4737aff into libgit2:master Mar 24, 2020
@jdavid jdavid mentioned this pull request Mar 24, 2020
@rcoup rcoup deleted the accept-pathlike branch March 24, 2020 13:31
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.

None yet

2 participants