[Python-checkins] CVS: python/dist/src/Objects typeobject.c,2.16.8.5,2.16.8.6
Guido van Rossum
gvanrossum@users.sourceforge.net
Mon, 30 Apr 2001 07:27:05 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv29176
Modified Files:
Tag: descr-branch
typeobject.c
Log Message:
Add wrappers for everything reasonable, except coerce().
The tp_call wrapper currently doesn't support keyword arguments,
because the wrapper object doesn't. :-(
Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.16.8.5
retrieving revision 2.16.8.6
diff -C2 -r2.16.8.5 -r2.16.8.6
*** typeobject.c 2001/04/30 01:14:56 2.16.8.5
--- typeobject.c 2001/04/30 14:27:03 2.16.8.6
***************
*** 521,524 ****
--- 521,525 ----
{0}
};
+
static PyObject *
wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
***************
*** 543,546 ****
--- 544,666 ----
};
+ static PyObject *
+ wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
+ {
+ cmpfunc func = (cmpfunc)wrapped;
+ int res;
+ PyObject *other;
+
+ if (!PyArg_ParseTuple(args, "O", &other))
+ return NULL;
+ res = (*func)(self, other);
+ if (PyErr_Occurred())
+ return NULL;
+ return PyInt_FromLong((long)res);
+ }
+
+ static struct wrapperbase tab_cmp[] = {
+ {"__cmp__", (wrapperfunc)wrap_cmpfunc,
+ "x.__cmp__(y) <==> cmp(x,y)"},
+ {0}
+ };
+
+ static struct wrapperbase tab_repr[] = {
+ {"__repr__", (wrapperfunc)wrap_unaryfunc,
+ "x.__repr__() <==> repr(x)"},
+ {0}
+ };
+
+ static PyObject *
+ wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
+ {
+ hashfunc func = (hashfunc)wrapped;
+ long res;
+
+ if (!PyArg_ParseTuple(args, ""))
+ return NULL;
+ res = (*func)(self);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ return PyInt_FromLong(res);
+ }
+
+ static struct wrapperbase tab_hash[] = {
+ {"__hash__", (wrapperfunc)wrap_hashfunc,
+ "x.__hash__() <==> hash(x)"},
+ {0}
+ };
+
+ static PyObject *
+ wrap_call(PyObject *self, PyObject *args, void *wrapped)
+ {
+ ternaryfunc func = (ternaryfunc)wrapped;
+
+ /* XXX What about keyword arguments? */
+ return (*func)(self, args, NULL);
+ }
+
+ static struct wrapperbase tab_call[] = {
+ {"__call__", (wrapperfunc)wrap_call,
+ "x.__call__(...) <==> x(...)"},
+ {0}
+ };
+
+ static struct wrapperbase tab_str[] = {
+ {"__str__", (wrapperfunc)wrap_unaryfunc,
+ "x.__str__() <==> str(x)"},
+ {0}
+ };
+
+ static PyObject *
+ wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
+ {
+ richcmpfunc func = (richcmpfunc)wrapped;
+ PyObject *other;
+
+ if (!PyArg_ParseTuple(args, "O", &other))
+ return NULL;
+ return (*func)(self, other, op);
+ }
+
+ #undef RICHCMP_WRAPPER
+ #define RICHCMP_WRAPPER(NAME, OP) \
+ static PyObject * \
+ richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
+ { \
+ return wrap_richcmpfunc(self, args, wrapped, OP); \
+ }
+
+ RICHCMP_WRAPPER(lt, Py_LT);
+ RICHCMP_WRAPPER(le, Py_LE);
+ RICHCMP_WRAPPER(eq, Py_EQ);
+ RICHCMP_WRAPPER(ne, Py_NE);
+ RICHCMP_WRAPPER(gt, Py_GT);
+ RICHCMP_WRAPPER(ge, Py_GE);
+
+ #undef RICHCMP_ENTRY
+ #define RICHCMP_ENTRY(NAME, EXPR) \
+ {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
+ "x.__" #NAME "__(y) <==> " EXPR}
+
+ static struct wrapperbase tab_richcmp[] = {
+ RICHCMP_ENTRY(lt, "x<y"),
+ RICHCMP_ENTRY(le, "x<=y"),
+ RICHCMP_ENTRY(eq, "x==y"),
+ RICHCMP_ENTRY(ne, "x!=y"),
+ RICHCMP_ENTRY(gt, "x>y"),
+ RICHCMP_ENTRY(ge, "x>=y"),
+ {0}
+ };
+
+ static struct wrapperbase tab_iter[] = {
+ {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
+ {0}
+ };
+
+ static struct wrapperbase tab_next[] = {
+ {"next", (wrapperfunc)wrap_unaryfunc, "x.next() -> next value"},
+ {0}
+ };
+
static int
add_operators(PyTypeObject *type)
***************
*** 577,580 ****
--- 697,703 ----
}
+ /* We don't support "old-style numbers" because their binary
+ operators require that both arguments have the same type;
+ the wrappers here only work for new-style numbers. */
if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
(nb = type->tp_as_number) != NULL) {
***************
*** 596,599 ****
--- 719,723 ----
ADD(nb->nb_xor, tab_xor);
ADD(nb->nb_or, tab_or);
+ /* We don't support coerce() -- see above comment */
ADD(nb->nb_int, tab_int);
ADD(nb->nb_long, tab_long);
***************
*** 614,618 ****
}
! /* XXX Slots in the type object itself, e.g. tp_str, tp_repr, etc. */
return 0;
--- 738,750 ----
}
! /* Not yet supported: __getattr__, __setattr__ */
! ADD(type->tp_compare, tab_cmp);
! ADD(type->tp_repr, tab_repr);
! ADD(type->tp_hash, tab_hash);
! ADD(type->tp_call, tab_call);
! ADD(type->tp_str, tab_str);
! ADD(type->tp_richcompare, tab_richcmp);
! ADD(type->tp_iter, tab_iter);
! ADD(type->tp_iternext, tab_next);
return 0;