[Python-checkins] cpython: Minor refactoring. Move reference count logic into function that adds entry.

raymond.hettinger python-checkins at python.org
Sun Jun 28 07:03:40 CEST 2015


https://hg.python.org/cpython/rev/637e197be547
changeset:   96697:637e197be547
user:        Raymond Hettinger <python at rcn.com>
date:        Sat Jun 27 22:03:35 2015 -0700
summary:
  Minor refactoring.  Move reference count logic into function that adds entry.

files:
  Objects/setobject.c |  18 ++++--------------
  1 files changed, 4 insertions(+), 14 deletions(-)


diff --git a/Objects/setobject.c b/Objects/setobject.c
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -125,11 +125,6 @@
     }
 }
 
-/*
-Internal routine to insert a new key into the table.
-Used by the public insert routine.
-Eats a reference to key.
-*/
 static int
 set_insert_key(PySetObject *so, PyObject *key, Py_hash_t hash)
 {
@@ -213,6 +208,7 @@
     }
 
   found_null_first:
+    Py_INCREF(key);
     so->fill++;
     so->used++;
     entry->key = key;
@@ -220,6 +216,7 @@
     return 0;
 
   found_null:
+    Py_INCREF(key);
     if (freeslot == NULL) {
         /* UNUSED */
         so->fill++;
@@ -233,7 +230,6 @@
     return 0;
 
   found_active:
-    Py_DECREF(key);
     return 0;
 }
 
@@ -381,11 +377,8 @@
 
     assert(so->fill <= so->mask);  /* at least one empty slot */
     n_used = so->used;
-    Py_INCREF(key);
-    if (set_insert_key(so, key, hash)) {
-        Py_DECREF(key);
+    if (set_insert_key(so, key, hash))
         return -1;
-    }
     if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
         return 0;
     return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
@@ -678,11 +671,8 @@
     for (i = 0; i <= other->mask; i++, other_entry++) {
         key = other_entry->key;
         if (key != NULL && key != dummy) {
-            Py_INCREF(key);
-            if (set_insert_key(so, key, other_entry->hash)) {
-                Py_DECREF(key);
+            if (set_insert_key(so, key, other_entry->hash))
                 return -1;
-            }
         }
     }
     return 0;

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list