[Python-checkins] CVS: python/dist/src/Objects dictobject.c,2.110,2.111

Tim Peters tim_one@users.sourceforge.net
2001年9月02日 01:22:50 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv12216/python/dist/src/Objects
Modified Files:
	dictobject.c 
Log Message:
Make dictionary() a real constructor. Accepts at most one argument, "a
mapping object", in the same sense dict.update(x) requires of x (that x
has a keys() method and a getitem).
Questionable: The other type constructors accept a keyword argument, so I
did that here too (e.g., dictionary(mapping={1:2}) works). But type_call
doesn't pass the keyword args to the tp_new slot (it passes NULL), it only
passes them to the tp_init slot, so getting at them required adding a
tp_init slot to dicts. Looks like that makes the normal case (i.e., no
args at all) a little slower (the time it takes to call dict.tp_init and
have it figure out there's nothing to do).
Index: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.110
retrieving revision 2.111
diff -C2 -d -r2.110 -r2.111
*** dictobject.c	2001年08月29日 23:51:52	2.110
--- dictobject.c	2001年09月02日 08:22:48	2.111
***************
*** 1693,1696 ****
--- 1693,1719 ----
 }
 
+ static int
+ dict_init(PyObject *self, PyObject *args, PyObject *kwds)
+ {
+ 	PyObject *arg = NULL;
+ 	static char *kwlist[] = {"mapping", 0};
+ 
+ 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:dictionary",
+ 					 kwlist, &arg))
+ 		return -1;
+ 	if (arg != NULL) {
+ 		if (PyDict_Merge(self, arg, 1) < 0) {
+ 			/* An error like "AttibuteError: keys" is too
+ 			 cryptic in this context. */
+ 			if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ 				PyErr_SetString(PyExc_TypeError,
+ 					"argument must be of a mapping type");
+ 			}
+ 			return -1;
+ 		}
+ 	}
+ 	return 0;
+ }
+ 
 static PyObject *
 dict_iter(dictobject *dict)
***************
*** 1699,1702 ****
--- 1722,1729 ----
 }
 
+ static char dictionary_doc[] =
+ "dictionary() -> new empty dictionary\n"
+ "dictionary(mapping) -> new dict initialized from mapping's key+value pairs";
+ 
 PyTypeObject PyDict_Type = {
 	PyObject_HEAD_INIT(&PyType_Type)
***************
*** 1722,1726 ****
 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
 		Py_TPFLAGS_BASETYPE,		/* tp_flags */
! 	"dictionary type",			/* tp_doc */
 	(traverseproc)dict_traverse,		/* tp_traverse */
 	(inquiry)dict_tp_clear,			/* tp_clear */
--- 1749,1753 ----
 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
 		Py_TPFLAGS_BASETYPE,		/* tp_flags */
! 	dictionary_doc,				/* tp_doc */
 	(traverseproc)dict_traverse,		/* tp_traverse */
 	(inquiry)dict_tp_clear,			/* tp_clear */
***************
*** 1737,1741 ****
 	0,					/* tp_descr_set */
 	0,					/* tp_dictoffset */
! 	0,					/* tp_init */
 	PyType_GenericAlloc,			/* tp_alloc */
 	dict_new,				/* tp_new */
--- 1764,1768 ----
 	0,					/* tp_descr_set */
 	0,					/* tp_dictoffset */
! 	(initproc)dict_init,			/* tp_init */
 	PyType_GenericAlloc,			/* tp_alloc */
 	dict_new,				/* tp_new */

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