summaryrefslogtreecommitdiffstats
path: root/Objects/classobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1993-03-29 10:43:31 (GMT)
committerGuido van Rossum <guido@python.org>1993-03-29 10:43:31 (GMT)
commit9bfef44d97d1ae24e03717e3d59024b44626a9de (patch)
tree736b9f406d27f9dc4496f103cd395fb7a97401d4 /Objects/classobject.c
parent4b1302bd1d211881178618aa8f41fa4460180f2e (diff)
downloadcpython-9bfef44d97d1ae24e03717e3d59024b44626a9de.zip
cpython-9bfef44d97d1ae24e03717e3d59024b44626a9de.tar.gz
cpython-9bfef44d97d1ae24e03717e3d59024b44626a9de.tar.bz2
* Changed all copyright messages to include 1993.
* Stubs for faster implementation of local variables (not yet finished) * Added function name to code object. Print it for code and function objects. THIS MAKES THE .PYC FILE FORMAT INCOMPATIBLE (the version number has changed accordingly) * Print address of self for built-in methods * New internal functions getattro and setattro (getattr/setattr with string object arg) * Replaced "dictobject" with more powerful "mappingobject" * New per-type functio tp_hash to implement arbitrary object hashing, and hashobject() to interface to it * Added built-in functions hash(v) and hasattr(v, 'name') * classobject: made some functions static that accidentally weren't; added __hash__ special instance method to implement hash() * Added proper comparison for built-in methods and functions
Diffstat (limited to 'Objects/classobject.c')
-rw-r--r--Objects/classobject.c68
1 files changed, 60 insertions, 8 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 95b85f5..823e397 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -261,7 +261,7 @@ instance_setattr(inst, name, v)
return dictinsert(inst->in_attr, name, v);
}
-object *
+static object *
instance_repr(inst)
instanceobject *inst;
{
@@ -280,7 +280,7 @@ instance_repr(inst)
return res;
}
-int
+static int
instance_compare(inst, other)
instanceobject *inst, *other;
{
@@ -311,7 +311,43 @@ instance_compare(inst, other)
return outcome;
}
-int
+static long
+instance_hash(inst)
+ instanceobject *inst;
+{
+ object *func;
+ object *res;
+ int outcome;
+
+ func = instance_getattr(inst, "__hash__");
+ if (func == NULL) {
+ /* If there is no __cmp__ method, we hash on the address.
+ If a __cmp__ method exists, there must be a __hash__. */
+ err_clear();
+ func = instance_getattr(inst, "__cmp__");
+ if (func == NULL)
+ return (long)inst;
+ err_setstr(TypeError, "unhashable instance");
+ return -1;
+ }
+ res = call_object(func, (object *)NULL);
+ DECREF(func);
+ if (res == NULL)
+ return -1;
+ if (is_intobject(res)) {
+ outcome = getintvalue(res);
+ if (outcome == -1)
+ outcome = -2;
+ }
+ else {
+ err_setstr(TypeError, "__hash__() should return an int");
+ outcome = -1;
+ }
+ DECREF(res);
+ return outcome;
+}
+
+static int
instance_length(inst)
instanceobject *inst;
{
@@ -339,7 +375,7 @@ instance_length(inst)
return outcome;
}
-object *
+static object *
instance_subscript(inst, key)
instanceobject *inst;
object *key;
@@ -362,7 +398,7 @@ instance_subscript(inst, key)
return res;
}
-int
+static int
instance_ass_subscript(inst, key, value)
instanceobject*inst;
object *key;
@@ -395,7 +431,7 @@ instance_ass_subscript(inst, key, value)
return 0;
}
-mapping_methods instance_as_mapping = {
+static mapping_methods instance_as_mapping = {
instance_length, /*mp_length*/
instance_subscript, /*mp_subscript*/
instance_ass_subscript, /*mp_ass_subscript*/
@@ -612,7 +648,7 @@ UNARY(instance_neg, "__neg__")
UNARY(instance_pos, "__pos__")
UNARY(instance_abs, "__abs__")
-int
+static int
instance_nonzero(self)
instanceobject *self;
{
@@ -739,9 +775,10 @@ typeobject Instancetype = {
&instance_as_number, /*tp_as_number*/
&instance_as_sequence, /*tp_as_sequence*/
&instance_as_mapping, /*tp_as_mapping*/
+ instance_hash, /*tp_hash*/
};
-object *
+static object *
instance_convert(inst, methodname)
object *inst;
char *methodname;
@@ -837,6 +874,20 @@ instancemethod_compare(a, b)
return cmp;
}
+static long
+instancemethod_hash(a)
+ instancemethodobject *a;
+{
+ long x, y;
+ x = hashobject(a->im_self);
+ if (x == -1)
+ return -1;
+ y = hashobject(a->im_func);
+ if (y == -1)
+ return -1;
+ return x ^ y;
+}
+
typeobject Instancemethodtype = {
OB_HEAD_INIT(&Typetype)
0,
@@ -852,4 +903,5 @@ typeobject Instancemethodtype = {
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ instancemethod_hash, /*tp_hash*/
};