[Python-checkins] r54465 - in python/trunk: Include/setobject.h Lib/test/test_set.py Objects/dictobject.c Objects/setobject.c

raymond.hettinger python-checkins at python.org
Tue Mar 20 22:27:30 CET 2007


Author: raymond.hettinger
Date: Tue Mar 20 22:27:24 2007
New Revision: 54465

Modified:
   python/trunk/Include/setobject.h
   python/trunk/Lib/test/test_set.py
   python/trunk/Objects/dictobject.c
   python/trunk/Objects/setobject.c
Log:
Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.

Modified: python/trunk/Include/setobject.h
==============================================================================
--- python/trunk/Include/setobject.h	(original)
+++ python/trunk/Include/setobject.h	Tue Mar 20 22:27:24 2007
@@ -82,7 +82,8 @@
 PyAPI_FUNC(int) PySet_Contains(PyObject *anyset, PyObject *key);
 PyAPI_FUNC(int) PySet_Discard(PyObject *set, PyObject *key);
 PyAPI_FUNC(int) PySet_Add(PyObject *set, PyObject *key);
-PyAPI_FUNC(int) _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry);
+PyAPI_FUNC(int) _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key);
+PyAPI_FUNC(int) _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash);
 PyAPI_FUNC(PyObject *) PySet_Pop(PyObject *set);
 PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
 

Modified: python/trunk/Lib/test/test_set.py
==============================================================================
--- python/trunk/Lib/test/test_set.py	(original)
+++ python/trunk/Lib/test/test_set.py	Tue Mar 20 22:27:24 2007
@@ -288,6 +288,10 @@
         self.assertEqual(sum(elem.hash_count for elem in d), n)
         if hasattr(s, 'symmetric_difference_update'):
             s.symmetric_difference_update(d)
+        self.assertEqual(sum(elem.hash_count for elem in d), n)     
+        d2 = dict.fromkeys(set(d))
+        self.assertEqual(sum(elem.hash_count for elem in d), n)
+        d3 = dict.fromkeys(frozenset(d))
         self.assertEqual(sum(elem.hash_count for elem in d), n)
 
 class TestSet(TestJointOps):

Modified: python/trunk/Objects/dictobject.c
==============================================================================
--- python/trunk/Objects/dictobject.c	(original)
+++ python/trunk/Objects/dictobject.c	Tue Mar 20 22:27:24 2007
@@ -1175,6 +1175,24 @@
 	if (d == NULL)
 		return NULL;
 
+	if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
+		dictobject *mp = (dictobject *)d;
+		Py_ssize_t pos = 0;
+		PyObject *key;
+		long hash;
+
+		if (dictresize(mp, PySet_GET_SIZE(seq)))
+			return NULL;
+
+		while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
+			Py_INCREF(key);
+			Py_INCREF(Py_None);
+			if (insertdict(mp, key, hash, Py_None))
+				return NULL;
+		}
+		return d;
+	}
+
 	it = PyObject_GetIter(seq);
 	if (it == NULL){
 		Py_DECREF(d);

Modified: python/trunk/Objects/setobject.c
==============================================================================
--- python/trunk/Objects/setobject.c	(original)
+++ python/trunk/Objects/setobject.c	Tue Mar 20 22:27:24 2007
@@ -2137,7 +2137,7 @@
 }
 
 int
-_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
+_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
 {
 	setentry *entry_ptr;
 
@@ -2147,7 +2147,23 @@
 	}
 	if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
 		return 0;
-	*entry = entry_ptr->key;
+	*key = entry_ptr->key;
+	return 1;
+}
+
+int
+_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
+{
+	setentry *entry;
+
+	if (!PyAnySet_Check(set)) {
+		PyErr_BadInternalCall();
+		return -1;
+	}
+	if (set_next((PySetObject *)set, pos, &entry) == 0)
+		return 0;
+	*key = entry->key;
+	*hash = entry->hash;
 	return 1;
 }
 


More information about the Python-checkins mailing list