[Python-checkins] CVS: python/dist/src/Objects classobject.c,2.82,2.83
Guido van Rossum
guido@cnri.reston.va.us
Mon, 28 Feb 2000 10:03:17 -0500 (EST)
- Previous message: [Python-checkins] CVS: python/dist/src/Objects abstract.c,2.30,2.31
- Next message: [Python-checkins] CVS: python/dist/src/Lib StringIO.py,1.8,1.9 cgi.py,1.46,1.47 code.py,1.8,1.9 ftplib.py,1.40,1.41 getpass.py,1.8,1.9 imaplib.py,1.16,1.17 poplib.py,1.6,1.7 smtplib.py,1.21,1.22 whrandom.py,1.15,1.16
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
Update of /projects/cvsroot/python/dist/src/Objects
In directory eric:/projects/python/develop/guido/src/Objects
Modified Files:
classobject.c
Log Message:
Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
an instance method instance_contains as sq_contains. It looks for
__contains__ and if not found falls back to previous behaviour.
Done.
Index: classobject.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/classobject.c,v
retrieving revision 2.82
retrieving revision 2.83
diff -C2 -r2.82 -r2.83
*** classobject.c 2000/01/20 22:32:54 2.82
--- classobject.c 2000/02/28 15:03:15 2.83
***************
*** 1066,1069 ****
--- 1066,1122 ----
}
+ static int instance_contains(PyInstanceObject *inst, PyObject *member)
+ {
+ static PyObject *__contains__;
+ PyObject *func, *arg, *res;
+ int ret;
+
+ if(__contains__ == NULL) {
+ __contains__ = PyString_InternFromString("__contains__");
+ if(__contains__ == NULL)
+ return -1;
+ }
+ func = instance_getattr(inst, __contains__);
+ if(func == NULL) {
+ /* fall back to previous behaviour */
+ int i, cmp_res;
+
+ if(!PyErr_ExceptionMatches(PyExc_AttributeError))
+ return -1;
+ PyErr_Clear();
+ for(i=0;;i++) {
+ PyObject *obj = instance_item(inst, i);
+ int ret = 0;
+
+ if(obj == NULL) {
+ if(!PyErr_ExceptionMatches(PyExc_IndexError))
+ return -1;
+ PyErr_Clear();
+ return 0;
+ }
+ if(PyObject_Cmp(obj, member, &cmp_res) == -1)
+ ret = -1;
+ if(cmp_res == 0)
+ ret = 1;
+ Py_DECREF(obj);
+ if(ret)
+ return ret;
+ }
+ }
+ arg = Py_BuildValue("(O)", member);
+ if(arg == NULL) {
+ Py_DECREF(func);
+ return -1;
+ }
+ res = PyEval_CallObject(func, arg);
+ Py_DECREF(func);
+ Py_DECREF(arg);
+ if(res == NULL)
+ return -1;
+ ret = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ return ret;
+ }
+
static PySequenceMethods instance_as_sequence = {
(inquiry)instance_length, /*sq_length*/
***************
*** 1074,1077 ****
--- 1127,1131 ----
(intobjargproc)instance_ass_item, /*sq_ass_item*/
(intintobjargproc)instance_ass_slice, /*sq_ass_slice*/
+ (objobjproc)instance_contains, /* sq_contains */
};
***************
*** 1406,1409 ****
--- 1460,1465 ----
(getattrofunc)instance_getattr, /*tp_getattro*/
(setattrofunc)instance_setattr, /*tp_setattro*/
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /*tp_flags */
};
- Previous message: [Python-checkins] CVS: python/dist/src/Objects abstract.c,2.30,2.31
- Next message: [Python-checkins] CVS: python/dist/src/Lib StringIO.py,1.8,1.9 cgi.py,1.46,1.47 code.py,1.8,1.9 ftplib.py,1.40,1.41 getpass.py,1.8,1.9 imaplib.py,1.16,1.17 poplib.py,1.6,1.7 smtplib.py,1.21,1.22 whrandom.py,1.15,1.16
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]