@@ -2897,3 +2897,128 @@ \subsection{DateTime Objects \label{datetime-objects}}
28972897 tuple suitable for passing to \code {datetime.date.fromtimestamp()}.
28982898 \versionadded {2.4}
28992899\end {cfuncdesc }
2900+
2901+
2902+ \subsection {Set Objects \label {setObjects } }
2903+ \sectionauthor {Raymond D. Hettinger}{
[email protected] }
2904+
2905+ \obindex {set}
2906+ \obindex {frozenset}
2907+ \versionadded {2.5}
2908+
2909+ This section details the public API for \class {set} and \class {frozenset}
2910+ objects. Any functionality not listed below is best accessed using the
2911+ abstract object API (including
2912+ \cfunction {PyObject_CallMethod()}, \cfunction {PyObject_RichCompareBool()},
2913+ \cfunction {PyObject_Hash()}, \cfunction {PyObject_Repr()},
2914+ \cfunction {PyObject_IsTrue()}, \cfunction {PyObject_Print()}, and
2915+ \cfunction {PyObject_GetIter()}).
2916+
2917+ \begin {ctypedesc }{PySetObject}
2918+ This subtype of \ctype {PyObject} is used to hold the internal data for
2919+ both \class {set} and \class {frozenset} objects. It is like a
2920+ \ctype {PyDictObject} in that it is a fixed size for small sets
2921+ (much like tuple storage) and will point to a separate, variable sized
2922+ block of memory for medium and large sized sets (much like list storage).
2923+ None of the fields of this structure should be considered public and
2924+ are subject to change. All access should be done through the
2925+ documented API.
2926+
2927+ \end {ctypedesc }
2928+
2929+ \begin {cvardesc }{PyTypeObject}{PySet_Type}
2930+ This is an instance of \ctype {PyTypeObject} representing the Python
2931+ \class {set} type.
2932+ \end {cvardesc }
2933+
2934+ \begin {cvardesc }{PyTypeObject}{PyFrozenSet_Type}
2935+ This is an instance of \ctype {PyTypeObject} representing the Python
2936+ \class {frozenset} type.
2937+ \end {cvardesc }
2938+
2939+
2940+ The following type check macros work on pointers to any Python object.
2941+ Likewise, the constructor functions work with any iterable Python object.
2942+
2943+ \begin {cfuncdesc }{int}{PyAnySet_Check}{PyObject *p}
2944+ Returns true if \var {p} is a \class {set} object, a \class {frozenset}
2945+ object, or an instance of a subtype.
2946+ \end {cfuncdesc }
2947+
2948+ \begin {cfuncdesc }{int}{PyAnySet_CheckExact}{PyObject *p}
2949+ Returns true if \var {p} is a \class {set} object or a \class {frozenset}
2950+ object but not an instance of a subtype.
2951+ \end {cfuncdesc }
2952+
2953+ \begin {cfuncdesc }{int}{PyFrozenSet_CheckExact}{PyObject *p}
2954+ Returns true if \var {p} is a \class {frozenset} object
2955+ but not an instance of a subtype.
2956+ \end {cfuncdesc }
2957+
2958+ \begin {cfuncdesc }{PyObject*}{PySet_New}{PyObject *iterable}
2959+ Returns a new \class {set} containing objects returned by the
2960+ \var {iterable}. The \var {iterable} may be \NULL {} to create a
2961+ new empty set. Returns the new set on success or \NULL {} on
2962+ failure.
2963+ \end {cfuncdesc }
2964+
2965+ \begin {cfuncdesc }{PyObject*}{PyFrozenSet_New}{PyObject *iterable}
2966+ Returns a new \class {frozenset} containing objects returned by the
2967+ \var {iterable}. The \var {iterable} may be \NULL {} to create a
2968+ new empty frozenset. Returns the new set on success or \NULL {} on
2969+ failure.
2970+ \end {cfuncdesc }
2971+
2972+
2973+ The following functions and macros are available for instances of
2974+ \class {set} or \class {frozenset} or instances of their subtypes.
2975+
2976+ \begin {cfuncdesc }{int}{PySet_Size}{PyObject *anyset}
2977+ Returns the length of a \class {set} or \class {frozenset} object.
2978+ Equivalent to \samp {len(\var {anyset})}. Raises a
2979+ \exception {PyExc_SystemError} if the argument is not a \class {set},
2980+ \class {frozenset}, or an instance of a subtype.
2981+ \bifuncindex {len}
2982+ \end {cfuncdesc }
2983+
2984+ \begin {cfuncdesc }{int}{PySet_GET_SIZE}{PyObject *anyset}
2985+ Macro form of \cfunction {PySet_Size()} without error checking.
2986+ \end {cfuncdesc }
2987+
2988+ \begin {cfuncdesc }{int}{PySet_Contains}{PyObject *anyset, PyObject *key}
2989+ Returns 1 if found, 0 if not found, and -1 if an error is
2990+ encountered. Unlike the Python \method {__contains__()} method, this
2991+ function does not automatically convert unhashable sets into temporary
2992+ frozensets. Raises a \exception {TypeError} if the key is unhashable.
2993+ \end {cfuncdesc }
2994+
2995+ \begin {cfuncdesc }{int}{PySet_Discard}{PyObject *anyset, PyObject *key}
2996+ Returns 1 if found and removed, 0 if not found (no action taken),
2997+ and -1 if an error is encountered. Does not raise \exception {KeyError}
2998+ for missing keys. Raises a \exception {TypeError} if the key is unhashable.
2999+ Unlike the Python \method {discard()} method, this function does
3000+ not automatically convert unhashable sets into temporary frozensets.
3001+ \end {cfuncdesc }
3002+
3003+
3004+ The following functions are available for instances of \class {set} or
3005+ its subtypes but not for instances of \class {frozenset} or its subtypes.
3006+
3007+ \begin {cfuncdesc }{int}{PySet_Add}{PyObject *set, PyObject *key}
3008+ Adds \var {key} to a \class {set} instance. Does not apply to
3009+ \class {frozenset} instances. Returns 0 on success or -1 on failure.
3010+ Raises a \exception {TypeError} if the key is unhashable.
3011+ Raises a \exception {MemoryError} if there is no room to grow.
3012+ Raises a \exception {SystemError} if \var {key} is an not an instance
3013+ of \class {set} or its subtype.
3014+ \end {cfuncdesc }
3015+
3016+ \begin {cfuncdesc }{PyObject*}{PySet_Pop}{PyObject *set}
3017+ Returns a new reference to an arbitrary object in the \var {set},
3018+ and removes the object from the \var {set}. Returns \NULL {} on
3019+ failure. Raises \exception {KeyError} if the set is empty.
3020+ Raises a \exception {SystemError} if \var {key} is an not an instance
3021+ of \class {set} or its subtype.
3022+ \end {cfuncdesc }
3023+
3024+
0 commit comments