From 7d5dbe292535fc68e118e5f57cb5b42a797542a9 Mon Sep 17 00:00:00 2001 From: ahgamut <41098605+ahgamut@users.noreply.github.com> Date: Tue, 21 Jun 2022 08:07:44 +0530 Subject: [PATCH] use _write_atomic even if file exists and add an error message if failure --- third_party/python/Python/import.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/third_party/python/Python/import.c b/third_party/python/Python/import.c index 2d7abab2c..4b31a13cd 100644 --- a/third_party/python/Python/import.c +++ b/third_party/python/Python/import.c @@ -2214,17 +2214,24 @@ static PyObject *_imp_write_atomic(PyObject *module, PyObject **args, Py_buffer data = {NULL, NULL}; uint32_t mode = 0666; int fd; + int failure = 0; if (!_PyArg_ParseStack(args, nargs, "s#y*|I:_write_atomic", &path, &n, &data, &mode)) - return 0; + goto end; mode &= 0666; - if ((fd = open(path, O_EXCL | O_CREAT | O_WRONLY, mode)) == -1 || - write(fd, data.buf, data.len) == -1) { - PyErr_Format(PyExc_OSError, ""); - if (data.obj) PyBuffer_Release(&data); - return 0; + if ((fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, mode)) == -1) { + failure = 1; + PyErr_Format(PyExc_OSError, "failed to create file: %s\n", path); + goto end; } + if (write(fd, data.buf, data.len) == -1) { + failure = 1; + PyErr_Format(PyExc_OSError, "failed to write to file: %s\n", path); + goto end; + } +end: if (data.obj) PyBuffer_Release(&data); + if (failure) return 0; Py_RETURN_NONE; } PyDoc_STRVAR(_imp_write_atomic_doc, "atomic write to a file");