Skip to content

Commit

Permalink
Propagate exception for non-existing path in os.scandir()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Nov 1, 2019
1 parent 947e83f commit 4f237ea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ the proposed changes so you can be ready.

## Version 3.7 (as yet unreleased)

### Fixes
* raise for `os.scandir` with non-existing directory
(see [#498](../../issues/498))

## [Version 3.6.1](https://pypi.python.org/pypi/pyfakefs/3.6.1)

### Fixes
Expand Down
12 changes: 9 additions & 3 deletions pyfakefs/fake_scandir.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
and the standalone function available in the standalone `scandir` python
package.
"""
import errno
import os
import sys

from pyfakefs.extra_packages import use_scandir_package, use_builtin_scandir
from pyfakefs.helpers import IS_PY2

if sys.version_info >= (3, 6) and use_builtin_scandir:
BaseClass = os.PathLike
Expand Down Expand Up @@ -132,11 +134,15 @@ def __init__(self, filesystem, path):
else:
self.abspath = self.filesystem.absnormpath(path)
self.path = path
contents = {}
try:
contents = self.filesystem.confirmdir(self.abspath).contents
except OSError:
pass
except OSError as ex:
if IS_PY2 and self.filesystem.is_windows_fs:
if ex.errno == errno.ENOENT:
# for some reason, under Python 2 / Windows
# raises "No such process" for non-existing path
raise OSError(errno.ESRCH, str(ex), ex.filename)
raise
self.contents_iter = iter(contents)

def __iter__(self):
Expand Down
16 changes: 16 additions & 0 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import time
import unittest

from pyfakefs.helpers import IS_PY2

from pyfakefs import fake_filesystem
from pyfakefs.fake_filesystem import FakeFileOpen, is_root
from pyfakefs.extra_packages import (
Expand Down Expand Up @@ -111,6 +113,8 @@ def test_get_cwd(self):
self.assertEqual(self.os.getcwd(), dirname)

def test_listdir(self):
self.assert_raises_os_error(
errno.ENOENT, self.os.listdir, 'non_existing/fake_dir')
directory = self.make_path('xyzzy', 'plugh')
files = ['foo', 'bar', 'baz']
for f in files:
Expand Down Expand Up @@ -4941,6 +4945,18 @@ def test_path_like(self):
self.assertEqual(self.os.path.join(self.scandir_path(), 'file'),
os.fspath(self.dir_entries[1]))

@unittest.skipIf(IS_PY2 and TestCase.is_windows,
'Exception subtype differs')
def test_non_existing_dir(self):
self.assert_raises_os_error(
errno.ENOENT, self.scandir, 'non_existing/fake_dir')

@unittest.skipIf(not IS_PY2 or not TestCase.is_windows,
'Exception subtype differs')
def test_non_existing_dir(self):
self.assert_raises_os_error(
errno.ESRCH, self.scandir, 'non_existing/fake_dir')


class RealScandirTest(FakeScandirTest):
def use_real_fs(self):
Expand Down

0 comments on commit 4f237ea

Please sign in to comment.