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

Pyfakefs doesn't work with Python 2.6+ backport of os.scandir #332

Closed
OskarPersson opened this issue Dec 7, 2017 · 10 comments · Fixed by #333
Closed

Pyfakefs doesn't work with Python 2.6+ backport of os.scandir #332

OskarPersson opened this issue Dec 7, 2017 · 10 comments · Fixed by #333
Assignees

Comments

@OskarPersson
Copy link

pyfakefs doesn't work with the backport of os.scandir Is this possible or does it have the same limitations as lxml?

Currently the backport, just like in #119, doesn't find any directories created by pyfakefs and raises an OSError:

OSError: [Errno 2] No such file or directory: 'root'
@mrbean-bremen
Copy link
Member

Yes, unfortunately it has the same limitations as lxml. The backport is a C based module, and thus cannot be faked without explicit support in the library. Currently, we have no support for any non-standard libraries, and I'm not sure if that would be in the scope of this package. @jmcgeheeiv - what do you think?

@jmcgeheeiv
Copy link
Contributor

jmcgeheeiv commented Dec 7, 2017 via email

@mrbean-bremen
Copy link
Member

Well, we could fake it the same way we fake os, I'm just not sure if this is the way to go. We already have a fake implementation of os.scandir, so that could be used in this case, e.g. we would have to fake out the scandir module in case it is available.

@mrbean-bremen
Copy link
Member

I have thought about this a little more, and I think that we can refactor Patcher a little to be able to add other faked modules without the need to directly reference them in the patcher itself.
I may have a go at this after the pep8 stuff is merged.

@mrbean-bremen
Copy link
Member

@OskarPersson - please check if this works for you and reopen otherwise.
Just learned that mentioning the issue in the PR description (not in the commit message) also auto-closes it...

@OskarPersson
Copy link
Author

OskarPersson commented Dec 13, 2017

Hmm, no I can't get it to work :/

I cloned the master branch, created a new Python 2.7.14 virtualenv, pip installed scandir and tested with python all_tests.py. This resulted in 8 errors, all with the same traceback:

ERROR: testIndexAccessToStatTimesReturnsInt (fake_filesystem_test.FakeScandirTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/oskar/git/pyfakefs/fake_filesystem_test.py", line 4832, in setUp
    from fake_scandir import scandir
ImportError: No module named fake_scandir

I also tried setting up a simple test in example_test.py:


from scandir import scandir

def test_scandir_backport(self):
    self.fs.CreateFile('/test/text.txt')
    self.assertEqual(len(list(scandir('/test'))), 1)

This results in the following error:

ERROR: test_scandir_backport (example_test.TestExample)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/oskar/git/pyfakefs/example_test.py", line 152, in test_scandir_backport
OSError: [Errno 2] No such file or directory: '/test'

@mrbean-bremen
Copy link
Member

Ok, I will check this tomorrow, if I find the time.

@mrbean-bremen mrbean-bremen reopened this Dec 13, 2017
mrbean-bremen added a commit that referenced this issue Dec 14, 2017
- adapted example test to work with scandir module
- fixed scandir test to work with scandir module
- see #332
@mrbean-bremen
Copy link
Member

Ok, several things here:

  • the from scandir import scandir will generally not work under pyfakefs, you have to import scandir (there is the possibility to enable the dynamic patcher to fix this, but this is experimental and breaks at least pytest)
  • the example will not work that way - you have to add the tested code to the "sut", which is example.py; I adapted the existing example to also work with the scandir module
  • there was a syntax error in the other test, which I did not catch, because I tested under Windows, where the test was disabled (I overlooked this)
  • there was a real bug in the implementation - it did not work under Python 2 due to changed syntax for next in the iterator (__next__ vs next)

@OskarPersson
Copy link
Author

From my tests this seems to work now, thanks!

@mrbean-bremen
Copy link
Member

Glad to hear that!

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

Successfully merging a pull request may close this issue.

3 participants