]> granicus.if.org Git - python/commitdiff
Ensure that PySet_Add() operates on a newly created frozenset, like PyTuple_SetItem...
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sun, 3 Feb 2008 22:51:43 +0000 (22:51 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sun, 3 Feb 2008 22:51:43 +0000 (22:51 +0000)
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.

Doc/c-api/set.rst
Include/setobject.h
Misc/NEWS
Objects/setobject.c

index 0aa6bcecda56d0f669790de47effe297913f1b4d..c6b097dae43a1f464f122d1a8b90f42189e74cee 100644 (file)
@@ -58,6 +58,13 @@ the constructor functions work with any iterable Python object.
 
    .. 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
index fc32367724ea961a94e352bdc81347e60680503d..102796d315f833d84c31d2fa1803ed2c6690169b 100644 (file)
@@ -74,7 +74,11 @@ PyAPI_DATA(PyTypeObject) PyFrozenSet_Type;
          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 *);
index 8dacce0e370799016f937cb20464836b63709e54..629172957da3355bd5895615517bb62203e7dcd3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1471,6 +1471,12 @@ Build
 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.
index 4b4213ab8a3ce9cf58d871e700b1b8efb7433275..53d284f6cccbca6f644499f60aa5f8ad6697fd68 100644 (file)
@@ -2188,7 +2188,8 @@ PySet_Discard(PyObject *set, PyObject *key)
 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 @@ test_c_api(PySetObject *so)
        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 */