diff --git a/src/calibre/startup.py b/src/calibre/startup.py index 8c329fd5aa..8c830ef7b9 100644 --- a/src/calibre/startup.py +++ b/src/calibre/startup.py @@ -64,11 +64,8 @@ if not _run_once: # Ensure that the max number of open files is at least 1024 if iswindows: # See https://msdn.microsoft.com/en-us/library/6e3b887c.aspx - import ctypes - msvcrt = ctypes.cdll.msvcrt - soft, hard = msvcrt._getmaxstdio(), 2048 - if soft < 1024: - msvcrt._setmaxstdio(min(1024, hard)) + if hasattr(winutil, 'setmaxstdio'): + winutil.setmaxstdio(max(1024, winutil.getmaxstdio())) else: import resource soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) diff --git a/src/calibre/utils/windows/winutil.c b/src/calibre/utils/windows/winutil.c index 1339825e08..d4caa0be44 100644 --- a/src/calibre/utils/windows/winutil.c +++ b/src/calibre/utils/windows/winutil.c @@ -215,6 +215,19 @@ winutil_prepare_for_restart(PyObject *self, PyObject *args) { Py_RETURN_NONE; } +static PyObject * +winutil_get_max_stdio(PyObject *self, PyObject *args) { + return Py_BuildValue("i", _getmaxstdio()); +} + +static PyObject * +winutil_set_max_stdio(PyObject *self, PyObject *args) { + int num = 0; + if (!PyArg_ParseTuple(args, "i", &num)) return NULL; + if (_setmaxstdio(num) == -1) return PyErr_SetFromErrno(PyExc_ValueError); + Py_RETURN_NONE; +} + static PyObject * winutil_strftime(PyObject *self, PyObject *args) { @@ -360,6 +373,14 @@ be a unicode string. Returns unicode strings." "prepare_for_restart()\n\nRedirect output streams so that the child process does not lock the temp files" }, + {"getmaxstdio", winutil_get_max_stdio, METH_VARARGS, + "getmaxstdio()\n\nThe maximum number of open file handles." + }, + + {"setmaxstdio", winutil_set_max_stdio, METH_VARARGS, + "setmaxstdio(num)\n\nSet the maximum number of open file handles." + }, + {NULL, NULL, 0, NULL} };