diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2009-02-01 10:28:51 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2009-02-01 10:28:51 (GMT) |
commit | 211c6258294bf683935bff73a61ce3dd84070988 (patch) | |
tree | 8dc2a5a5fdcd1f7bc0e176bce006b12e1b588a05 /Modules | |
parent | 776e7014e97a79cc2bd5fffd15908e83ff0273cf (diff) | |
download | cpython-211c6258294bf683935bff73a61ce3dd84070988.zip cpython-211c6258294bf683935bff73a61ce3dd84070988.tar.gz cpython-211c6258294bf683935bff73a61ce3dd84070988.tar.bz2 |
Issue #1717, stage 2: remove uses of tp_compare in Modules and most
Objects.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_csv.c | 6 | ||||
-rw-r--r-- | Modules/_elementtree.c | 9 | ||||
-rw-r--r-- | Modules/_localemodule.c | 4 | ||||
-rw-r--r-- | Modules/_pickle.c | 2 | ||||
-rw-r--r-- | Modules/_tkinter.c | 134 | ||||
-rw-r--r-- | Modules/parsermodule.c | 72 | ||||
-rw-r--r-- | Modules/pyexpat.c | 2 |
7 files changed, 161 insertions, 68 deletions
diff --git a/Modules/_csv.c b/Modules/_csv.c index 430ccdc..5175e71 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -443,7 +443,7 @@ static PyTypeObject Dialect_Type = { (printfunc)0, /* tp_print */ (getattrfunc)0, /* tp_getattr */ (setattrfunc)0, /* tp_setattr */ - (cmpfunc)0, /* tp_compare */ + 0, /* tp_compare */ (reprfunc)0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -864,7 +864,7 @@ static PyTypeObject Reader_Type = { (printfunc)0, /*tp_print*/ (getattrfunc)0, /*tp_getattr*/ (setattrfunc)0, /*tp_setattr*/ - (cmpfunc)0, /*tp_compare*/ + 0, /*tp_compare*/ (reprfunc)0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ @@ -1286,7 +1286,7 @@ static PyTypeObject Writer_Type = { (printfunc)0, /*tp_print*/ (getattrfunc)0, /*tp_getattr*/ (setattrfunc)0, /*tp_setattr*/ - (cmpfunc)0, /*tp_compare*/ + 0, /*tp_compare*/ (reprfunc)0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 767c6ee..46c19eb 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -761,7 +761,7 @@ element_find(ElementObject* self, PyObject* args) for (i = 0; i < self->extra->length; i++) { PyObject* item = self->extra->children[i]; if (Element_CheckExact(item) && - PyObject_Compare(((ElementObject*)item)->tag, tag) == 0) { + PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ) == 1) { Py_INCREF(item); return item; } @@ -792,7 +792,8 @@ element_findtext(ElementObject* self, PyObject* args) for (i = 0; i < self->extra->length; i++) { ElementObject* item = (ElementObject*) self->extra->children[i]; - if (Element_CheckExact(item) && !PyObject_Compare(item->tag, tag)) { + if (Element_CheckExact(item) && (PyObject_RichCompareBool(item->tag, tag, Py_EQ) == 1)) { + PyObject* text = element_get_text(item); if (text == Py_None) return PyBytes_FromString(""); @@ -830,7 +831,7 @@ element_findall(ElementObject* self, PyObject* args) for (i = 0; i < self->extra->length; i++) { PyObject* item = self->extra->children[i]; if (Element_CheckExact(item) && - PyObject_Compare(((ElementObject*)item)->tag, tag) == 0) { + PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ) == 1) { if (PyList_Append(out, item) < 0) { Py_DECREF(out); return NULL; @@ -1102,7 +1103,7 @@ element_remove(ElementObject* self, PyObject* args) for (i = 0; i < self->extra->length; i++) { if (self->extra->children[i] == element) break; - if (PyObject_Compare(self->extra->children[i], element) == 0) + if (PyObject_RichCompareBool(self->extra->children[i], element, Py_EQ) == 1) break; } diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c index fb2837e..432df36 100644 --- a/Modules/_localemodule.c +++ b/Modules/_localemodule.c @@ -272,7 +272,9 @@ PyLocale_strcoll(PyObject* self, PyObject* args) #ifdef HAVE_WCSXFRM PyDoc_STRVAR(strxfrm__doc__, -"string -> string. Returns a string that behaves for cmp locale-aware."); +"strxfrm(string) -> string.\n\ +\n\ +Return a string that can be used as a key for locale-aware comparisons."); static PyObject* PyLocale_strxfrm(PyObject* self, PyObject* args) diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 5813fd4..1036367 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -715,7 +715,7 @@ whichmodule(PyObject *global, PyObject *global_name) i = 0; module_name = NULL; while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) { - if (PyObject_Compare(module_name, main_str) == 0) + if (PyObject_RichCompareBool(module_name, main_str, Py_EQ) == 1) continue; obj = PyObject_GetAttr(module, global_name); diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 6e595cc..01d1069 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -788,15 +788,59 @@ PyTclObject_repr(PyTclObject *self) self->value->typePtr->name, self->value); } -static int -PyTclObject_cmp(PyTclObject *self, PyTclObject *other) +#define TEST_COND(cond) ((cond) ? Py_True : Py_False) + +static PyObject * +PyTclObject_richcompare(PyObject *self, PyObject *other, int op) { - int res; - res = strcmp(Tcl_GetString(self->value), - Tcl_GetString(other->value)); - if (res < 0) return -1; - if (res > 0) return 1; - return 0; + int result; + PyObject *v; + + /* neither argument should be NULL, unless something's gone wrong */ + if (self == NULL || other == NULL) { + PyErr_BadInternalCall(); + return NULL; + } + + /* both arguments should be instances of PyTclObject */ + if (!PyTclObject_Check(self) || !PyTclObject_Check(other)) { + v = Py_NotImplemented; + goto finished; + } + + if (self == other) + /* fast path when self and other are identical */ + result = 0; + else + result = strcmp(Tcl_GetString(((PyTclObject *)self)->value), + Tcl_GetString(((PyTclObject *)other)->value)); + /* Convert return value to a Boolean */ + switch (op) { + case Py_EQ: + v = TEST_COND(result == 0); + break; + case Py_NE: + v = TEST_COND(result != 0); + break; + case Py_LE: + v = TEST_COND(result <= 0); + break; + case Py_GE: + v = TEST_COND(result >= 0); + break; + case Py_LT: + v = TEST_COND(result < 0); + break; + case Py_GT: + v = TEST_COND(result > 0); + break; + default: + PyErr_BadArgument(); + return NULL; + } + finished: + Py_INCREF(v); + return v; } PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type"); @@ -818,45 +862,45 @@ static PyGetSetDef PyTclObject_getsetlist[] = { static PyTypeObject PyTclObject_Type = { PyVarObject_HEAD_INIT(NULL, 0) "_tkinter.Tcl_Obj", /*tp_name*/ - sizeof(PyTclObject), /*tp_basicsize*/ - 0, /*tp_itemsize*/ + sizeof(PyTclObject), /*tp_basicsize*/ + 0, /*tp_itemsize*/ /* methods */ - (destructor)PyTclObject_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - (cmpfunc)PyTclObject_cmp, /*tp_compare*/ + (destructor)PyTclObject_dealloc,/*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ (reprfunc)PyTclObject_repr, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - 0, /*tp_call*/ - (reprfunc)PyTclObject_str, /*tp_str*/ - PyObject_GenericGetAttr,/*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ - 0, /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - 0, /*tp_methods*/ - 0, /*tp_members*/ - PyTclObject_getsetlist, /*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*/ - 0, /*tp_new*/ - 0, /*tp_free*/ - 0, /*tp_is_gc*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + (reprfunc)PyTclObject_str, /*tp_str*/ + PyObject_GenericGetAttr, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + PyTclObject_richcompare, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + 0, /*tp_methods*/ + 0, /*tp_members*/ + PyTclObject_getsetlist, /*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*/ + 0, /*tp_new*/ + 0, /*tp_free*/ + 0, /*tp_is_gc*/ }; static Tcl_Obj* diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c index 6778fb4..32de6bc 100644 --- a/Modules/parsermodule.c +++ b/Modules/parsermodule.c @@ -169,7 +169,7 @@ typedef struct { static void parser_free(PyST_Object *st); -static int parser_compare(PyST_Object *left, PyST_Object *right); +static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op); static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *); static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *); static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *); @@ -203,7 +203,7 @@ PyTypeObject PyST_Type = { 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ - (cmpfunc)parser_compare, /* tp_compare */ + 0, /* tp_compare */ 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -223,7 +223,7 @@ PyTypeObject PyST_Type = { "Intermediate representation of a Python parse tree.", 0, /* tp_traverse */ 0, /* tp_clear */ - 0, /* tp_richcompare */ + parser_richcompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ @@ -231,6 +231,9 @@ PyTypeObject PyST_Type = { }; /* PyST_Type */ +/* PyST_Type isn't subclassable, so just check ob_type */ +#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type) + static int parser_compare_nodes(node *left, node *right) { @@ -260,26 +263,69 @@ parser_compare_nodes(node *left, node *right) return (0); } - -/* int parser_compare(PyST_Object* left, PyST_Object* right) +/* parser_richcompare(PyObject* left, PyObject* right, int op) * * Comparison function used by the Python operators ==, !=, <, >, <=, >= * This really just wraps a call to parser_compare_nodes() with some easy * checks and protection code. * */ -static int -parser_compare(PyST_Object *left, PyST_Object *right) + +#define TEST_COND(cond) ((cond) ? Py_True : Py_False) + +static PyObject * +parser_richcompare(PyObject *left, PyObject *right, int op) { - if (left == right) - return (0); + int result; + PyObject *v; - if ((left == 0) || (right == 0)) - return (-1); + /* neither argument should be NULL, unless something's gone wrong */ + if (left == NULL || right == NULL) { + PyErr_BadInternalCall(); + return NULL; + } - return (parser_compare_nodes(left->st_node, right->st_node)); -} + /* both arguments should be instances of PyST_Object */ + if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) { + v = Py_NotImplemented; + goto finished; + } + if (left == right) + /* if arguments are identical, they're equal */ + result = 0; + else + result = parser_compare_nodes(((PyST_Object *)left)->st_node, + ((PyST_Object *)right)->st_node); + + /* Convert return value to a Boolean */ + switch (op) { + case Py_EQ: + v = TEST_COND(result == 0); + break; + case Py_NE: + v = TEST_COND(result != 0); + break; + case Py_LE: + v = TEST_COND(result <= 0); + break; + case Py_GE: + v = TEST_COND(result >= 0); + break; + case Py_LT: + v = TEST_COND(result < 0); + break; + case Py_GT: + v = TEST_COND(result > 0); + break; + default: + PyErr_BadArgument(); + return NULL; + } + finished: + Py_INCREF(v); + return v; +} /* parser_newstobject(node* st) * diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index df37589..ce72885 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1622,7 +1622,7 @@ static PyTypeObject Xmlparsetype = { (printfunc)0, /*tp_print*/ 0, /*tp_getattr*/ (setattrfunc)xmlparse_setattr, /*tp_setattr*/ - (cmpfunc)0, /*tp_compare*/ + 0, /*tp_compare*/ (reprfunc)0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ |