Skip to content

Commit 7e03f13

Browse files
committed
Make sure symlinks are considered in some functions
- resolve path for os.path.size and os.path.getmtime/getatime/getctime - see #210
1 parent 1924447 commit 7e03f13

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The release versions are PyPi releases.
1212
#### Infrastructure
1313

1414
#### Fixes
15+
* Incorrect handling of symlinks in `os.path.size` and other functions ([#210](../../issues/210)).
1516
* Failed to create directory with symlink as parent ([#215](../../issues/215)).
1617
* Incorrect error handling during directory creation ([#209](../../issues/209)).
1718
* Creating files in read-only directory was possible ([#203](../../issues/203)).

fake_filesystem_test.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,15 @@ def testGetsize(self):
632632
self.filesystem.CreateFile(file_path, contents='1234567')
633633
self.assertEqual(7, self.path.getsize('FOO/BAR/BAZ'))
634634

635+
def testGetsizeWithLoopingSymlink(self):
636+
self.filesystem.is_windows_fs = False
637+
dir_path = '/foo/bar'
638+
self.filesystem.CreateDirectory(dir_path)
639+
link_path = dir_path + "/link"
640+
link_target = link_path + "/link"
641+
self.os.symlink(link_target, link_path)
642+
self.assertRaisesOSError(errno.ELOOP, self.os.path.getsize, link_path)
643+
635644
def testGetMtime(self):
636645
test_file = self.filesystem.CreateFile('foo/bar1.txt')
637646
test_file.SetMTime(24)
@@ -1458,6 +1467,7 @@ def testMkdirRaisesIfParentIsReadOnly(self):
14581467
self.assertRaises(Exception, self.os.mkdir, directory)
14591468

14601469
def testMkdirWithWithSymlinkParent(self):
1470+
self.filesystem.is_windows_fs = False
14611471
dir_path = '/foo/bar'
14621472
self.filesystem.CreateDirectory(dir_path)
14631473
link_path = '/foo/link'
@@ -1494,9 +1504,9 @@ def testMakedirsRaisesIfParentIsBrokenLink(self):
14941504
def testMakedirsRaisesIfParentIsLoopingLink(self):
14951505
dir_path = '/foo/bar'
14961506
self.filesystem.CreateDirectory(dir_path)
1497-
link_target = dir_path + "/link"
1498-
link_path = link_target + "/link"
1499-
self.os.symlink(link_path, link_target)
1507+
link_path = dir_path + "/link"
1508+
link_target = link_path + "/link"
1509+
self.os.symlink(link_target, link_path)
15001510
self.assertRaisesOSError(errno.ELOOP, self.os.makedirs, link_path)
15011511

15021512
def testMakedirsRaisesIfAccessDenied(self):

pyfakefs/fake_filesystem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,7 +2644,7 @@ def getsize(self, path):
26442644
file size in bytes.
26452645
"""
26462646
try:
2647-
file_obj = self.filesystem.GetObject(path)
2647+
file_obj = self.filesystem.ResolveObject(path)
26482648
return file_obj.st_size
26492649
except IOError as exc:
26502650
raise os.error(exc.errno, exc.strerror)
@@ -2698,7 +2698,7 @@ def getmtime(self, path):
26982698
OSError: if the file does not exist.
26992699
"""
27002700
try:
2701-
file_obj = self.filesystem.GetObject(path)
2701+
file_obj = self.filesystem.ResolveObject(path)
27022702
except IOError as exc:
27032703
raise OSError(errno.ENOENT, str(exc))
27042704
return file_obj.st_mtime
@@ -2718,7 +2718,7 @@ def getatime(self, path):
27182718
OSError: if the file does not exist.
27192719
"""
27202720
try:
2721-
file_obj = self.filesystem.GetObject(path)
2721+
file_obj = self.filesystem.ResolveObject(path)
27222722
except IOError as exc:
27232723
raise OSError(errno.ENOENT, str(exc))
27242724
return file_obj.st_atime
@@ -2736,7 +2736,7 @@ def getctime(self, path):
27362736
OSError: if the file does not exist.
27372737
"""
27382738
try:
2739-
file_obj = self.filesystem.GetObject(path)
2739+
file_obj = self.filesystem.ResolveObject(path)
27402740
except IOError as exc:
27412741
raise OSError(errno.ENOENT, str(exc))
27422742
return file_obj.st_ctime

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist=py26,py27,py33,py34,py35,py36
2+
envlist=py26,py27,py33,py34,py35,py36,py37,pypy
33

44
[testenv]
55
deps = -rrequirements.txt

0 commit comments

Comments
 (0)