]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Added compare operations for functions and code objects.
authorGuido van Rossum <guido@python.org>
Fri, 5 Nov 1993 10:20:10 +0000 (10:20 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 5 Nov 1993 10:20:10 +0000 (10:20 +0000)
(Also hash, but it doesn't work yet.)

Objects/funcobject.c
Python/compile.c

index cf08af738d2c1cba58f280936c3cd54685cf1402..4dc0b9034d2129d274c0474ab7c8c014d191179a 100644 (file)
@@ -106,6 +106,27 @@ func_repr(op)
        return newstringobject(buf);
 }
 
+static int
+func_compare(f, g)
+       funcobject *f, *g;
+{
+       if (f->func_globals != g->func_globals)
+               return (f->func_globals < g->func_globals) ? -1 : 1;
+       return cmpobject(f->func_code, g->func_code);
+}
+
+static long
+func_hash(f)
+       funcobject *f;
+{
+       long h;
+       h = hashobject(f->func_code);
+       if (h == -1) return h;
+       h = h ^ (long)f->func_globals;
+       if (h == -1) h = -2;
+       return h;
+}
+
 typeobject Functype = {
        OB_HEAD_INIT(&Typetype)
        0,
@@ -116,6 +137,10 @@ typeobject Functype = {
        0,              /*tp_print*/
        func_getattr,   /*tp_getattr*/
        0,              /*tp_setattr*/
-       0,              /*tp_compare*/
+       func_compare,   /*tp_compare*/
        func_repr,      /*tp_repr*/
+       0,              /*tp_as_number*/
+       0,              /*tp_as_sequence*/
+       0,              /*tp_as_mapping*/
+       func_hash,      /*tp_hash*/
 };
index 3a6d1819c05f0e266c547fd9a53f90b149f7e55b..565c65cbfcdaf4bad4ebc95749b31b8b157b4e7c 100644 (file)
@@ -92,6 +92,35 @@ code_repr(co)
        return newstringobject(buf);
 }
 
+static int
+code_compare(co, cp)
+       codeobject *co, *cp;
+{
+       int cmp;
+       cmp = cmpobject((object *)co->co_code, (object *)cp->co_code);
+       if (cmp) return cmp;
+       cmp = cmpobject(co->co_consts, cp->co_consts);
+       if (cmp) return cmp;
+       cmp = cmpobject(co->co_names, cp->co_names);
+       return cmp;
+}
+
+static long
+code_hash(co)
+       codeobject *co;
+{
+       long h, h1, h2, h3;
+       h1 = hashobject((object *)co->co_code);
+       if (h1 == -1) return -1;
+       h2 = hashobject(co->co_consts);
+       if (h2 == -1) return -1;
+       h3 = hashobject(co->co_names);
+       if (h3 == -1) return -1;
+       h = h1 ^ h2 ^ h3;
+       if (h == -1) h = -2;
+       return h;
+}
+
 typeobject Codetype = {
        OB_HEAD_INIT(&Typetype)
        0,
@@ -102,11 +131,12 @@ typeobject Codetype = {
        0,              /*tp_print*/
        code_getattr,   /*tp_getattr*/
        0,              /*tp_setattr*/
-       0,              /*tp_compare*/
+       code_compare,   /*tp_compare*/
        code_repr,      /*tp_repr*/
        0,              /*tp_as_number*/
        0,              /*tp_as_sequence*/
        0,              /*tp_as_mapping*/
+       code_hash,      /*tp_hash*/
 };
 
 codeobject *