]> granicus.if.org Git - python/commitdiff
Make PySet_Add() work with frozensets.
authorRaymond Hettinger <python@rcn.com>
Mon, 28 Jan 2008 20:34:33 +0000 (20:34 +0000)
committerRaymond Hettinger <python@rcn.com>
Mon, 28 Jan 2008 20:34:33 +0000 (20:34 +0000)
Works like PyTuple_SetItem() to build-up values in a brand new frozenset.
Also, PyFrozenSet_New() is now guaranteed to produce a distinct new frozenset.

Doc/c-api/set.rst
Objects/setobject.c

index e677c05ea546ba941cf387bdc4d7831a996b90a2..2ed93e926b4664b8c0499b58e60b3ed58054efeb 100644 (file)
@@ -86,6 +86,11 @@ the constructor functions work with any iterable Python object.
    set on success or *NULL* on failure.  Raise :exc:`TypeError` if *iterable* is
    not actually iterable.
 
+   .. versionchanged:: 2.6
+      Now guaranteed to return a brand-new :class:`frozenset`.  Formerly,
+      frozensets of zero-length were a singleton.  This got in the way of 
+      building-up new frozensets with :meth:`PySet_Add`.
+
 The following functions and macros are available for instances of :class:`set`
 or :class:`frozenset` or instances of their subtypes.
 
@@ -112,9 +117,6 @@ or :class:`frozenset` or instances of their subtypes.
    the *key* is unhashable. Raise :exc:`PyExc_SystemError` if *anyset* is not a
    :class:`set`, :class:`frozenset`, or an instance of a subtype.
 
-The following functions are available for instances of :class:`set` or its
-subtypes but not for instances of :class:`frozenset` or its subtypes.
-
 
 .. cfunction:: int PySet_Add(PyObject *set, PyObject *key)
 
@@ -124,6 +126,14 @@ subtypes but not for instances of :class:`frozenset` or its subtypes.
    Raise a :exc:`SystemError` if *set* is an not an instance of :class:`set` or its
    subtype.
 
+   .. versionchanged:: 2.6
+      Now works with instances of :class:`frozenset` or its subtypes.
+      Like :cfunc:`PyTuple_SetItem` in that it can be used to fill-in the
+      values of brand new frozensets before they are exposed to other code.
+
+The following functions are available for instances of :class:`set` or its
+subtypes but not for instances of :class:`frozenset` or its subtypes.
+
 
 .. cfunction:: int PySet_Discard(PyObject *set, PyObject *key)
 
index c8db7cef474acda0b04c8c80a09faf40ef6dd291..ee11b9f9b14136e65ecaa9f65a6b0b661eabb9fb 100644 (file)
@@ -2142,17 +2142,7 @@ PySet_New(PyObject *iterable)
 PyObject *
 PyFrozenSet_New(PyObject *iterable)
 {
-       PyObject *args, *result;
-
-       if (iterable == NULL)
-               args = PyTuple_New(0);
-       else
-               args = PyTuple_Pack(1, iterable);
-       if (args == NULL)
-               return NULL;
-       result = frozenset_new(&PyFrozenSet_Type, args, NULL);
-       Py_DECREF(args);
-       return result;
+       return make_new_set(&PyFrozenSet_Type, iterable);
 }
 
 Py_ssize_t
@@ -2196,13 +2186,13 @@ PySet_Discard(PyObject *set, PyObject *key)
 }
 
 int
-PySet_Add(PyObject *set, PyObject *key)
+PySet_Add(PyObject *anyset, PyObject *key)
 {
-       if (!PyType_IsSubtype(Py_TYPE(set), &PySet_Type)) {
+       if (!PyAnySet_Check(anyset)) {
                PyErr_BadInternalCall();
                return -1;
        }
-       return set_add_key((PySetObject *)set, key);
+       return set_add_key((PySetObject *)anyset, key);
 }
 
 int
@@ -2345,7 +2335,6 @@ test_c_api(PySetObject *so)
        f = PyFrozenSet_New(dup);
        assert(PySet_Size(f) == 3);
        assert(PyFrozenSet_CheckExact(f));
-       assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
        assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
        assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
        Py_DECREF(f);