Skip to content

Commit bfa89b2

Browse files
miss-islingtonberkerpeksag
authored andcommitted
bpo-12382: Make OpenDatabase() raise better exception messages (GH-4528)
Previously, 'msilib.OpenDatabase()' function raised a cryptical exception message when it couldn't open or create an MSI file. For example: Traceback (most recent call last): File "<stdin>", line 1, in <module> _msi.MSIError: unknown error 6e (cherry picked from commit 4864a61)
1 parent 90abbee commit bfa89b2

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/test/test_msilib.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" Test suite for the code in msilib """
2+
import os.path
23
import unittest
34
from test.support import TESTFN, import_module, unlink
45
msilib = import_module('msilib')
@@ -40,6 +41,17 @@ def test_view_fetch_returns_none(self):
4041
)
4142
self.addCleanup(unlink, db_path)
4243

44+
def test_database_open_failed(self):
45+
with self.assertRaises(msilib.MSIError) as cm:
46+
msilib.OpenDatabase('non-existent.msi', msilib.MSIDBOPEN_READONLY)
47+
self.assertEqual(str(cm.exception), 'open failed')
48+
49+
def test_database_create_failed(self):
50+
db_path = os.path.join(TESTFN, 'test.msi')
51+
with self.assertRaises(msilib.MSIError) as cm:
52+
msilib.OpenDatabase(db_path, msilib.MSIDBOPEN_CREATE)
53+
self.assertEqual(str(cm.exception), 'create failed')
54+
4355

4456
class Test_make_id(unittest.TestCase):
4557
#http://msdn.microsoft.com/en-us/library/aa369212(v=vs.85).aspx
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`msilib.OpenDatabase` now raises a better exception message when it
2+
couldn't open or create an MSI file. Initial patch by William Tisäter.

PC/_msi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ msierror(int status)
325325
case ERROR_INVALID_PARAMETER:
326326
PyErr_SetString(MSIError, "invalid parameter");
327327
return NULL;
328+
case ERROR_OPEN_FAILED:
329+
PyErr_SetString(MSIError, "open failed");
330+
return NULL;
331+
case ERROR_CREATE_FAILED:
332+
PyErr_SetString(MSIError, "create failed");
333+
return NULL;
328334
default:
329335
PyErr_Format(MSIError, "unknown error %x", status);
330336
return NULL;

0 commit comments

Comments
 (0)