Skip to content

Commit

Permalink
Fix CRT SECURE warning in winutil
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Feb 13, 2018
1 parent c344c42 commit ac181d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/calibre/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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
is_ci = os.environ.get('CI', '').lower() == 'true'

Expand Down Expand Up @@ -135,6 +135,16 @@ def au(x, name):
au(v, k)
for k in os.environ.keys():
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):
import sqlite3
Expand Down
12 changes: 8 additions & 4 deletions src/calibre/utils/windows/winutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,15 @@ winutil_set_max_stdio(PyObject *self, PyObject *args) {

static PyObject *
winutil_getenv(PyObject *self, PyObject *args) {
const wchar_t *q;
const Py_UNICODE *q;
if (!PyArg_ParseTuple(args, "u", &q)) return NULL;
wchar_t *ans = _wgetenv(q);
if (ans == NULL) Py_RETURN_NONE;
return PyUnicode_FromWideChar(ans, wcslen(ans));
wchar_t *buf = NULL;
size_t sz = 0;
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*
Expand Down

0 comments on commit ac181d2

Please sign in to comment.