Fix CRT SECURE warning in winutil

This commit is contained in:
Kovid Goyal 2018-02-14 03:36:47 +05:30
parent c344c42a7d
commit ac181d2198
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 19 additions and 5 deletions

View File

@ -12,7 +12,7 @@ __docformat__ = 'restructuredtext en'
Test a binary calibre build to ensure that all needed binary images/libraries have loaded. Test a binary calibre build to ensure that all needed binary images/libraries have loaded.
''' '''
import os, ctypes, sys, unittest import os, ctypes, sys, unittest, time
from calibre.constants import plugins, iswindows, islinux, isosx from calibre.constants import plugins, iswindows, islinux, isosx
is_ci = os.environ.get('CI', '').lower() == 'true' is_ci = os.environ.get('CI', '').lower() == 'true'
@ -135,6 +135,16 @@ class BuildTest(unittest.TestCase):
au(v, k) au(v, k)
for k in os.environ.keys(): for k in os.environ.keys():
au(winutil.getenv(unicode(k)), 'getenv-' + k) au(winutil.getenv(unicode(k)), 'getenv-' + k)
os.environ['XXXTEST'] = 'YYY'
self.assertEqual(winutil.getenv(u'XXXTEST'), u'YYY')
del os.environ['XXXTEST']
self.assertIsNone(winutil.getenv(u'XXXTEST'))
t = time.localtime()
fmt = u'%Y%a%b%e%H%M'
for fmt in (fmt, fmt.encode('ascii')):
x = winutil.strftime(fmt, t)
au(x, 'strftime')
self.ae(unicode(time.strftime(fmt, t)), x)
def test_sqlite(self): def test_sqlite(self):
import sqlite3 import sqlite3

View File

@ -232,11 +232,15 @@ winutil_set_max_stdio(PyObject *self, PyObject *args) {
static PyObject * static PyObject *
winutil_getenv(PyObject *self, PyObject *args) { winutil_getenv(PyObject *self, PyObject *args) {
const wchar_t *q; const Py_UNICODE *q;
if (!PyArg_ParseTuple(args, "u", &q)) return NULL; if (!PyArg_ParseTuple(args, "u", &q)) return NULL;
wchar_t *ans = _wgetenv(q); wchar_t *buf = NULL;
if (ans == NULL) Py_RETURN_NONE; size_t sz = 0;
return PyUnicode_FromWideChar(ans, wcslen(ans)); PyObject *ans = NULL;
if (_wdupenv_s(&buf, &sz, q) != 0 || buf == NULL) { ans = Py_None; Py_INCREF(ans); }
else ans = PyUnicode_FromWideChar(buf, sz);
if (buf) free(buf);
return ans;
} }
static PyObject* static PyObject*