Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ddf2e95

Browse files
authored
gh-109033: Return filename with os.utime errors (#109034)
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.
1 parent fd7e08a commit ddf2e95

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

Lib/test/test_os.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,13 @@ def set_time(filename):
913913
os.utime(self.fname, None)
914914
self._test_utime_current(set_time)
915915

916+
def test_utime_nonexistent(self):
917+
now = time.time()
918+
filename = 'nonexistent'
919+
with self.assertRaises(FileNotFoundError) as cm:
920+
os.utime(filename, (now, now))
921+
self.assertEqual(cm.exception.filename, filename)
922+
916923
def get_file_system(self, path):
917924
if sys.platform == 'win32':
918925
root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Exceptions raised by os.utime builtin function now include the related
2+
filename

Modules/posixmodule.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6307,11 +6307,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
63076307
_Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
63086308
}
63096309
if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
6310-
/* Avoid putting the file name into the error here,
6311-
as that may confuse the user into believing that
6312-
something is wrong with the file, when it also
6313-
could be the time stamp that gives a problem. */
6314-
PyErr_SetFromWindowsErr(0);
6310+
path_error(path);
63156311
CloseHandle(hFile);
63166312
return NULL;
63176313
}
@@ -6351,8 +6347,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
63516347
#endif
63526348

63536349
if (result < 0) {
6354-
/* see previous comment about not putting filename in error here */
6355-
posix_error();
6350+
path_error(path);
63566351
return NULL;
63576352
}
63586353

0 commit comments

Comments
 (0)