Commit b6bb0c79 authored by Guido van Rossum's avatar Guido van Rossum

Implement == and != comparisons for code objects by value.

This makes test_codeop and test_marshal pass.
parent 8f78fe9a
...@@ -294,44 +294,60 @@ code_repr(PyCodeObject *co) ...@@ -294,44 +294,60 @@ code_repr(PyCodeObject *co)
static PyObject * static PyObject *
code_richcompare(PyObject *self, PyObject *other, int op) code_richcompare(PyObject *self, PyObject *other, int op)
{ {
/* Temporarily make this unsupported */ PyCodeObject *co, *cp;
_Py_Break(); int eq;
Py_INCREF(Py_NotImplemented); PyObject *res;
return Py_NotImplemented;
if ((op != Py_EQ && op != Py_NE) ||
#if 0 !PyCode_Check(self) ||
int cmp; !PyCode_Check(other)) {
cmp = PyObject_Compare(co->co_name, cp->co_name); Py_INCREF(Py_NotImplemented);
if (cmp) return cmp; return Py_NotImplemented;
cmp = co->co_argcount - cp->co_argcount; }
if (cmp) goto normalize;
cmp = co->co_nlocals - cp->co_nlocals; co = (PyCodeObject *)self;
if (cmp) goto normalize; cp = (PyCodeObject *)other;
cmp = co->co_flags - cp->co_flags;
if (cmp) goto normalize; eq = PyObject_RichCompare(co->co_name, cp->co_name, Py_EQ);
cmp = co->co_firstlineno - cp->co_firstlineno; if (eq <= 0) goto unequal;
if (cmp) goto normalize; eq = co->co_argcount == cp->co_argcount;
cmp = PyObject_Compare(co->co_code, cp->co_code); if (!eq) goto unequal;
if (cmp) return cmp; eq = co->co_nlocals == cp->co_nlocals;
cmp = PyObject_Compare(co->co_consts, cp->co_consts); if (!eq) goto unequal;
if (cmp) return cmp; eq = co->co_flags == cp->co_flags;
cmp = PyObject_Compare(co->co_names, cp->co_names); if (!eq) goto unequal;
if (cmp) return cmp; eq = co->co_firstlineno == cp->co_firstlineno;
cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); if (!eq) goto unequal;
if (cmp) return cmp; eq = PyObject_RichCompare(co->co_code, cp->co_code, Py_EQ);
cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); if (eq <= 0) goto unequal;
if (cmp) return cmp; eq = PyObject_RichCompare(co->co_consts, cp->co_consts, Py_EQ);
cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); if (eq <= 0) goto unequal;
return cmp; eq = PyObject_RichCompare(co->co_names, cp->co_names, Py_EQ);
if (eq <= 0) goto unequal;
normalize: eq = PyObject_RichCompare(co->co_varnames, cp->co_varnames, Py_EQ);
if (cmp > 0) if (eq <= 0) goto unequal;
return 1; eq = PyObject_RichCompare(co->co_freevars, cp->co_freevars, Py_EQ);
else if (cmp < 0) if (eq <= 0) goto unequal;
return -1; eq = PyObject_RichCompare(co->co_cellvars, cp->co_cellvars, Py_EQ);
if (eq <= 0) goto unequal;
if (op == Py_EQ)
res = Py_True;
else
res = Py_False;
goto done;
unequal:
if (eq < 0)
return NULL;
if (op == Py_NE)
res = Py_True;
else else
return 0; res = Py_False;
#endif
done:
Py_INCREF(res);
return res;
} }
static long static long
...@@ -375,7 +391,7 @@ PyTypeObject PyCode_Type = { ...@@ -375,7 +391,7 @@ PyTypeObject PyCode_Type = {
0, /* tp_as_number */ 0, /* tp_as_number */
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ (hashfunc)code_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
...@@ -385,7 +401,7 @@ PyTypeObject PyCode_Type = { ...@@ -385,7 +401,7 @@ PyTypeObject PyCode_Type = {
code_doc, /* tp_doc */ code_doc, /* tp_doc */
0, /* tp_traverse */ 0, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ code_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment