BIND 10 trac1359, updated. 21887dffc4cd692ce23bfff1685fba0e2c1e55b0 [1359] throw InternalError when PyObject_Str fails (rather than letting PyObjectContainer throw PyCPPWrapperException, which would result in RuntimeError) since it can for other reasons than memory shortage. added a test case to confirm this.

BIND 10 source code commits bind10-changes at lists.isc.org
Wed Nov 16 18:23:50 UTC 2011


The branch, trac1359 has been updated
       via  21887dffc4cd692ce23bfff1685fba0e2c1e55b0 (commit)
      from  ff5154291678973eaa0483518302b74a62f0acba (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 21887dffc4cd692ce23bfff1685fba0e2c1e55b0
Author: JINMEI Tatuya <jinmei at isc.org>
Date:   Wed Nov 16 10:22:09 2011 -0800

    [1359] throw InternalError when PyObject_Str fails (rather than letting
    PyObjectContainer throw PyCPPWrapperException, which would result in
    RuntimeError) since it can for other reasons than memory shortage.
    added a test case to confirm this.

-----------------------------------------------------------------------

Summary of changes:
 src/lib/python/isc/log/log.cc            |    9 ++++++++-
 src/lib/python/isc/log/tests/log_test.py |   18 +++++++++++++-----
 2 files changed, 21 insertions(+), 6 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/python/isc/log/log.cc b/src/lib/python/isc/log/log.cc
index 8602db9..2e4a28f 100644
--- a/src/lib/python/isc/log/log.cc
+++ b/src/lib/python/isc/log/log.cc
@@ -483,7 +483,14 @@ string
 objectToStr(PyObject* object, bool convert) {
     PyObjectContainer objstr_container;
     if (convert) {
-        objstr_container.reset(PyObject_Str(object));
+        PyObject* text_obj = PyObject_Str(object);
+        if (text_obj == NULL) {
+            // PyObject_Str could fail for various reasons, including because
+            // the object cannot be converted to a string.  We exit with
+            // InternalError to preserve the PyErr set in PyObject_Str.
+            throw InternalError();
+        }
+        objstr_container.reset(text_obj);
         object = objstr_container.get();
     }
 
diff --git a/src/lib/python/isc/log/tests/log_test.py b/src/lib/python/isc/log/tests/log_test.py
index 7233009..1337654 100644
--- a/src/lib/python/isc/log/tests/log_test.py
+++ b/src/lib/python/isc/log/tests/log_test.py
@@ -90,6 +90,7 @@ class Logger(unittest.TestCase):
     def setUp(self):
         isc.log.init("root", "DEBUG", 50)
         self.sevs = ['INFO', 'WARN', 'ERROR', 'FATAL']
+        self.TEST_MSG = isc.log.create_message('TEST_MESSAGE', '%1')
 
     # Checks defaults of the logger
     def defaults(self, logger):
@@ -174,23 +175,30 @@ class Logger(unittest.TestCase):
         """
         Check whether passing a parameter to a logger causes a reference leak.
         """
-        MSG = isc.log.create_message('TEST_MESSAGE', '%1')
         class LogParam:
             def __str__(self):
                 return 'LogParam'
         logger = isc.log.Logger("child")
         param = LogParam()
         orig_msgrefcnt = sys.getrefcount(param)
-        orig_idrefcnt = sys.getrefcount(MSG)
-        logger.info(MSG, param);
-        self.assertEqual(sys.getrefcount(MSG), orig_idrefcnt)
+        orig_idrefcnt = sys.getrefcount(self.TEST_MSG)
+        logger.info(self.TEST_MSG, param);
+        self.assertEqual(sys.getrefcount(self.TEST_MSG), orig_idrefcnt)
         self.assertEqual(sys.getrefcount(param), orig_msgrefcnt)
 
         # intentionally pass an invalid type for debug level.  It will
         # result in TypeError.  The passed object still shouldn't leak a
         # reference.
-        self.assertRaises(TypeError, logger.debug, param, MSG, param)
+        self.assertRaises(TypeError, logger.debug, param, self.TEST_MSG, param)
         self.assertEqual(sys.getrefcount(param), orig_msgrefcnt)
 
+    def test_bad_parameter(self):
+        # a log parameter cannot be converted to a string object.
+        class LogParam:
+            def __str__(self):
+                raise ValueError("LogParam can't be converted to string")
+        logger = isc.log.Logger("child")
+        self.assertRaises(ValueError, logger.info, self.TEST_MSG, LogParam())
+
 if __name__ == '__main__':
     unittest.main()




More information about the bind10-changes mailing list