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

Support pathlib2 #408

Closed
sk- opened this Issue May 30, 2018 · 7 comments

Comments

Projects
None yet
3 participants
@sk-

sk- commented May 30, 2018

First of all thanks for this project, it's really useful. However, it'd be great if there was support for pathlib2.

In my code I'm importing it as import pathlib2 as pathlib. Which gets patched by pyfakefs, however the patch uses the system pathlib, which means that in my case it only works for Python 3.5 and 3.6, as I'm using the exist_ok argument of mkdir. In Python 3.4 I get the following error:

TypeError: mkdir() got an unexpected keyword argument 'exist_ok' 

And in Python 2.7 the module is not patched at all.

I already managed to get this to work in Python 2.7 (and probably in 3.4),

In my test I had to add

def setUp():
  from pyfakefs import fake_pathlib
  self.modules_to_patch = {'pathlib': fake_pathlib.FakePathlibModule}
  self.setUpPyfakefs()

and I had to change fake_pathlib.py as following

try:
  import pathlib2 as pathlib
except ImportError:
  import pathlib
try:
  from urllib.parse import quote_from_bytes as urlquote_from_bytes
except ImportError:
  from urllib import quote as urlquote_from_bytes

Let me know if you would be interested in accepting this change, so I could prepare a patch.

@mrbean-bremen

This comment has been minimized.

Show comment
Hide comment
@mrbean-bremen

mrbean-bremen May 30, 2018

Collaborator

Sure, that sounds helpful. I actually wasn't aware of pathlib2, but we did something similar with the scandir module which is a backport of scandir to Python < 3.5.
And nice to know that modules_to_patch has been used by someone :)

Collaborator

mrbean-bremen commented May 30, 2018

Sure, that sounds helpful. I actually wasn't aware of pathlib2, but we did something similar with the scandir module which is a backport of scandir to Python < 3.5.
And nice to know that modules_to_patch has been used by someone :)

@matthew16550

This comment has been minimized.

Show comment
Hide comment
@matthew16550

matthew16550 Aug 1, 2018

Contributor

There is another difficulty using pathlib2 with Python 2.7, the version checking in FakePath.resolve() prevents using strict=False.

e.g.

def test_resolve(fs):
    Path('foo').resolve(False)

Causes:
TypeError: resolve() got an unexpected keyword argument 'strict'

Contributor

matthew16550 commented Aug 1, 2018

There is another difficulty using pathlib2 with Python 2.7, the version checking in FakePath.resolve() prevents using strict=False.

e.g.

def test_resolve(fs):
    Path('foo').resolve(False)

Causes:
TypeError: resolve() got an unexpected keyword argument 'strict'

@mrbean-bremen

This comment has been minimized.

Show comment
Hide comment
@mrbean-bremen

mrbean-bremen Aug 1, 2018

Collaborator

Yes, the version checking has been made for pathlib only - the checks have to be changed for pathlib2 support.
@sk- - are you still working on the PR? Do you need help?

Collaborator

mrbean-bremen commented Aug 1, 2018

Yes, the version checking has been made for pathlib only - the checks have to be changed for pathlib2 support.
@sk- - are you still working on the PR? Do you need help?

@sk-

This comment has been minimized.

Show comment
Hide comment
@sk-

sk- Aug 2, 2018

@mrbean-bremen sorry I don't have the time to prepare the PR. Feel free to use any of the code snippets if they are of any help.

sk- commented Aug 2, 2018

@mrbean-bremen sorry I don't have the time to prepare the PR. Feel free to use any of the code snippets if they are of any help.

@matthew16550

This comment has been minimized.

Show comment
Hide comment
@matthew16550

matthew16550 Aug 2, 2018

Contributor

@mrbean-bremen I probably have time this weekend if you would like me to make a PR?

Contributor

matthew16550 commented Aug 2, 2018

@mrbean-bremen I probably have time this weekend if you would like me to make a PR?

@mrbean-bremen

This comment has been minimized.

Show comment
Hide comment
@mrbean-bremen

mrbean-bremen Aug 2, 2018

Collaborator

@sk- no problem, thanks!
@matthew16550 - thanks for the offer - sure, we always welcome contributions! If you've got the time, please go ahead!

Collaborator

mrbean-bremen commented Aug 2, 2018

@sk- no problem, thanks!
@matthew16550 - thanks for the offer - sure, we always welcome contributions! If you've got the time, please go ahead!

matthew16550 pushed a commit to matthew16550/pyfakefs that referenced this issue Aug 7, 2018

jmcgeheeiv added a commit that referenced this issue Sep 3, 2018

@mrbean-bremen

This comment has been minimized.

Show comment
Hide comment
@mrbean-bremen

mrbean-bremen Sep 3, 2018

Collaborator

Implemented in #422.
Thanks for your contribution and your patience!

Collaborator

mrbean-bremen commented Sep 3, 2018

Implemented in #422.
Thanks for your contribution and your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment