[Python-checkins] bpo-12382: Make OpenDatabase() raise better exception messages (GH-4528)

Berker Peksag webhook-mailer at python.org
Fri Nov 24 04:54:05 EST 2017


https://github.com/python/cpython/commit/4864a619dc1cc9092780ccf5a6327e8abf66133d
commit: 4864a619dc1cc9092780ccf5a6327e8abf66133d
branch: master
author: Berker Peksag <berker.peksag at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-11-24T12:53:58+03:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2017-11-23-21-47-36.bpo-12382.xWT9k0.rst
M Lib/test/test_msilib.py
M PC/_msi.c

diff --git a/Lib/test/test_msilib.py b/Lib/test/test_msilib.py
index 65ff3869c33..4093134195a 100644
--- a/Lib/test/test_msilib.py
+++ b/Lib/test/test_msilib.py
@@ -1,4 +1,5 @@
 """ Test suite for the code in msilib """
+import os.path
 import unittest
 from test.support import TESTFN, import_module, unlink
 msilib = import_module('msilib')
@@ -41,6 +42,17 @@ def test_view_fetch_returns_none(self):
         )
         self.addCleanup(unlink, db_path)
 
+    def test_database_open_failed(self):
+        with self.assertRaises(msilib.MSIError) as cm:
+            msilib.OpenDatabase('non-existent.msi', msilib.MSIDBOPEN_READONLY)
+        self.assertEqual(str(cm.exception), 'open failed')
+
+    def test_database_create_failed(self):
+        db_path = os.path.join(TESTFN, 'test.msi')
+        with self.assertRaises(msilib.MSIError) as cm:
+            msilib.OpenDatabase(db_path, msilib.MSIDBOPEN_CREATE)
+        self.assertEqual(str(cm.exception), 'create failed')
+
 
 class Test_make_id(unittest.TestCase):
     #http://msdn.microsoft.com/en-us/library/aa369212(v=vs.85).aspx
diff --git a/Misc/NEWS.d/next/Library/2017-11-23-21-47-36.bpo-12382.xWT9k0.rst b/Misc/NEWS.d/next/Library/2017-11-23-21-47-36.bpo-12382.xWT9k0.rst
new file mode 100644
index 00000000000..d9b54259cc6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-11-23-21-47-36.bpo-12382.xWT9k0.rst
@@ -0,0 +1,2 @@
+:func:`msilib.OpenDatabase` now raises a better exception message when it
+couldn't open or create an MSI file.  Initial patch by William Tisäter.
diff --git a/PC/_msi.c b/PC/_msi.c
index a6a12e20107..8cc1fd59dd1 100644
--- a/PC/_msi.c
+++ b/PC/_msi.c
@@ -315,6 +315,12 @@ msierror(int status)
         case ERROR_INVALID_PARAMETER:
             PyErr_SetString(MSIError, "invalid parameter");
             return NULL;
+        case ERROR_OPEN_FAILED:
+            PyErr_SetString(MSIError, "open failed");
+            return NULL;
+        case ERROR_CREATE_FAILED:
+            PyErr_SetString(MSIError, "create failed");
+            return NULL;
         default:
             PyErr_Format(MSIError, "unknown error %x", status);
             return NULL;



More information about the Python-checkins mailing list