From c51b0352441cdf7683f286009387b9df84adcdd5 Mon Sep 17 00:00:00 2001 From: Diego Argueta Date: Thu, 29 Jul 2021 10:39:52 -0700 Subject: [PATCH] Fix #484 --- CHANGELOG.md | 1 + fs/error_tools.py | 3 ++- tests/test_error_tools.py | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11d8330d..a23dc9b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed performance bugs in `fs.copy.copy_dir_if_newer`. Test cases were adapted to catch those bugs in the future. - Fixed precision bug for timestamps in `fs.OSFS.setinfo`. +- Fixed `ResourceLocked` error translation on Windows [#484](https://github.com/PyFilesystem/pyfilesystem2/issues/484). ## [2.4.13] - 2021-03-27 diff --git a/fs/error_tools.py b/fs/error_tools.py index 66d38696..ba516eeb 100644 --- a/fs/error_tools.py +++ b/fs/error_tools.py @@ -84,7 +84,8 @@ def __exit__( _errno = exc_value.errno fserror = os_errors.get(_errno, errors.OperationFailed) if _errno == errno.EACCES and sys.platform == "win32": - if getattr(exc_value, "args", None) == 32: # pragma: no cover + error_args = getattr(exc_value, "args", (None,)) + if error_args and error_args[0] == 32: # pragma: no cover fserror = errors.ResourceLocked reraise(fserror, fserror(self._path, exc=exc_value), traceback) diff --git a/tests/test_error_tools.py b/tests/test_error_tools.py index 4f6aa324..02def95f 100644 --- a/tests/test_error_tools.py +++ b/tests/test_error_tools.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals import errno +import sys import unittest import fs.errors @@ -23,3 +24,13 @@ def test_convert_enametoolong(self): raise exception self.assertEqual(ctx.exception.exc, exception) self.assertEqual(ctx.exception.path, "/tmp/test") + + @unittest.skipIf(sys.platform != "win32", "requires Windows") + def test_convert_resourcelocked_windows(self): + exception = OSError(32, "resource locked") + with self.assertRaises(fs.errors.ResourceLocked) as ctx: + with convert_os_errors("stat", "/tmp/test"): + raise exception + + self.assertEqual(ctx.exception.exc, exception) + self.assertEqual(ctx.exception.path, "/tmp/test")