[Python-checkins] python/nondist/sandbox/setobj setobject.c, NONE, 1.1 setup.py, NONE, 1.1 test_set.py, NONE, 1.1

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed Nov 12 15:06:26 EST 2003


Update of /cvsroot/python/python/nondist/sandbox/setobj
In directory sc8-pr-cvs1:/tmp/cvs-serv24822/setobj
Added Files:
	setobject.c setup.py test_set.py 
Log Message:
Setup sandbox for builtin set() and frozenset() types
--- NEW FILE: setobject.c ---
#include "Python.h"
/* setmodule written and maintained 
 by Raymond D. Hettinger <python at rcn.com>
 derived from sets.py written by Greg V. Wilson, Alex Martelli, 
 Guido van Rossum, Raymond Hettinger, and Tim Peters.
 This module introduces two new builtin types, set() and
 frozenset(). Based on user feedback from sets.py, there
 is no automatic conversion between the two types.
 Copyright (c) 2003 Python Software Foundation.
 All rights reserved.
*/
/* set object **********************************************************/
typedef struct {
	PyObject_HEAD
	PyObject *data;
} setobject;
static PyTypeObject set_type;
static PyObject *
set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	PyObject *data;
	PyObject *it;
	PyObject *iterable = NULL;
	PyObject *item;
	setobject *so;
	if (!PyArg_UnpackTuple(args, "set", 0, 1, &iterable))
		return NULL;
	/* Get iterator. */
	it = PyObject_GetIter(iterable);
	if (it == NULL)
		return NULL;
	data = PyDict_New();
	if (data == NULL) {
		Py_DECREF(data);
		return NULL;
	}
	while (iterable != NULL) {
		item = PyIter_Next(it);
 if (item == NULL)
 break; // XXX add error checking
 if (PyDict_SetItem(data, item, Py_None) == -1) {
			Py_DECREF(it);
			Py_DECREF(data);
			PyErr_SetString(PyExc_ValueError,
					"all set entries must be immutable");
			return NULL;
 } 
		Py_DECREF(item);
	}
	Py_DECREF(it);
	/* create setobject structure */
	so = (setobject *)type->tp_alloc(type, 0);
	if (so == NULL) {
		Py_DECREF(data);
		return NULL;
	}
	so->data = data;
	return (PyObject *)so;
}
static void
set_dealloc(setobject *so)
{
	PyObject_GC_UnTrack(so);
	Py_XDECREF(so->data);
	so->ob_type->tp_free(so);
}
static int
set_traverse(setobject *so, visitproc visit, void *arg)
{
	if (so->data)
		return visit(so->data, arg);
	return 0;
}
static PyObject *
set_iter(setobject *so)
{
	return PyObject_GetIter(so->data);
}
PyDoc_STRVAR(set_doc,
"set(iterable) --> set object\n\
\n\
Build an unordered collection.");
static PyTypeObject set_type = {
	PyObject_HEAD_INIT(NULL)
	0,				/* ob_size */
	"set.set",			/* tp_name */
	sizeof(setobject),		/* tp_basicsize */
	0,				/* tp_itemsize */
	/* methods */
	(destructor)set_dealloc,	/* tp_dealloc */
	0,				/* tp_print */
	0,				/* tp_getattr */
	0,				/* tp_setattr */
	0,				/* tp_compare */
	0,				/* tp_repr */
	0,				/* tp_as_number */
	0,				/* tp_as_sequence */
	0,				/* tp_as_mapping */
	0,				/* tp_hash */
	0,				/* tp_call */
	0,				/* tp_str */
	PyObject_GenericGetAttr,	/* tp_getattro */
	0,				/* tp_setattro */
	0,				/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
		Py_TPFLAGS_BASETYPE,	/* tp_flags */
	set_doc,			/* tp_doc */
	(traverseproc)set_traverse,	/* tp_traverse */
	0,				/* tp_clear */
	0,				/* tp_richcompare */
	0,				/* tp_weaklistoffset */
	(getiterfunc)set_iter,		/* tp_iter */
	0,				/* tp_iternext */
	0,				/* tp_methods */
	0,				/* tp_members */
	0,				/* tp_getset */
	0,				/* tp_base */
	0,				/* tp_dict */
	0,				/* tp_descr_get */
	0,				/* tp_descr_set */
	0,				/* tp_dictoffset */
	0,				/* tp_init */
	0,				/* tp_alloc */
	set_new,			/* tp_new */
	PyObject_GC_Del,		/* tp_free */
};
/* module level code ********************************************************/
PyDoc_STRVAR(module_doc,
"Functional tools for creating and using sets.\n\
\n\
set(iterable) --> new mutuable set \n\
frozenset(iterable) --> new immutable set \n\
");
PyMODINIT_FUNC
initset(void)
{
	PyObject *m;
	if (PyType_Ready(&set_type) < 0)
		return;
	m = Py_InitModule3("set", NULL, module_doc);
	Py_INCREF(&set_type);
	PyModule_AddObject(m, "set", (PyObject *)&set_type);
}
--- NEW FILE: setup.py ---
from distutils.core import setup, Extension
setup(name="set",
 ext_modules=[Extension("set", ["setobject.c"])])
--- NEW FILE: test_set.py ---
from set import set
import unittest
from test import test_support
class TestBasicOps(unittest.TestCase):
 def test_uniquification(self):
 s = set('abracadabra')
 result = list.sorted(s)
 self.assertEqual(result, list('abcdr'))
def test_main(verbose=None):
 from test import test_sets
 test_support.run_unittest(TestBasicOps)
if __name__ == "__main__":
 test_main(verbose=True)


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /