From 19d2d7ed9aab99597d01fd90d41db06325cb97d9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 16 Feb 2013 10:36:03 +0530 Subject: [PATCH] Fix stdout/stderr redirection temp files not being deleted when restarting calibre from within calibre on windows --- src/calibre/gui2/main.py | 2 ++ src/calibre/utils/windows/winutil.c | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/calibre/gui2/main.py b/src/calibre/gui2/main.py index d395ed90c1..f722cf226f 100644 --- a/src/calibre/gui2/main.py +++ b/src/calibre/gui2/main.py @@ -323,6 +323,8 @@ def run_gui(opts, args, actions, listener, app, gui_debug=None): app = os.path.dirname(os.path.dirname(sys.frameworks_dir)) subprocess.Popen('sleep 3s; open '+app, shell=True) else: + if iswindows and hasattr(winutil, 'prepare_for_restart'): + winutil.prepare_for_restart() subprocess.Popen([e] + sys.argv[1:]) else: if iswindows: diff --git a/src/calibre/utils/windows/winutil.c b/src/calibre/utils/windows/winutil.c index 71eac1e414..a7475e1765 100644 --- a/src/calibre/utils/windows/winutil.c +++ b/src/calibre/utils/windows/winutil.c @@ -815,6 +815,15 @@ winutil_internet_connected(PyObject *self, PyObject *args) { Py_RETURN_FALSE; } +static PyObject * +winutil_prepare_for_restart(PyObject *self, PyObject *args) { + FILE *f1 = NULL, *f2 = NULL; + if (stdout != NULL) fclose(stdout); + if (stderr != NULL) fclose(stderr); + _wfreopen_s(&f1, L"NUL", L"a+t", stdout); + _wfreopen_s(&f2, L"NUL", L"a+t", stderr); + Py_RETURN_NONE; +} static PyObject * winutil_strftime(PyObject *self, PyObject *args) @@ -978,6 +987,10 @@ be a unicode string. Returns unicode strings." "internet_connected()\n\nReturn True if there is an active internet connection" }, + {"prepare_for_restart", winutil_prepare_for_restart, METH_VARARGS, + "prepare_for_restart()\n\nRedirect output streams so that the child process does not lock the temp files" + }, + {NULL, NULL, 0, NULL} };