Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4864a61

Browse files
authored
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
1 parent cdfe910 commit 4864a61

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')
@@ -41,6 +42,17 @@ def test_view_fetch_returns_none(self):
4142
)
4243
self.addCleanup(unlink, db_path)
4344

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

4557
class Test_make_id(unittest.TestCase):
4658
#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
@@ -315,6 +315,12 @@ msierror(int status)
315315
case ERROR_INVALID_PARAMETER:
316316
PyErr_SetString(MSIError, "invalid parameter");
317317
return NULL;
318+
case ERROR_OPEN_FAILED:
319+
PyErr_SetString(MSIError, "open failed");
320+
return NULL;
321+
case ERROR_CREATE_FAILED:
322+
PyErr_SetString(MSIError, "create failed");
323+
return NULL;
318324
default:
319325
PyErr_Format(MSIError, "unknown error %x", status);
320326
return NULL;

0 commit comments

Comments
 (0)