/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/* An intermediate code fragment contains:
- a string that encodes the instructions,
- a list of the constants,
- - and a list of the names used. */
+ - a list of the names used,
+ - the filename from which it was compiled,
+ - the name of the object for which it was compiled. */
typedef struct {
OB_HEAD
object *co_consts; /* list of immutable constant objects */
object *co_names; /* list of stringobjects */
object *co_filename; /* string */
+ object *co_name; /* string */
} codeobject;
extern typeobject Codetype;
/* Public interface */
struct _node; /* Declare the existence of this type */
codeobject *compile PROTO((struct _node *, char *));
-codeobject *newcodeobject PROTO((object *, object *, object *, object *));
+codeobject *newcodeobject
+ PROTO((object *, object *, object *, object *, object *));
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
******************************************************************/
-/*
-Dictionary object type -- mapping from char * to object.
-NB: the key is given as a char *, not as a stringobject.
-These functions set errno for errors. Functions dictremove() and
-dictinsert() return nonzero for errors, getdictsize() returns -1,
-the others NULL. A successful call to dictinsert() calls INCREF()
-for the inserted item.
-*/
+/* All in the sake of backward compatibility... */
-extern typeobject Dicttype;
+#include "mappingobject.h"
-#define is_dictobject(op) ((op)->ob_type == &Dicttype)
+#define is_dictobject(op) is_mappingobject(op)
+
+#define newdictobject newmappingobject
-extern object *newdictobject PROTO((void));
extern object *dictlookup PROTO((object *dp, char *key));
extern int dictinsert PROTO((object *dp, char *key, object *item));
extern int dictremove PROTO((object *dp, char *key));
-extern int getdictsize PROTO((object *dp));
extern char *getdictkey PROTO((object *dp, int i));
-extern object *getdictkeys PROTO((object *dp));
-extern object *getdict2key PROTO((object *dp, int i));
-extern object *dict2lookup PROTO((object *dp, object *key));
-extern int dict2insert PROTO((object *dp, object *key, object *item));
-extern int dict2remove PROTO((object *dp, object *key));
+#define getdictsize getmappingsize
+#define getdictkeys getmappingkeys
+
+#define getdict2key getmappingkey
+#define dict2lookup mappinglookup
+#define dict2insert mappinginsert
+#define dict2remove mappingremove
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
number_methods *tp_as_number;
sequence_methods *tp_as_sequence;
mapping_methods *tp_as_mapping;
+
+ /* More standard operations (at end for binary compatibility) */
+
+ long (*tp_hash) FPROTO((object *));
} typeobject;
extern typeobject Typetype; /* The type of type objects */
extern object * reprobject PROTO((object *));
extern int cmpobject PROTO((object *, object *));
extern object *getattr PROTO((object *, char *));
-extern int setattr PROTO((object *, char *, object *));
+extern object *getattro PROTO((object *, object *));
+extern int setattro PROTO((object *, object *, object *));
+extern long hashobject PROTO((object *));
/* Flag bits for printing: */
#define PRINT_RAW 1 /* No string quotes etc. */
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
#define LOAD_LOCAL 115 /* Index in name list */
#define LOAD_GLOBAL 116 /* Index in name list */
+#define LOAD_FAST 117 /* Local variable number */
+#define STORE_FAST 118 /* Local variable number */
+#define RESERVE_FAST 119 /* Number of local variables */
+
#define SETUP_LOOP 120 /* Target address (absolute) */
#define SETUP_EXCEPT 121 /* "" */
#define SETUP_FINALLY 122 /* "" */
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/**********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
#include "patchlevel.h"
-#define VERSION "0.9.%d (%s)"
+#define VERSION "0.9.%d BETA (%s)"
#ifdef __DATE__
#define DATE __DATE__
#else
-#define DATE ">= 11 Jan 1993"
+#define DATE ">= 27 Mar 1993"
#endif
#ifdef USE_STDWIN
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
{
err_setstr(ImgfileError, str);
error_called = 1;
- return; /* To imglib, which will return a failure indictaor */
+ return; /* To imglib, which will return a failure indicator */
}
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
return dictinsert(inst->in_attr, name, v);
}
-object *
+static object *
instance_repr(inst)
instanceobject *inst;
{
return res;
}
-int
+static int
instance_compare(inst, other)
instanceobject *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;
{
return outcome;
}
-object *
+static object *
instance_subscript(inst, key)
instanceobject *inst;
object *key;
return res;
}
-int
+static int
instance_ass_subscript(inst, key, value)
instanceobject*inst;
object *key;
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*/
UNARY(instance_pos, "__pos__")
UNARY(instance_abs, "__abs__")
-int
+static int
instance_nonzero(self)
instanceobject *self;
{
&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;
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,
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ instancemethod_hash, /*tp_hash*/
};
/* Mapping object implementation; using a hash table */
+/* This file should really be called "dictobject.c", since "mapping"
+ is the generic name for objects with an unorderred arbitrary key
+ set (just like lists are sequences), but since it improves (and was
+ originally derived from) a file by that name I had to change its
+ name. For the user these objects are still called "dictionaries". */
+
#include "allobjects.h"
-#include "mappingobject.h"
#include "modsupport.h"
typeobject Mappingtype = {
OB_HEAD_INIT(&Typetype)
0,
- "mapping",
+ "dictionary",
sizeof(mappingobject),
0,
mapping_dealloc, /*tp_dealloc*/
return (i < j) ? -1 : (i > j) ? 1 : 0;
}
+static long
+float_hash(v)
+ floatobject *v;
+{
+ double intpart, fractpart;
+ int expo;
+ long x;
+ /* This is designed so that Python numbers with the same
+ value hash to the same value, otherwise comparisons
+ of mapping keys will turn out weird */
+ fractpart = modf(v->ob_fval, &intpart);
+ if (fractpart == 0.0) {
+ if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
+ /* Convert to long int and use its hash... */
+ object *w = dnewlongobject(v->ob_fval);
+ if (w == NULL)
+ return -1;
+ x = hashobject(w);
+ DECREF(w);
+ return x;
+ }
+ x = (long)intpart;
+ }
+ else {
+ fractpart = frexp(fractpart, &expo);
+ x = (long) (intpart + fractpart) ^ expo; /* Rather arbitrary */
+ }
+ if (x == -1)
+ x = -2;
+ return x;
+}
+
static object *
float_add(v, w)
floatobject *v;
&float_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ float_hash, /*tp_hash */
};
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/* Function object implementation */
#include "allobjects.h"
-
+#include "compile.h"
#include "structmember.h"
typedef struct {
DEL(op);
}
+static object*
+func_repr(op)
+ funcobject *op;
+{
+ char buf[140];
+ sprintf(buf, "<function %.100s at %lx>",
+ getstringvalue(((codeobject*)(op->func_code))->co_name),
+ (long)op);
+ return newstringobject(buf);
+}
+
typeobject Functype = {
OB_HEAD_INIT(&Typetype)
0,
func_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
- 0, /*tp_repr*/
+ func_repr, /*tp_repr*/
};
return (i < j) ? -1 : (i > j) ? 1 : 0;
}
+static long
+int_hash(v)
+ intobject *v;
+{
+ long x = v -> ob_ival;
+ if (x == -1)
+ x = -2;
+ return x;
+}
+
static object *
int_add(v, w)
intobject *v;
static object *
int_int(v)
- object *v;
+ intobject *v;
{
INCREF(v);
return v;
static object *
int_long(v)
- object *v;
+ intobject *v;
{
- long x = getintvalue(v);
- return newlongobject(x);
+ return newlongobject((v -> ob_ival));
}
static object *
int_float(v)
- object *v;
+ intobject *v;
{
- long x = getintvalue(v);
- return newfloatobject((double)x);
+ return newfloatobject((double)(v -> ob_ival));
}
static object *
int_oct(v)
- object *v;
+ intobject *v;
{
char buf[20];
- long x = getintvalue(v);
+ long x = v -> ob_ival;
if (x == 0)
strcpy(buf, "0");
else if (x > 0)
static object *
int_hex(v)
- object *v;
+ intobject *v;
{
char buf[20];
- long x = getintvalue(v);
+ long x = v -> ob_ival;
if (x >= 0)
sprintf(buf, "0x%lx", x);
else
return newstringobject(buf);
}
-
static number_methods int_as_number = {
int_add, /*nb_add*/
int_sub, /*nb_subtract*/
&int_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ &int_hash, /*tp_hash*/
};
static int long_print PROTO((object *, FILE *, int));
static object *long_repr PROTO((object *));
static int long_compare PROTO((longobject *, longobject *));
+static long long_hash PROTO((longobject *));
static object *long_add PROTO((longobject *, longobject *));
static object *long_sub PROTO((longobject *, longobject *));
return sign < 0 ? -1 : sign > 0 ? 1 : 0;
}
+static long
+long_hash(v)
+ longobject *v;
+{
+ long x;
+ int i, sign;
+
+ /* This is designed so that Python ints and longs with the
+ same value hash to the same value, otherwise comparisons
+ of mapping keys will turn out weird */
+ i = v->ob_size;
+ sign = 1;
+ x = 0;
+ if (i < 0) {
+ sign = -1;
+ i = -(i);
+ }
+ while (--i >= 0) {
+ /* Force a 32-bit circular shift */
+ x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK);
+ x += v->ob_digit[i];
+ }
+ x = x * sign;
+ if (x == -1)
+ x = -2;
+ return x;
+}
+
+
/* Add the absolute values of two long integers. */
static longobject *x_add PROTO((longobject *, longobject *));
&long_as_number,/*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ (long (*) FPROTO((object *)))
+ long_hash, /*tp_hash*/
};
/* Mapping object implementation; using a hash table */
+/* This file should really be called "dictobject.c", since "mapping"
+ is the generic name for objects with an unorderred arbitrary key
+ set (just like lists are sequences), but since it improves (and was
+ originally derived from) a file by that name I had to change its
+ name. For the user these objects are still called "dictionaries". */
+
#include "allobjects.h"
-#include "mappingobject.h"
#include "modsupport.h"
typeobject Mappingtype = {
OB_HEAD_INIT(&Typetype)
0,
- "mapping",
+ "dictionary",
sizeof(mappingobject),
0,
mapping_dealloc, /*tp_dealloc*/
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
sprintf(buf, "<built-in function '%.80s'>", m->m_name);
else
sprintf(buf,
- "<built-in method '%.80s' of some %.80s object>",
- m->m_name, m->m_self->ob_type->tp_name);
+ "<built-in method '%.80s' of %.80s object at %lx>",
+ m->m_name, m->m_self->ob_type->tp_name,
+ (long)m->m_self);
return newstringobject(buf);
}
+static int
+meth_compare(a, b)
+ methodobject *a, *b;
+{
+ if (a->m_self != b->m_self)
+ return cmpobject(a->m_self, b->m_self);
+ if (a->m_meth == b->m_meth)
+ return 0;
+ if (strcmp(a->m_name, b->m_name) < 0)
+ return -1;
+ else
+ return 1;
+}
+
+static long
+meth_hash(a)
+ methodobject *a;
+{
+ long x, y;
+ if (a->m_self == NULL)
+ x = 0;
+ else {
+ x = hashobject(a->m_self);
+ if (x == -1)
+ return -1;
+ }
+ return x ^ (long) a->m_meth;
+}
+
typeobject Methodtype = {
OB_HEAD_INIT(&Typetype)
0,
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
- 0, /*tp_compare*/
+ meth_compare, /*tp_compare*/
meth_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ meth_hash, /*tp_hash*/
};
object *listmethods PROTO((struct methodlist *)); /* Forward */
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
return (*tp->tp_compare)(v, w);
}
+long
+hashobject(v)
+ object *v;
+{
+ typeobject *tp = v->ob_type;
+ if (tp->tp_hash != NULL)
+ return (*tp->tp_hash)(v);
+ if (tp->tp_compare == NULL)
+ return (long) v; /* Use address as hash value */
+ /* If there's a cmp but no hash defined, the object can't be hashed */
+ err_setstr(TypeError, "unhashable type");
+ return -1;
+}
+
object *
getattr(v, name)
object *v;
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ 0, /*tp_hash */
};
object NoObject = {
return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
}
+static long
+string_hash(a)
+ stringobject *a;
+{
+ register int len = a->ob_size;
+ register unsigned char *p = (unsigned char *) a->ob_sval;
+ register long x = *p << 7;
+ while (--len >= 0)
+ x = (x + x + x) ^ *p++;
+ x ^= a->ob_size;
+ if (x == -1)
+ x = -2;
+ return x;
+}
+
static sequence_methods string_as_sequence = {
string_length, /*sq_length*/
string_concat, /*sq_concat*/
0, /*tp_as_number*/
&string_as_sequence, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ string_hash, /*tp_hash*/
};
void
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
return v->ob_size - w->ob_size;
}
+static long
+tuplehash(v)
+ tupleobject *v;
+{
+ register long x, y;
+ register int len = v->ob_size;
+ register object **p;
+ x = 0x345678L;
+ p = v->ob_item;
+ while (--len >= 0) {
+ y = hashobject(*p++);
+ if (y == -1)
+ return -1;
+ x = (x + x + x) ^ y;
+ }
+ x ^= v->ob_size;
+ if (x == -1)
+ x = -2;
+ return x;
+}
+
static int
tuplelength(a)
tupleobject *a;
0, /*tp_as_number*/
&tuple_as_sequence, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ tuplehash, /*tp_hash*/
};
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
object *args;
{
object *v;
- char *name;
- if (!getargs(args, "(Os)", &v, &name))
+ object *name;
+ if (!getargs(args, "(OS)", &v, &name))
return NULL;
- return getattr(v, name);
+ return getattro(v, name);
+}
+
+static object *
+builtin_hasattr(self, args)
+ object *self;
+ object *args;
+{
+ object *v;
+ object *name;
+ if (!getargs(args, "(OS)", &v, &name))
+ return NULL;
+ v = getattro(v, name);
+ if (v == NULL) {
+ err_clear();
+ return newintobject(0L);
+ }
+ DECREF(v);
+ return newintobject(1L);
}
static object *
object *args;
{
object *v;
- char *name;
+ object *name;
object *value;
- if (!getargs(args, "(OsO)", &v, &name, &value))
+ if (!getargs(args, "(OSO)", &v, &name, &value))
return NULL;
- if (setattr(v, name, value) != 0)
+ if (setattro(v, name, value) != 0)
return NULL;
INCREF(None);
return None;
}
+static object *
+builtin_hash(self, args)
+ object *self;
+ object *args;
+{
+ object *v;
+ long x;
+ if (!getargs(args, "O", &v))
+ return NULL;
+ x = hashobject(v);
+ if (x == -1)
+ return NULL;
+ return newintobject(x);
+}
+
static object *
builtin_hex(self, v)
object *self;
{"execfile", builtin_execfile},
{"float", builtin_float},
{"getattr", builtin_getattr},
+ {"hasattr", builtin_hasattr},
+ {"hash", builtin_hash},
{"hex", builtin_hex},
{"input", builtin_input},
{"int", builtin_int},
register object *u;
register object *t;
register frameobject *f; /* Current frame */
+ register listobject *fastlocals = NULL;
object *trace = NULL; /* Trace function or NULL */
object *retval; /* Return value iff why == WHY_RETURN */
char *name; /* Name used by some instructions */
break;
case STORE_ATTR:
- name = GETNAME(oparg);
+ w = GETNAMEV(oparg);
v = POP();
u = POP();
- err = setattr(v, name, u); /* v.name = u */
+ err = setattro(v, w, u); /* v.w = u */
DECREF(v);
DECREF(u);
break;
case DELETE_ATTR:
- name = GETNAME(oparg);
+ w = GETNAMEV(oparg);
v = POP();
- err = setattr(v, name, (object *)NULL);
- /* del v.name */
+ err = setattro(v, w, (object *)NULL); /* del v.w */
DECREF(v);
break;
INCREF(x);
PUSH(x);
break;
+
+ case RESERVE_FAST:
+ if (oparg > 0) {
+ XDECREF(fastlocals);
+ x = newlistobject(oparg);
+ fastlocals = (listobject *) x;
+ }
+ break;
+
+ case LOAD_FAST:
+ /* NYI */
+ break;
+
+ case STORE_FAST:
+ /* NYI */
+ break;
case BUILD_TUPLE:
x = newtupleobject(oparg);
break;
case LOAD_ATTR:
- name = GETNAME(oparg);
+ w = GETNAMEV(oparg);
v = POP();
- x = getattr(v, name);
+ x = getattro(v, w);
DECREF(v);
PUSH(x);
break;
current_frame = f->f_back;
DECREF(f);
+
+ XDECREF(fastlocals);
return retval;
}
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
{"co_consts", T_OBJECT, OFF(co_consts), READONLY},
{"co_names", T_OBJECT, OFF(co_names), READONLY},
{"co_filename", T_OBJECT, OFF(co_filename), READONLY},
+ {"co_name", T_OBJECT, OFF(co_name), READONLY},
{NULL} /* Sentinel */
};
XDECREF(co->co_consts);
XDECREF(co->co_names);
XDECREF(co->co_filename);
+ XDECREF(co->co_name);
DEL(co);
}
int lineno = -1;
char *p = GETSTRINGVALUE(co->co_code);
char *filename = "???";
+ char *name = "???";
if (*p == SET_LINENO)
lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
if (co->co_filename && is_stringobject(co->co_filename))
filename = getstringvalue(co->co_filename);
- sprintf(buf, "<code object at %lx, file \"%.400s\", line %d>",
- (long)co, filename, lineno);
+ if (co->co_name && is_stringobject(co->co_name))
+ name = getstringvalue(co->co_name);
+ sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
+ name, (long)co, filename, lineno);
return newstringobject(buf);
}
};
codeobject *
-newcodeobject(code, consts, names, filename)
+newcodeobject(code, consts, names, filename, name)
object *code;
object *consts;
object *names;
object *filename;
+ object *name;
{
codeobject *co;
int i;
/* Check argument types */
if (code == NULL || !is_stringobject(code) ||
consts == NULL || !is_listobject(consts) ||
- names == NULL || !is_listobject(names)) {
+ names == NULL || !is_listobject(names) ||
+ name == NULL || !is_stringobject(name)) {
err_badcall();
return NULL;
}
co->co_names = names;
INCREF(filename);
co->co_filename = filename;
+ INCREF(name);
+ co->co_name = name;
}
return co;
}
int c_block[MAXBLOCKS]; /* stack of block types */
int c_nblocks; /* current block stack level */
char *c_filename; /* filename of current node */
+ char *c_name; /* name of object (e.g. function) */
};
c->c_begin = 0;
c->c_nblocks = 0;
c->c_filename = filename;
+ c->c_name = "?";
return 1;
fail_0:
{
node *ch;
REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
+ c->c_name = STR(CHILD(n, 1));
+ com_addoparg(c, RESERVE_FAST, 0); /* Patched up later */
ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
ch = CHILD(ch, 1); /* ')' | varargslist */
if (TYPE(ch) == RPAR)
/* Optimization for local and global variables.
+ XXX Need to update this text for LOAD_FAST stuff...
+
Attempt to replace all LOAD_NAME instructions that refer to a local
variable with LOAD_LOCAL instructions, and all that refer to a global
variable with LOAD_GLOBAL instructions.
{
struct compiling sc;
codeobject *co;
- object *v;
if (!com_init(&sc, filename))
return NULL;
compile_node(&sc, n);
com_done(&sc);
- if (sc.c_errors == 0 && (v = newstringobject(filename)) != NULL) {
- co = newcodeobject(sc.c_code, sc.c_consts, sc.c_names, v);
- DECREF(v);
+ co = NULL;
+ if (sc.c_errors == 0) {
+ object *v, *w;
+ v = newstringobject(sc.c_filename);
+ w = newstringobject(sc.c_name);
+ if (v != NULL && w != NULL)
+ co = newcodeobject(sc.c_code, sc.c_consts,
+ sc.c_names, v, w);
+ XDECREF(v);
+ XDECREF(w);
}
- else
- co = NULL;
com_free(&sc);
if (co != NULL && filename[0] != '<')
optimizer(co);
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
extern char *argv0;
#endif
-/* Magic word to reject pre-0.9.4 .pyc files */
+/* Magic word to reject pre-0.9.9 .pyc files */
-#define MAGIC 0x949494L
+#define MAGIC 0x99BE2AL
static object *modules;
w_object(co->co_consts, p);
w_object(co->co_names, p);
w_object(co->co_filename, p);
+ w_object(co->co_name, p);
}
else {
w_byte(TYPE_UNKNOWN, p);
object *consts = r_object(p);
object *names = r_object(p);
object *filename = r_object(p);
+ object *name = r_object(p);
if (!err_occurred()) {
v = (object *) newcodeobject(code,
- consts, names, filename);
+ consts, names, filename, name);
}
else
v = NULL;
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved
/***********************************************************
-Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
-Netherlands.
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
All Rights Reserved