[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sanlock-devel
Subject:    [sanlock] 03/08: python: Streamline initexception
From:       pagure () pagure ! io
Date:       2019-06-25 15:53:35
Message-ID: 20190625155332.60F6C254704F () pagure01 ! fedoraproject ! org
[Download RAW message or body]

This is an automated email from the git hooks/post-receive script.

nsoffer pushed a commit to branch master
in repository sanlock.

commit f5c5b73895b68d8c6fd032a4fd5f98a9e5fb8a4a
Author: Nir Soffer <nsoffer@redhat.com>
AuthorDate: Sun Jun 16 20:15:36 2019 +0300

    python: Streamline initexception
    
    When we create the sanlock exception we create a function, create a
    method from the function, create a dict from the method, and finally
    create an exception from the dict. This simple flow does not require
    goto for cleanup.
    
    Creating a dict is simpler and less error prone using Py_BuildValue().
    
    When we finish with unused pointers, it is safer to set them to NULL
    using Py_CLEAR(), ensuring that we don't access freed memory.
    
    Signed-off-by: Nir Soffer <nsoffer@redhat.com>
---
 python/sanlock.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)

diff --git a/python/sanlock.c b/python/sanlock.c
index 21a7a76..5c2fd1a 100644
--- a/python/sanlock.c
+++ b/python/sanlock.c
@@ -1743,29 +1743,22 @@ sanlock_exception = {
 static PyObject *
 initexception(void)
 {
-    int rv;
-    PyObject *dict, *func, *meth, *excp = NULL;
-
-    if ((dict = PyDict_New()) == NULL)
-        goto exit_fail;
-
-    if ((func = PyCFunction_New(&sanlock_exception, NULL)) == NULL)
-        goto exit_fail;
+    PyObject *func = PyCFunction_New(&sanlock_exception, NULL);
+    if (func == NULL)
+        return NULL;
 
-    meth = PyObject_CallFunction((PyObject *) &PyProperty_Type, "O", func);
-    Py_DECREF(func);
+    PyObject *meth = PyObject_CallFunction((PyObject *) &PyProperty_Type, "O", func);
+    Py_CLEAR(func);
     if (meth == NULL)
-        goto exit_fail;
-
-    rv = PyDict_SetItemString(dict, sanlock_exception.ml_name, meth);
-    Py_DECREF(meth);
-    if (rv < 0)
-        goto exit_fail;
+        return NULL;
 
-    excp = PyErr_NewException("sanlock.SanlockException", NULL, dict);
+    PyObject *dict = Py_BuildValue("{s:O}", sanlock_exception.ml_name, meth);
+    Py_CLEAR(meth);
+    if (dict == NULL)
+        return NULL;
 
-exit_fail:
-    Py_XDECREF(dict);
+    PyObject *excp = PyErr_NewException("sanlock.SanlockException", NULL, dict);
+    Py_CLEAR(dict);
 
     return excp;
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
sanlock-devel mailing list -- sanlock-devel@lists.fedorahosted.org
To unsubscribe send an email to sanlock-devel-leave@lists.fedorahosted.org
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedorahosted.org/archives/list/sanlock-devel@lists.fedorahosted.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic