]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-109033: Return filename with os.utime errors (#109034)
authorRonan Pigott <ronan@rjp.ie>
Tue, 19 Sep 2023 23:18:23 +0000 (16:18 -0700)
committerGitHub <noreply@github.com>
Tue, 19 Sep 2023 23:18:23 +0000 (01:18 +0200)
The filename was previously intentionally omitted from exception because
"it might confuse the user". Uncaught exceptions are not generally a
replacement for user-facing error messages, so obscuring this
information only has the effect of making the programmer's life more
difficult.

Lib/test/test_os.py
Misc/NEWS.d/next/Library/2023-09-06-14-47-28.gh-issue-109033.piUzDx.rst [new file with mode: 0644]
Modules/posixmodule.c

index 34cd27b143f231f17f6d2a45ae9935a48ccc9f3f..66aece2c4b3eb993ffa7a7652f98f60a3da01680 100644 (file)
@@ -913,6 +913,13 @@ class UtimeTests(unittest.TestCase):
             os.utime(self.fname, None)
         self._test_utime_current(set_time)
 
+    def test_utime_nonexistent(self):
+        now = time.time()
+        filename = 'nonexistent'
+        with self.assertRaises(FileNotFoundError) as cm:
+            os.utime(filename, (now, now))
+        self.assertEqual(cm.exception.filename, filename)
+
     def get_file_system(self, path):
         if sys.platform == 'win32':
             root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'
diff --git a/Misc/NEWS.d/next/Library/2023-09-06-14-47-28.gh-issue-109033.piUzDx.rst b/Misc/NEWS.d/next/Library/2023-09-06-14-47-28.gh-issue-109033.piUzDx.rst
new file mode 100644 (file)
index 0000000..15ec0b4
--- /dev/null
@@ -0,0 +1,2 @@
+Exceptions raised by os.utime builtin function now include the related
+filename
index c4340397fbe577cc9a0a9610f189f973f102f070..2c89a68fa57f2e6003e411e620aa503742cd2422 100644 (file)
@@ -6307,11 +6307,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
         _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
     }
     if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
-        /* Avoid putting the file name into the error here,
-           as that may confuse the user into believing that
-           something is wrong with the file, when it also
-           could be the time stamp that gives a problem. */
-        PyErr_SetFromWindowsErr(0);
+        path_error(path);
         CloseHandle(hFile);
         return NULL;
     }
@@ -6351,8 +6347,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
 #endif
 
     if (result < 0) {
-        /* see previous comment about not putting filename in error here */
-        posix_error();
+        path_error(path);
         return NULL;
     }