Ensure no trailing NULLs in result of file_association()

This commit is contained in:
Kovid Goyal 2019-06-11 16:35:29 +05:30
parent 000bb4c89e
commit 1a1db4d1f8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 5 additions and 3 deletions

View File

@ -24,7 +24,9 @@ class TestWinutil(unittest.TestCase):
self.winutil.add_to_recent_docs(path, 'some-app-uid') self.winutil.add_to_recent_docs(path, 'some-app-uid')
def test_file_association(self): def test_file_association(self):
self.assertIn('notepad.exe', self.winutil.file_association('.txt')) q = self.winutil.file_association('.txt')
self.assertIn('notepad.exe', q.lower())
self.assertNotIn('\0', q)
self.assertIsNone(self.winutil.file_association('.mkjsfks')) self.assertIsNone(self.winutil.file_association('.mkjsfks'))
def test_special_folder_path(self): def test_special_folder_path(self):

View File

@ -68,8 +68,8 @@ file_association(PyObject *self, PyObject *args) {
if (!PyArg_ParseTuple(args, "O&", py_to_wchar, &ext)) return NULL; if (!PyArg_ParseTuple(args, "O&", py_to_wchar, &ext)) return NULL;
HRESULT hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, ext, NULL, buf, &sz); HRESULT hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, ext, NULL, buf, &sz);
free_wchar_buffer(&ext); free_wchar_buffer(&ext);
if (!SUCCEEDED(hr)) Py_RETURN_NONE; if (!SUCCEEDED(hr) || sz < 1) Py_RETURN_NONE;
return Py_BuildValue("u#", buf, (int)sz); return Py_BuildValue("u", buf);
} }
PyObject * PyObject *