changeset: 103878:eabb86463462 branch: 3.5 parent: 103875:0e2a891639dd user: Berker Peksag date: Sat Sep 17 15:49:59 2016 +0300 files: Lib/test/test_os.py Misc/NEWS Modules/posixmodule.c description: Issue #28075: Check for ERROR_ACCESS_DENIED in Windows implementation of os.stat() Patch by Eryk Sun. diff -r 0e2a891639dd -r eabb86463462 Lib/test/test_os.py --- a/Lib/test/test_os.py Sat Sep 17 03:26:16 2016 +0000 +++ b/Lib/test/test_os.py Sat Sep 17 15:49:59 2016 +0300 @@ -432,6 +432,25 @@ result.st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY, stat.FILE_ATTRIBUTE_DIRECTORY) + @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") + def test_access_denied(self): + # Default to FindFirstFile WIN32_FIND_DATA when access is + # denied. See issue 28075. + # os.environ['TEMP'] should be located on a volume that + # supports file ACLs. + fname = os.path.join(os.environ['TEMP'], self.fname) + self.addCleanup(support.unlink, fname) + create_file(fname, b'ABC') + # Deny the right to [S]YNCHRONIZE on the file to + # force CreateFile to fail with ERROR_ACCESS_DENIED. + DETACHED_PROCESS = 8 + subprocess.check_call( + ['icacls.exe', fname, '/deny', 'Users:(S)'], + creationflags=DETACHED_PROCESS + ) + result = os.stat(fname) + self.assertNotEqual(result.st_size, 0) + class UtimeTests(unittest.TestCase): def setUp(self): diff -r 0e2a891639dd -r eabb86463462 Misc/NEWS --- a/Misc/NEWS Sat Sep 17 03:26:16 2016 +0000 +++ b/Misc/NEWS Sat Sep 17 15:49:59 2016 +0300 @@ -71,6 +71,9 @@ Library ------- +- Issue #28075: Check for ERROR_ACCESS_DENIED in Windows implementation of + os.stat(). Patch by Eryk Sun. + - Issue #25270: Prevent codecs.escape_encode() from raising SystemError when an empty bytestring is passed. diff -r 0e2a891639dd -r eabb86463462 Modules/posixmodule.c --- a/Modules/posixmodule.c Sat Sep 17 03:26:16 2016 +0000 +++ b/Modules/posixmodule.c Sat Sep 17 15:49:59 2016 +0300 @@ -1515,7 +1515,9 @@ /* Either the target doesn't exist, or we don't have access to get a handle to it. If the former, we need to return an error. If the latter, we can use attributes_from_dir. */ - if (GetLastError() != ERROR_SHARING_VIOLATION) + DWORD lastError = GetLastError(); + if (lastError != ERROR_ACCESS_DENIED && + lastError != ERROR_SHARING_VIOLATION) return -1; /* Could not get attributes on open file. Fall back to reading the directory. */ @@ -1525,7 +1527,7 @@ if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { if (traverse) { /* Should traverse, but could not open reparse point handle */ - SetLastError(ERROR_SHARING_VIOLATION); + SetLastError(lastError); return -1; } }