[Python-checkins] r60560 - in python/trunk: Doc/c-api/set.rst Include/setobject.h Misc/NEWS Objects/setobject.c

amaury.forgeotdarc python-checkins at python.org
Sun Feb 3 23:51:44 CET 2008


Author: amaury.forgeotdarc
Date: Sun Feb  3 23:51:43 2008
New Revision: 60560

Modified:
   python/trunk/Doc/c-api/set.rst
   python/trunk/Include/setobject.h
   python/trunk/Misc/NEWS
   python/trunk/Objects/setobject.c
Log:
Ensure that PySet_Add() operates on a newly created frozenset, like PyTuple_SetItem does.

Add PyFrozenSet_Check(), which was not needed before; The list of Py*Set_Check* macros seems to be complete now.

Add missing NEWS entries about all this.


Modified: python/trunk/Doc/c-api/set.rst
==============================================================================
--- python/trunk/Doc/c-api/set.rst	(original)
+++ python/trunk/Doc/c-api/set.rst	Sun Feb  3 23:51:43 2008
@@ -58,6 +58,13 @@
 
    .. versionadded:: 2.6
 
+.. cfunction:: int PyFrozenSet_Check(PyObject *p)
+
+   Return true if *p* is a :class:`frozenset` object or an instance of a
+   subtype.
+
+   .. versionadded:: 2.6
+
 .. cfunction:: int PyAnySet_Check(PyObject *p)
 
    Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an

Modified: python/trunk/Include/setobject.h
==============================================================================
--- python/trunk/Include/setobject.h	(original)
+++ python/trunk/Include/setobject.h	Sun Feb  3 23:51:43 2008
@@ -74,7 +74,11 @@
 	  PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
 	  PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
 #define PySet_Check(ob) \
-	(Py_TYPE(ob) == &PySet_Type || PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
+	(Py_TYPE(ob) == &PySet_Type || \
+	PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
+#define   PyFrozenSet_Check(ob) \
+	(Py_TYPE(ob) == &PyFrozenSet_Type || \\
+	  PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
 
 PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
 PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sun Feb  3 23:51:43 2008
@@ -1471,6 +1471,12 @@
 C API
 -----
 
+- ``PySet_Add()`` can now modify a newly created frozenset.  Similarly to
+  ``PyTuple_SetItem``, it can be used to populate a brand new frozenset; but 
+  it does not steal a reference to the added item.
+
+- Added ``PySet_Check()`` and ``PyFrozenSet_Check()`` to the set API. 
+
 - Backport of PyUnicode_FromString(), _FromStringAndSize(), _Format and
   _FormatV from Python 3.0. Made PyLong_AsSsize_t and PyLong_FromSsize_t
   public functions.

Modified: python/trunk/Objects/setobject.c
==============================================================================
--- python/trunk/Objects/setobject.c	(original)
+++ python/trunk/Objects/setobject.c	Sun Feb  3 23:51:43 2008
@@ -2188,7 +2188,8 @@
 int
 PySet_Add(PyObject *anyset, PyObject *key)
 {
-	if (!PyAnySet_Check(anyset)) {
+	if (!PySet_Check(anyset) && 
+	    (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
 		PyErr_BadInternalCall();
 		return -1;
 	}
@@ -2306,6 +2307,10 @@
 	f = PyFrozenSet_New(dup);
 	assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
 	assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
+	assert(PySet_Add(f, elem) == 0);
+	Py_INCREF(f);
+	assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
+	Py_DECREF(f);
 	Py_DECREF(f);
 
 	/* Exercise direct iteration */


More information about the Python-checkins mailing list