mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
* 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
This commit is contained in:
parent
4b1302bd1d
commit
9bfef44d97
97 changed files with 559 additions and 246 deletions
|
@ -281,10 +281,28 @@ builtin_getattr(self, args)
|
|||
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 *
|
||||
|
@ -293,16 +311,31 @@ builtin_setattr(self, args)
|
|||
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;
|
||||
|
@ -687,6 +720,8 @@ static struct methodlist builtin_methods[] = {
|
|||
{"execfile", builtin_execfile},
|
||||
{"float", builtin_float},
|
||||
{"getattr", builtin_getattr},
|
||||
{"hasattr", builtin_hasattr},
|
||||
{"hash", builtin_hash},
|
||||
{"hex", builtin_hex},
|
||||
{"input", builtin_input},
|
||||
{"int", builtin_int},
|
||||
|
|
|
@ -173,6 +173,7 @@ eval_code(co, globals, locals, arg)
|
|||
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 */
|
||||
|
@ -911,19 +912,18 @@ eval_code(co, globals, locals, arg)
|
|||
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;
|
||||
|
||||
|
@ -992,6 +992,22 @@ eval_code(co, globals, locals, arg)
|
|||
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);
|
||||
|
@ -1025,9 +1041,9 @@ eval_code(co, globals, locals, arg)
|
|||
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;
|
||||
|
@ -1283,6 +1299,8 @@ eval_code(co, globals, locals, arg)
|
|||
|
||||
current_frame = f->f_back;
|
||||
DECREF(f);
|
||||
|
||||
XDECREF(fastlocals);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
@ -50,6 +50,7 @@ static struct memberlist code_memberlist[] = {
|
|||
{"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 */
|
||||
};
|
||||
|
||||
|
@ -69,6 +70,7 @@ code_dealloc(co)
|
|||
XDECREF(co->co_consts);
|
||||
XDECREF(co->co_names);
|
||||
XDECREF(co->co_filename);
|
||||
XDECREF(co->co_name);
|
||||
DEL(co);
|
||||
}
|
||||
|
||||
|
@ -80,12 +82,15 @@ code_repr(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);
|
||||
}
|
||||
|
||||
|
@ -107,18 +112,20 @@ typeobject Codetype = {
|
|||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -140,6 +147,8 @@ newcodeobject(code, consts, names, filename)
|
|||
co->co_names = names;
|
||||
INCREF(filename);
|
||||
co->co_filename = filename;
|
||||
INCREF(name);
|
||||
co->co_name = name;
|
||||
}
|
||||
return co;
|
||||
}
|
||||
|
@ -162,6 +171,7 @@ struct compiling {
|
|||
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) */
|
||||
};
|
||||
|
||||
|
||||
|
@ -232,6 +242,7 @@ com_init(c, filename)
|
|||
c->c_begin = 0;
|
||||
c->c_nblocks = 0;
|
||||
c->c_filename = filename;
|
||||
c->c_name = "?";
|
||||
return 1;
|
||||
|
||||
fail_0:
|
||||
|
@ -2020,6 +2031,8 @@ compile_funcdef(c, n)
|
|||
{
|
||||
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)
|
||||
|
@ -2089,6 +2102,8 @@ compile_node(c, n)
|
|||
|
||||
/* 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.
|
||||
|
@ -2187,17 +2202,21 @@ compile(n, filename)
|
|||
{
|
||||
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);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
|
|
@ -52,9 +52,9 @@ extern int verbose; /* Defined in pythonmain.c */
|
|||
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;
|
||||
|
||||
|
|
|
@ -195,6 +195,7 @@ w_object(v, p)
|
|||
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);
|
||||
|
@ -384,9 +385,10 @@ r_object(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;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/***********************************************************
|
||||
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
||||
Netherlands.
|
||||
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
||||
Amsterdam, The Netherlands.
|
||||
|
||||
All Rights Reserved
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue