[Python-checkins] r43465 - in python/trunk: Doc/api/concrete.tex Include/setobject.h Lib/test/test_set.py Objects/setobject.c
barry.warsaw
python-checkins at python.org
Fri Mar 31 00:45:37 CEST 2006
Author: barry.warsaw
Date: Fri Mar 31 00:45:35 2006
New Revision: 43465
Modified:
python/trunk/Doc/api/concrete.tex
python/trunk/Include/setobject.h
python/trunk/Lib/test/test_set.py
python/trunk/Objects/setobject.c
Log:
SF patch #1458476 with modifications based on discussions in python-dev. This
adds the following API calls: PySet_Clear(), _PySet_Next(), and
_PySet_Update(). The latter two are considered non-public. Tests and
documentation (for the public API) are included.
Modified: python/trunk/Doc/api/concrete.tex
==============================================================================
--- python/trunk/Doc/api/concrete.tex (original)
+++ python/trunk/Doc/api/concrete.tex Fri Mar 31 00:45:35 2006
@@ -3027,8 +3027,6 @@
\cfunction{PyNumber_InPlaceAdd()}, \cfunction{PyNumber_InPlaceSubtract()},
\cfunction{PyNumber_InPlaceOr()}, and \cfunction{PyNumber_InPlaceXor()}).
-For example, to clear a set, write: \code{PyObject_CallMethod(s, "clear", NULL)}
-
\begin{ctypedesc}{PySetObject}
This subtype of \ctype{PyObject} is used to hold the internal data for
both \class{set} and \class{frozenset} objects. It is like a
@@ -3112,7 +3110,6 @@
\class{frozenset}, or an instance of a subtype.
\end{cfuncdesc}
-
The following functions are available for instances of \class{set} or
its subtypes but not for instances of \class{frozenset} or its subtypes.
@@ -3143,4 +3140,6 @@
of \class{set} or its subtype.
\end{cfuncdesc}
-
+\begin{cfuncdesc}{int}{PySet_Clear}{PyObject *set}
+ Empty an existing set of all elements.
+\end{cfuncdesc}
Modified: python/trunk/Include/setobject.h
==============================================================================
--- python/trunk/Include/setobject.h (original)
+++ python/trunk/Include/setobject.h Fri Mar 31 00:45:35 2006
@@ -78,10 +78,13 @@
PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
PyAPI_FUNC(Py_ssize_t) PySet_Size(PyObject *anyset);
#define PySet_GET_SIZE(so) (((PySetObject *)(so))->used)
+PyAPI_FUNC(int) PySet_Clear(PyObject *set);
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(PyObject *) PySet_Pop(PyObject *set);
+PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
#ifdef __cplusplus
}
Modified: python/trunk/Lib/test/test_set.py
==============================================================================
--- python/trunk/Lib/test/test_set.py (original)
+++ python/trunk/Lib/test/test_set.py Fri Mar 31 00:45:35 2006
@@ -421,7 +421,7 @@
self.assertRaises(ReferenceError, str, p)
# C API test only available in a debug build
- if hasattr(sys, "gettotalrefcount"):
+ if hasattr(set, "test_c_api"):
def test_c_api(self):
self.assertEqual(set('abc').test_c_api(), True)
Modified: python/trunk/Objects/setobject.c
==============================================================================
--- python/trunk/Objects/setobject.c (original)
+++ python/trunk/Objects/setobject.c Fri Mar 31 00:45:35 2006
@@ -1969,6 +1969,16 @@
}
int
+PySet_Clear(PyObject *set)
+{
+ if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return set_clear_internal((PySetObject *)set);
+}
+
+int
PySet_Contains(PyObject *anyset, PyObject *key)
{
if (!PyAnySet_Check(anyset)) {
@@ -1998,6 +2008,21 @@
return set_add_key((PySetObject *)set, key);
}
+int
+_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
+{
+ setentry *entry_ptr;
+
+ if (!PyAnySet_Check(set)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
+ return 0;
+ *entry = entry_ptr->key;
+ return 1;
+}
+
PyObject *
PySet_Pop(PyObject *set)
{
@@ -2008,6 +2033,15 @@
return set_pop((PySetObject *)set);
}
+int
+_PySet_Update(PyObject *set, PyObject *iterable)
+{
+ if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return set_update_internal((PySetObject *)set, iterable);
+}
#ifdef Py_DEBUG
@@ -2024,7 +2058,11 @@
static PyObject *
test_c_api(PySetObject *so)
{
- PyObject *elem, *dup, *t, *f, *ob = (PyObject *)so;
+ int count;
+ char *s;
+ Py_ssize_t i;
+ PyObject *elem, *dup, *t, *f, *dup2;
+ PyObject *ob = (PyObject *)so;
/* Verify preconditions and exercise type/size checks */
assert(PyAnySet_Check(ob));
@@ -2055,6 +2093,35 @@
assert(PySet_Discard(ob, elem) == 0);
assert(PySet_GET_SIZE(ob) == 2);
+ /* Exercise clear */
+ dup2 = PySet_New(dup);
+ assert(PySet_Clear(dup2) == 0);
+ assert(PySet_Size(dup2) == 0);
+ Py_DECREF(dup2);
+
+ /* Raise SystemError on clear or update of frozen set */
+ f = PyFrozenSet_New(dup);
+ assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
+ assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
+ Py_DECREF(f);
+
+ /* Exercise direct iteration */
+ i = 0, count = 0;
+ while (_PySet_Next((PyObject *)dup, &i, &elem)) {
+ s = PyString_AsString(elem);
+ assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
+ count++;
+ }
+ assert(count == 3);
+
+ /* Exercise updates */
+ dup2 = PySet_New(NULL);
+ assert(_PySet_Update(dup2, dup) == 0);
+ assert(PySet_Size(dup2) == 3);
+ assert(_PySet_Update(dup2, dup) == 0);
+ assert(PySet_Size(dup2) == 3);
+ Py_DECREF(dup2);
+
/* Raise SystemError when self argument is not a set or frozenset. */
t = PyTuple_New(0);
assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
More information about the Python-checkins
mailing list