"Compiling" version

This commit is contained in:
Guido van Rossum 1990-12-20 15:06:42 +00:00
parent 226d79eb4a
commit 3f5da24ea3
72 changed files with 3363 additions and 2061 deletions

535
Python/bltinmodule.c Normal file
View file

@ -0,0 +1,535 @@
/* Built-in functions */
#include "allobjects.h"
#include "node.h"
#include "graminit.h"
#include "errcode.h"
#include "sysmodule.h"
#include "builtinmodule.h"
#include "import.h"
#include "pythonrun.h"
#include "compile.h" /* For ceval.h */
#include "ceval.h"
#include "modsupport.h"
static object *
builtin_abs(self, v)
object *self;
object *v;
{
/* XXX This should be a method in the as_number struct in the type */
if (v == NULL) {
/* */
}
else if (is_intobject(v)) {
long x = getintvalue(v);
if (x < 0)
x = -x;
return newintobject(x);
}
else if (is_floatobject(v)) {
double x = getfloatvalue(v);
if (x < 0)
x = -x;
return newfloatobject(x);
}
err_setstr(TypeError, "abs() argument must be float or int");
return NULL;
}
static object *
builtin_chr(self, v)
object *self;
object *v;
{
long x;
char s[1];
if (v == NULL || !is_intobject(v)) {
err_setstr(TypeError, "chr() must have int argument");
return NULL;
}
x = getintvalue(v);
if (x < 0 || x >= 256) {
err_setstr(RuntimeError, "chr() arg not in range(256)");
return NULL;
}
s[0] = x;
return newsizedstringobject(s, 1);
}
static object *
builtin_dir(self, v)
object *self;
object *v;
{
object *d;
if (v == NULL) {
d = getlocals();
}
else {
if (!is_moduleobject(v)) {
err_setstr(TypeError,
"dir() argument, must be module or absent");
return NULL;
}
d = getmoduledict(v);
}
v = getdictkeys(d);
if (sortlist(v) != 0) {
DECREF(v);
v = NULL;
}
return v;
}
static object *
builtin_divmod(self, v)
object *self;
object *v;
{
object *x, *y;
long xi, yi, xdivy, xmody;
if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) {
err_setstr(TypeError, "divmod() requires 2 int arguments");
return NULL;
}
x = gettupleitem(v, 0);
y = gettupleitem(v, 1);
if (!is_intobject(x) || !is_intobject(y)) {
err_setstr(TypeError, "divmod() requires 2 int arguments");
return NULL;
}
xi = getintvalue(x);
yi = getintvalue(y);
if (yi == 0) {
err_setstr(TypeError, "divmod() division by zero");
return NULL;
}
if (yi < 0) {
xdivy = -xi / -yi;
}
else {
xdivy = xi / yi;
}
xmody = xi - xdivy*yi;
if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) {
xmody += yi;
xdivy -= 1;
}
v = newtupleobject(2);
x = newintobject(xdivy);
y = newintobject(xmody);
if (v == NULL || x == NULL || y == NULL ||
settupleitem(v, 0, x) != 0 ||
settupleitem(v, 1, y) != 0) {
XDECREF(v);
XDECREF(x);
XDECREF(y);
return NULL;
}
return v;
}
static object *
exec_eval(v, start)
object *v;
int start;
{
object *str = NULL, *globals = NULL, *locals = NULL;
int n;
if (v != NULL) {
if (is_stringobject(v))
str = v;
else if (is_tupleobject(v) &&
((n = gettuplesize(v)) == 2 || n == 3)) {
str = gettupleitem(v, 0);
globals = gettupleitem(v, 1);
if (n == 3)
locals = gettupleitem(v, 2);
}
}
if (str == NULL || !is_stringobject(str) ||
globals != NULL && !is_dictobject(globals) ||
locals != NULL && !is_dictobject(locals)) {
err_setstr(TypeError,
"exec/eval arguments must be string[,dict[,dict]]");
return NULL;
}
return run_string(getstringvalue(str), start, globals, locals);
}
static object *
builtin_eval(self, v)
object *self;
object *v;
{
return exec_eval(v, eval_input);
}
static object *
builtin_exec(self, v)
object *self;
object *v;
{
return exec_eval(v, file_input);
}
static object *
builtin_float(self, v)
object *self;
object *v;
{
if (v == NULL) {
/* */
}
else if (is_floatobject(v)) {
INCREF(v);
return v;
}
else if (is_intobject(v)) {
long x = getintvalue(v);
return newfloatobject((double)x);
}
err_setstr(TypeError, "float() argument must be float or int");
return NULL;
}
static object *
builtin_input(self, v)
object *self;
object *v;
{
FILE *in = sysgetfile("stdin", stdin);
FILE *out = sysgetfile("stdout", stdout);
node *n;
int err;
object *m, *d;
flushline();
if (v != NULL)
printobject(v, out, PRINT_RAW);
m = add_module("__main__");
d = getmoduledict(m);
return run_file(in, "<stdin>", expr_input, d, d);
}
static object *
builtin_int(self, v)
object *self;
object *v;
{
if (v == NULL) {
/* */
}
else if (is_intobject(v)) {
INCREF(v);
return v;
}
else if (is_floatobject(v)) {
double x = getfloatvalue(v);
return newintobject((long)x);
}
err_setstr(TypeError, "int() argument must be float or int");
return NULL;
}
static object *
builtin_len(self, v)
object *self;
object *v;
{
long len;
typeobject *tp;
if (v == NULL) {
err_setstr(TypeError, "len() without argument");
return NULL;
}
tp = v->ob_type;
if (tp->tp_as_sequence != NULL) {
len = (*tp->tp_as_sequence->sq_length)(v);
}
else if (tp->tp_as_mapping != NULL) {
len = (*tp->tp_as_mapping->mp_length)(v);
}
else {
err_setstr(TypeError, "len() of unsized object");
return NULL;
}
return newintobject(len);
}
static object *
min_max(v, sign)
object *v;
int sign;
{
int i, n, cmp;
object *w, *x;
sequence_methods *sq;
if (v == NULL) {
err_setstr(TypeError, "min() or max() without argument");
return NULL;
}
sq = v->ob_type->tp_as_sequence;
if (sq == NULL) {
err_setstr(TypeError, "min() or max() of non-sequence");
return NULL;
}
n = (*sq->sq_length)(v);
if (n == 0) {
err_setstr(RuntimeError, "min() or max() of empty sequence");
return NULL;
}
w = (*sq->sq_item)(v, 0); /* Implies INCREF */
for (i = 1; i < n; i++) {
x = (*sq->sq_item)(v, i); /* Implies INCREF */
cmp = cmpobject(x, w);
if (cmp * sign > 0) {
DECREF(w);
w = x;
}
else
DECREF(x);
}
return w;
}
static object *
builtin_min(self, v)
object *self;
object *v;
{
return min_max(v, -1);
}
static object *
builtin_max(self, v)
object *self;
object *v;
{
return min_max(v, 1);
}
static object *
builtin_open(self, v)
object *self;
object *v;
{
object *name, *mode;
if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2 ||
!is_stringobject(name = gettupleitem(v, 0)) ||
!is_stringobject(mode = gettupleitem(v, 1))) {
err_setstr(TypeError, "open() requires 2 string arguments");
return NULL;
}
v = newfileobject(getstringvalue(name), getstringvalue(mode));
return v;
}
static object *
builtin_ord(self, v)
object *self;
object *v;
{
if (v == NULL || !is_stringobject(v)) {
err_setstr(TypeError, "ord() must have string argument");
return NULL;
}
if (getstringsize(v) != 1) {
err_setstr(RuntimeError, "ord() arg must have length 1");
return NULL;
}
return newintobject((long)(getstringvalue(v)[0] & 0xff));
}
static object *
builtin_range(self, v)
object *self;
object *v;
{
static char *errmsg = "range() requires 1-3 int arguments";
int i, n;
long ilow, ihigh, istep;
if (v != NULL && is_intobject(v)) {
ilow = 0; ihigh = getintvalue(v); istep = 1;
}
else if (v == NULL || !is_tupleobject(v)) {
err_setstr(TypeError, errmsg);
return NULL;
}
else {
n = gettuplesize(v);
if (n < 1 || n > 3) {
err_setstr(TypeError, errmsg);
return NULL;
}
for (i = 0; i < n; i++) {
if (!is_intobject(gettupleitem(v, i))) {
err_setstr(TypeError, errmsg);
return NULL;
}
}
if (n == 3) {
istep = getintvalue(gettupleitem(v, 2));
--n;
}
else
istep = 1;
ihigh = getintvalue(gettupleitem(v, --n));
if (n > 0)
ilow = getintvalue(gettupleitem(v, 0));
else
ilow = 0;
}
if (istep == 0) {
err_setstr(RuntimeError, "zero step for range()");
return NULL;
}
/* XXX ought to check overflow of subtraction */
if (istep > 0)
n = (ihigh - ilow + istep - 1) / istep;
else
n = (ihigh - ilow + istep + 1) / istep;
if (n < 0)
n = 0;
v = newlistobject(n);
if (v == NULL)
return NULL;
for (i = 0; i < n; i++) {
object *w = newintobject(ilow);
if (w == NULL) {
DECREF(v);
return NULL;
}
setlistitem(v, i, w);
ilow += istep;
}
return v;
}
static object *
builtin_raw_input(self, v)
object *self;
object *v;
{
FILE *in = sysgetfile("stdin", stdin);
FILE *out = sysgetfile("stdout", stdout);
char *p;
int err;
int n = 1000;
flushline();
if (v != NULL)
printobject(v, out, PRINT_RAW);
v = newsizedstringobject((char *)NULL, n);
if (v != NULL) {
if ((err = fgets_intr(getstringvalue(v), n+1, in)) != E_OK) {
err_input(err);
DECREF(v);
return NULL;
}
else {
n = strlen(getstringvalue(v));
if (n > 0 && getstringvalue(v)[n-1] == '\n')
n--;
resizestring(&v, n);
}
}
return v;
}
static object *
builtin_reload(self, v)
object *self;
object *v;
{
return reload_module(v);
}
static object *
builtin_type(self, v)
object *self;
object *v;
{
if (v == NULL) {
err_setstr(TypeError, "type() requres an argument");
return NULL;
}
v = (object *)v->ob_type;
INCREF(v);
return v;
}
static struct methodlist builtin_methods[] = {
{"abs", builtin_abs},
{"chr", builtin_chr},
{"dir", builtin_dir},
{"divmod", builtin_divmod},
{"eval", builtin_eval},
{"exec", builtin_exec},
{"float", builtin_float},
{"input", builtin_input},
{"int", builtin_int},
{"len", builtin_len},
{"min", builtin_min},
{"max", builtin_max},
{"open", builtin_open}, /* XXX move to OS module */
{"ord", builtin_ord},
{"range", builtin_range},
{"raw_input", builtin_raw_input},
{"reload", builtin_reload},
{"type", builtin_type},
{NULL, NULL},
};
static object *builtin_dict;
object *
getbuiltin(name)
char *name;
{
return dictlookup(builtin_dict, name);
}
/* Predefined exceptions */
object *RuntimeError;
object *EOFError;
object *TypeError;
object *MemoryError;
object *NameError;
object *SystemError;
object *KeyboardInterrupt;
static object *
newstdexception(name, message)
char *name, *message;
{
object *v = newstringobject(message);
if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
fatal("no mem for new standard exception");
return v;
}
static void
initerrors()
{
RuntimeError = newstdexception("RuntimeError", "run-time error");
EOFError = newstdexception("EOFError", "end-of-file read");
TypeError = newstdexception("TypeError", "type error");
MemoryError = newstdexception("MemoryError", "out of memory");
NameError = newstdexception("NameError", "undefined name");
SystemError = newstdexception("SystemError", "system error");
KeyboardInterrupt =
newstdexception("KeyboardInterrupt", "keyboard interrupt");
}
void
initbuiltin()
{
object *m;
m = initmodule("builtin", builtin_methods);
builtin_dict = getmoduledict(m);
INCREF(builtin_dict);
initerrors();
(void) dictinsert(builtin_dict, "None", None);
}

File diff suppressed because it is too large Load diff

View file

@ -1,31 +1,49 @@
/* Compile an expression node to intermediate code */
#include <stdio.h>
#include <ctype.h>
#include "string.h"
/* XXX TO DO:
XXX Compute maximum needed stack sizes while compiling
XXX Generate simple jump for break/return outside 'try...finally'
XXX Include function name in code (and module names?)
*/
#include "allobjects.h"
#include "PROTO.h"
#include "object.h"
#include "objimpl.h"
#include "intobject.h"
#include "floatobject.h"
#include "stringobject.h"
#include "listobject.h"
#include "node.h"
#include "token.h"
#include "graminit.h"
#include "errors.h"
#include "compile.h"
#include "opcode.h"
#include "structmember.h"
#include <ctype.h>
#define OFF(x) offsetof(codeobject, x)
static struct memberlist code_memberlist[] = {
{"co_code", T_OBJECT, OFF(co_code)},
{"co_consts", T_OBJECT, OFF(co_consts)},
{"co_names", T_OBJECT, OFF(co_names)},
{"co_filename", T_OBJECT, OFF(co_filename)},
{NULL} /* Sentinel */
};
static object *
code_getattr(co, name)
codeobject *co;
char *name;
{
return getmember((char *)co, code_memberlist, name);
}
static void
code_dealloc(c)
codeobject *c;
code_dealloc(co)
codeobject *co;
{
XDECREF(c->co_code);
XDECREF(c->co_consts);
XDECREF(c->co_names);
DEL(c);
XDECREF(co->co_code);
XDECREF(co->co_consts);
XDECREF(co->co_names);
XDECREF(co->co_filename);
DEL(co);
}
typeobject Codetype = {
@ -36,7 +54,7 @@ typeobject Codetype = {
0,
code_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
code_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
@ -45,13 +63,14 @@ typeobject Codetype = {
0, /*tp_as_mapping*/
};
static codeobject *newcodeobject PROTO((object *, object *, object *));
static codeobject *newcodeobject PROTO((object *, object *, object *, char *));
static codeobject *
newcodeobject(code, consts, names)
newcodeobject(code, consts, names, filename)
object *code;
object *consts;
object *names;
char *filename;
{
codeobject *co;
int i;
@ -78,6 +97,10 @@ newcodeobject(code, consts, names)
co->co_consts = consts;
INCREF(names);
co->co_names = names;
if ((co->co_filename = newstringobject(filename)) == NULL) {
DECREF(co);
co = NULL;
}
}
return co;
}
@ -90,10 +113,13 @@ struct compiling {
object *c_names; /* list of strings (names) */
int c_nexti; /* index into c_code */
int c_errors; /* counts errors occurred */
int c_infunction; /* set when compiling a function */
int c_loops; /* counts nested loops */
char *c_filename; /* filename of current node */
};
/* Prototypes */
static int com_init PROTO((struct compiling *));
static int com_init PROTO((struct compiling *, char *));
static void com_free PROTO((struct compiling *));
static void com_done PROTO((struct compiling *));
static void com_node PROTO((struct compiling *, struct _node *));
@ -108,8 +134,9 @@ static int com_addname PROTO((struct compiling *, object *));
static void com_addopname PROTO((struct compiling *, int, node *));
static int
com_init(c)
com_init(c, filename)
struct compiling *c;
char *filename;
{
if ((c->c_code = newsizedstringobject((char *)NULL, 0)) == NULL)
goto fail_3;
@ -119,6 +146,9 @@ com_init(c)
goto fail_1;
c->c_nexti = 0;
c->c_errors = 0;
c->c_infunction = 0;
c->c_loops = 0;
c->c_filename = filename;
return 1;
fail_1:
@ -153,6 +183,8 @@ com_addbyte(c, byte)
{
int len;
if (byte < 0 || byte > 255) {
fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
abort();
err_setstr(SystemError, "com_addbyte: byte out of range");
c->c_errors++;
}
@ -1076,6 +1108,10 @@ com_return_stmt(c, n)
node *n;
{
REQ(n, return_stmt); /* 'return' [testlist] NEWLINE */
if (!c->c_infunction) {
err_setstr(TypeError, "'return' outside function");
c->c_errors++;
}
if (NCH(n) == 2)
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
else
@ -1134,6 +1170,9 @@ com_if_stmt(c, n)
/*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
for (i = 0; i+3 < NCH(n); i+=4) {
int a = 0;
node *ch = CHILD(n, i+1);
if (i > 0)
com_addoparg(c, SET_LINENO, ch->n_lineno);
com_node(c, CHILD(n, i+1));
com_addfwref(c, JUMP_IF_FALSE, &a);
com_addbyte(c, POP_TOP);
@ -1158,10 +1197,13 @@ com_while_stmt(c, n)
REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
com_addfwref(c, SETUP_LOOP, &break_anchor);
begin = c->c_nexti;
com_addoparg(c, SET_LINENO, n->n_lineno);
com_node(c, CHILD(n, 1));
com_addfwref(c, JUMP_IF_FALSE, &anchor);
com_addbyte(c, POP_TOP);
c->c_loops++;
com_node(c, CHILD(n, 3));
c->c_loops--;
com_addoparg(c, JUMP_ABSOLUTE, begin);
com_backpatch(c, anchor);
com_addbyte(c, POP_TOP);
@ -1190,9 +1232,12 @@ com_for_stmt(c, n)
com_addoparg(c, LOAD_CONST, com_addconst(c, v));
XDECREF(v);
begin = c->c_nexti;
com_addoparg(c, SET_LINENO, n->n_lineno);
com_addfwref(c, FOR_LOOP, &anchor);
com_assign(c, CHILD(n, 1), 1/*assigning*/);
c->c_loops++;
com_node(c, CHILD(n, 5));
c->c_loops--;
com_addoparg(c, JUMP_ABSOLUTE, begin);
com_backpatch(c, anchor);
com_addbyte(c, POP_BLOCK);
@ -1225,7 +1270,6 @@ com_for_stmt(c, n)
<code for S>
POP_BLOCK
LOAD_CONST <nil>
LOAD_CONST <nil>
L: <code for Sf>
END_FINALLY
@ -1242,13 +1286,9 @@ com_for_stmt(c, n)
stack until its level is the same as indicated on the
block stack. (The label is ignored.)
END_FINALLY:
Pops two entries from the *value* stack and re-raises
the exception they specify. If the top entry is nil,
no exception is raised. If it is a number, a pseudo
exception is raised ('return' or 'break'). Otherwise,
a real exception (specified by a string) is raised.
The second entry popped from the is the value that goes
with the exception (or the return value).
Pops a variable number of entries from the *value* stack
and re-raises the exception they specify. The number of
entries popped depends on the (pseudo) exception type.
The block stack is unwound when an exception is raised:
when a SETUP_FINALLY entry is found, the exception is pushed
@ -1257,6 +1297,9 @@ com_for_stmt(c, n)
stack.
Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
(The contents of the value stack is shown in [], with the top
at the right; 'tb' is trace-back info, 'val' the exception's
associated value, and 'exc' the exception.)
Value stack Label Instruction Argument
[] SETUP_EXCEPT L1
@ -1264,22 +1307,23 @@ com_for_stmt(c, n)
[] POP_BLOCK
[] JUMP_FORWARD L0
[val, exc] L1: DUP
[val, exc, exc] <evaluate E1>
[val, exc, exc, E1] COMPARE_OP EXC_MATCH
[val, exc, 1-or-0] JUMP_IF_FALSE L2
[val, exc, 1] POP
[val, exc] POP
[val] <assign to V1>
[tb, val, exc] L1: DUP )
[tb, val, exc, exc] <evaluate E1> )
[tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
[tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
[tb, val, exc, 1] POP )
[tb, val, exc] POP
[tb, val] <assign to V1> (or POP if no V1)
[tb] POP
[] <code for S1>
JUMP_FORWARD L0
[val, exc, 0] L2: POP
[val, exc] DUP
[tb, val, exc, 0] L2: POP
[tb, val, exc] DUP
.............................etc.......................
[val, exc, 0] Ln+1: POP
[val, exc] END_FINALLY # re-raise exception
[tb, val, exc, 0] Ln+1: POP
[tb, val, exc] END_FINALLY # re-raise exception
[] L0: <next statement>
@ -1323,6 +1367,7 @@ com_try_stmt(c, n)
break;
}
except_anchor = 0;
com_addoparg(c, SET_LINENO, ch->n_lineno);
if (NCH(ch) > 1) {
com_addbyte(c, DUP_TOP);
com_node(c, CHILD(ch, 1));
@ -1335,6 +1380,7 @@ com_try_stmt(c, n)
com_assign(c, CHILD(ch, 3), 1/*assigning*/);
else
com_addbyte(c, POP_TOP);
com_addbyte(c, POP_TOP);
com_node(c, CHILD(n, i+2));
com_addfwref(c, JUMP_FORWARD, &end_anchor);
if (except_anchor) {
@ -1346,11 +1392,13 @@ com_try_stmt(c, n)
com_backpatch(c, end_anchor);
}
if (finally_anchor) {
node *ch;
com_addbyte(c, POP_BLOCK);
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
com_backpatch(c, finally_anchor);
com_node(c, CHILD(n, NCH(n)-1));
ch = CHILD(n, NCH(n)-1);
com_addoparg(c, SET_LINENO, ch->n_lineno);
com_node(c, ch);
com_addbyte(c, END_FINALLY);
}
}
@ -1382,7 +1430,7 @@ com_funcdef(c, n)
{
object *v;
REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
v = (object *)compile(n);
v = (object *)compile(n, c->c_filename);
if (v == NULL)
c->c_errors++;
else {
@ -1426,7 +1474,7 @@ com_classdef(c, n)
com_bases(c, CHILD(n, 4));
else
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
v = (object *)compile(n);
v = (object *)compile(n, c->c_filename);
if (v == NULL)
c->c_errors++;
else {
@ -1459,9 +1507,13 @@ com_node(c, n)
/* Trivial parse tree nodes */
case stmt:
case simple_stmt:
case flow_stmt:
com_node(c, CHILD(n, 0));
break;
case simple_stmt:
case compound_stmt:
com_addoparg(c, SET_LINENO, n->n_lineno);
com_node(c, CHILD(n, 0));
break;
@ -1479,6 +1531,10 @@ com_node(c, n)
case pass_stmt:
break;
case break_stmt:
if (c->c_loops == 0) {
err_setstr(TypeError, "'break' outside loop");
c->c_errors++;
}
com_addbyte(c, BREAK_LOOP);
break;
case return_stmt:
@ -1608,48 +1664,50 @@ compile_funcdef(c, n)
com_addbyte(c, REQUIRE_ARGS);
com_fplist(c, ch);
}
c->c_infunction = 1;
com_node(c, CHILD(n, 4));
c->c_infunction = 0;
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
com_addbyte(c, RETURN_VALUE);
}
static void
compile_classdef(c, n)
struct compiling *c;
node *n;
{
node *ch;
REQ(n, classdef);
/*
classdef: 'class' NAME parameters ['=' baselist] ':' suite
*/
com_addbyte(c, REFUSE_ARGS);
com_node(c, CHILD(n, NCH(n)-1));
com_addbyte(c, LOAD_LOCALS);
com_addbyte(c, RETURN_VALUE);
}
static void
compile_node(c, n)
struct compiling *c;
node *n;
{
com_addoparg(c, SET_LINENO, n->n_lineno);
switch (TYPE(n)) {
case single_input:
/* NEWLINE | simple_stmt | compound_stmt NEWLINE */
com_addbyte(c, REFUSE_ARGS);
n = CHILD(n, 0);
if (TYPE(n) != NEWLINE)
com_node(c, n);
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
com_addbyte(c, RETURN_VALUE);
break;
case file_input:
com_addbyte(c, REFUSE_ARGS);
com_file_input(c, n);
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
com_addbyte(c, RETURN_VALUE);
break;
case expr_input:
case eval_input:
com_addbyte(c, REFUSE_ARGS);
com_node(c, CHILD(n, 0));
com_addoparg(c, LOAD_CONST, com_addconst(c, None));
com_addbyte(c, RETURN_VALUE);
break;
case eval_input:
com_addbyte(c, REFUSE_ARGS);
com_node(c, CHILD(n, 0));
com_addbyte(c, RETURN_VALUE);
break;
case funcdef:
@ -1657,7 +1715,11 @@ compile_node(c, n)
break;
case classdef:
compile_classdef(c, n);
/* 'class' NAME parameters ['=' baselist] ':' suite */
com_addbyte(c, REFUSE_ARGS);
com_node(c, CHILD(n, NCH(n)-1));
com_addbyte(c, LOAD_LOCALS);
com_addbyte(c, RETURN_VALUE);
break;
default:
@ -1668,17 +1730,18 @@ compile_node(c, n)
}
codeobject *
compile(n)
compile(n, filename)
node *n;
char *filename;
{
struct compiling sc;
codeobject *co;
if (!com_init(&sc))
if (!com_init(&sc, filename))
return NULL;
compile_node(&sc, n);
com_done(&sc);
if (sc.c_errors == 0)
co = newcodeobject(sc.c_code, sc.c_consts, sc.c_names);
co = newcodeobject(sc.c_code, sc.c_consts, sc.c_names, filename);
else
co = NULL;
com_free(&sc);

View file

@ -11,12 +11,11 @@
return value and then calls puterrno(ctx) to turn the errno value
into a true exception. Problems with this approach are:
- it used standard errno values to indicate Python-specific errors,
but this means that when such an error code is reported by UNIX the
user gets a confusing message
but this means that when such an error code is reported by a system
call (e.g., in module posix), the user gets a confusing message
- errno is a global variable, which makes extensions to a multi-
threading environment difficult; e.g., in IRIX, multi-threaded
programs must use the function getoserror() (sp.?) instead of
looking in errno
programs must use the function oserror() instead of looking in errno
- there is no portable way to add new error numbers for specic
situations -- the value space for errno is reserved to the OS, yet
the way to turn module-specific errors into a module-specific
@ -25,21 +24,18 @@
error.
The new interface solves all these problems. To return an error, a
built-in function calls err_set(exception), err_set(valexception,
built-in function calls err_set(exception), err_setval(exception,
value) or err_setstr(exception, string), and returns NULL. These
functions save the value for later use by puterrno(). To adapt this
scheme to a multi-threaded environment, only the implementation of
err_setval() has to be changed.
*/
#include <stdio.h>
#include "errno.h"
#include "PROTO.h"
#include "object.h"
#include "intobject.h"
#include "stringobject.h"
#include "tupleobject.h"
#include "errors.h"
#include "allobjects.h"
#include "errcode.h"
extern char *strerror PROTO((int));
@ -53,16 +49,12 @@ err_setval(exception, value)
object *exception;
object *value;
{
if (last_exception != NULL)
DECREF(last_exception);
if (exception != NULL)
INCREF(exception);
XDECREF(last_exception);
XINCREF(exception);
last_exception = exception;
if (last_exc_val != NULL)
DECREF(last_exc_val);
if (value != NULL)
INCREF(value);
XDECREF(last_exc_val);
XINCREF(value);
last_exc_val = value;
}
@ -80,8 +72,7 @@ err_setstr(exception, string)
{
object *value = newstringobject(string);
err_setval(exception, value);
if (value != NULL)
DECREF(value);
XDECREF(value);
}
int
@ -104,14 +95,10 @@ err_get(p_exc, p_val)
void
err_clear()
{
if (last_exception != NULL) {
DECREF(last_exception);
last_exception = NULL;
}
if (last_exc_val != NULL) {
DECREF(last_exc_val);
last_exc_val = NULL;
}
XDECREF(last_exception);
last_exception = NULL;
XDECREF(last_exc_val);
last_exc_val = NULL;
}
/* Convenience functions to set a type error exception and return 0 */
@ -126,7 +113,7 @@ err_badarg()
object *
err_nomem()
{
err_setstr(MemoryError, "in built-in function");
err_set(MemoryError);
return NULL;
}
@ -140,8 +127,7 @@ err_errno(exc)
settupleitem(v, 1, newstringobject(strerror(errno)));
}
err_setval(exc, v);
if (v != NULL)
DECREF(v);
XDECREF(v);
return NULL;
}
@ -150,3 +136,34 @@ err_badcall()
{
err_setstr(SystemError, "bad argument to internal function");
}
/* Set the error appropriate to the given input error code (see errcode.h) */
void
err_input(err)
int err;
{
switch (err) {
case E_DONE:
case E_OK:
break;
case E_SYNTAX:
err_setstr(RuntimeError, "syntax error");
break;
case E_TOKEN:
err_setstr(RuntimeError, "illegal token");
break;
case E_INTR:
err_set(KeyboardInterrupt);
break;
case E_NOMEM:
err_nomem();
break;
case E_EOF:
err_set(EOFError);
break;
default:
err_setstr(RuntimeError, "unknown input error");
break;
}
}

View file

@ -1,4 +1,4 @@
#include "PROTO.h"
#include "pgenheaders.h"
#include "grammar.h"
static arc arcs_0_0[3] = {
{2, 1},

View file

@ -1,153 +1,67 @@
/* Module definition and import implementation */
#include <stdio.h>
#include "string.h"
#include "allobjects.h"
#include "PROTO.h"
#include "object.h"
#include "stringobject.h"
#include "listobject.h"
#include "dictobject.h"
#include "moduleobject.h"
#include "node.h"
#include "context.h"
#include "token.h"
#include "graminit.h"
#include "run.h"
#include "support.h"
#include "import.h"
#include "errcode.h"
#include "sysmodule.h"
#include "pythonrun.h"
/* Define pathname separator and delimiter in $PYTHONPATH */
/* Define pathname separator used in file names */
#ifdef THINK_C
#define SEP ':'
#define DELIM ' '
#endif
#ifndef SEP
#define SEP '/'
#endif
#ifndef DELIM
#define DELIM ':'
#endif
static object *modules;
/* Initialization */
void
initimport()
{
object *v;
if ((v = newdictobject()) == NULL)
fatal("no mem for module table");
if (sysset("modules", v) != 0)
fatal("can't assign sys.modules");
DECREF(v);
if ((modules = newdictobject()) == NULL)
fatal("no mem for dictionary of modules");
}
object *
new_module(name)
get_modules()
{
return modules;
}
object *
add_module(name)
char *name;
{
object *m;
object *mtab;
mtab = sysget("modules");
if (mtab == NULL || !is_dictobject(mtab)) {
errno = EBADF;
return NULL;
}
if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m))
return m;
m = newmoduleobject(name);
if (m == NULL)
return NULL;
if (dictinsert(mtab, name, m) != 0) {
if (dictinsert(modules, name, m) != 0) {
DECREF(m);
return NULL;
}
DECREF(m); /* Yes, it still exists, in modules! */
return m;
}
static void
use_module(ctx, d)
context *ctx;
object *d;
{
INCREF(d);
DECREF(ctx->ctx_locals);
ctx->ctx_locals = d;
INCREF(d);
DECREF(ctx->ctx_globals);
ctx->ctx_globals = d;
}
static void
define_module(ctx, name)
context *ctx;
char *name;
{
object *m;
m = new_module(name);
if (m == NULL) {
puterrno(ctx);
return;
}
use_module(ctx, getmoduledict(m));
DECREF(m);
}
static object *
parsepath(path, delim)
char *path;
int delim;
{
int i, n;
char *p;
object *v, *w;
n = 1;
p = path;
while ((p = strchr(p, delim)) != NULL) {
n++;
p++;
}
v = newlistobject(n);
if (v == NULL)
return NULL;
for (i = 0; ; i++) {
p = strchr(path, delim);
if (p == NULL)
p = strchr(path, '\0'); /* End of string */
w = newsizedstringobject(path, (int) (p - path));
if (w == NULL) {
DECREF(v);
return NULL;
}
setlistitem(v, i, w);
if (*p == '\0')
break;
path = p+1;
}
return v;
}
void
setpythonpath(path)
char *path;
{
object *v;
if ((v = parsepath(path, DELIM)) != NULL) {
if (sysset("path", v) != 0)
fatal("can't assign sys.path");
DECREF(v);
}
}
static FILE *
open_module(name, suffix)
open_module(name, suffix, namebuf)
char *name;
char *suffix;
char *namebuf; /* XXX No buffer overflow checks! */
{
object *path;
char namebuf[256];
FILE *fp;
path = sysget("path");
@ -169,8 +83,8 @@ open_module(name, suffix)
len = getstringsize(v);
if (len > 0 && namebuf[len-1] != SEP)
namebuf[len++] = SEP;
strcpy(namebuf+len, name); /* XXX check for overflow */
strcat(namebuf, suffix); /* XXX ditto */
strcpy(namebuf+len, name);
strcat(namebuf, suffix);
fp = fopen(namebuf, "r");
if (fp != NULL)
break;
@ -180,119 +94,113 @@ open_module(name, suffix)
}
static object *
load_module(ctx, name)
context *ctx;
get_module(m, name, m_ret)
/*module*/object *m;
char *name;
object **m_ret;
{
object *m;
char **p;
FILE *fp;
node *n;
int err;
object *mtab;
object *save_locals, *save_globals;
mtab = sysget("modules");
if (mtab == NULL || !is_dictobject(mtab)) {
errno = EBADF;
return NULL;
}
fp = open_module(name, ".py");
if (fp == NULL) {
name_error(ctx, name);
return NULL;
}
err = parseinput(fp, file_input, &n);
fclose(fp);
if (err != E_DONE) {
input_error(ctx, err);
return NULL;
}
save_locals = ctx->ctx_locals;
INCREF(save_locals);
save_globals = ctx->ctx_globals;
INCREF(save_globals);
define_module(ctx, name);
exec_node(ctx, n);
DECREF(ctx->ctx_locals);
ctx->ctx_locals = save_locals;
DECREF(ctx->ctx_globals);
ctx->ctx_globals = save_globals;
/* XXX need to free the tree n here; except referenced defs */
if (ctx->ctx_exception) {
dictremove(mtab, name); /* Undefine the module */
return NULL;
}
m = dictlookup(mtab, name);
if (m == NULL) {
error(ctx, "module not defined after loading");
return NULL;
}
return m;
}
object *
import_module(ctx, name)
context *ctx;
char *name;
{
object *m;
object *mtab;
mtab = sysget("modules");
if (mtab == NULL || !is_dictobject(mtab)) {
error(ctx, "bad sys.modules");
return NULL;
}
if ((m = dictlookup(mtab, name)) == NULL) {
m = load_module(ctx, name);
}
return m;
}
object *
reload_module(ctx, m)
context *ctx;
object *m;
{
char *name;
FILE *fp;
node *n;
int err;
object *d;
object *save_locals, *save_globals;
if (m == NULL || !is_moduleobject(m)) {
type_error(ctx, "reload() argument must be module");
return NULL;
}
/* XXX Ought to check for builtin module */
name = getmodulename(m);
fp = open_module(name, ".py");
FILE *fp;
node *n;
int err;
char namebuf[256];
fp = open_module(name, ".py", namebuf);
if (fp == NULL) {
error(ctx, "reload() cannot find module source file");
if (m == NULL)
err_setstr(NameError, name);
else
err_setstr(RuntimeError, "no module source file");
return NULL;
}
err = parseinput(fp, file_input, &n);
err = parse_file(fp, namebuf, file_input, &n);
fclose(fp);
if (err != E_DONE) {
input_error(ctx, err);
err_input(err);
return NULL;
}
d = newdictobject();
if (d == NULL)
return NULL;
setmoduledict(m, d);
save_locals = ctx->ctx_locals;
INCREF(save_locals);
save_globals = ctx->ctx_globals;
INCREF(save_globals);
use_module(ctx, d);
exec_node(ctx, n);
DECREF(ctx->ctx_locals);
ctx->ctx_locals = save_locals;
DECREF(ctx->ctx_globals);
ctx->ctx_globals = save_globals;
if (ctx->ctx_exception)
return NULL;
INCREF(None);
return None;
if (m == NULL) {
m = add_module(name);
if (m == NULL) {
freetree(n);
return NULL;
}
*m_ret = m;
}
d = getmoduledict(m);
return run_node(n, namebuf, d, d);
}
static object *
load_module(name)
char *name;
{
object *m, *v;
v = get_module((object *)NULL, name, &m);
if (v == NULL)
return NULL;
DECREF(v);
return m;
}
object *
import_module(name)
char *name;
{
object *m;
if ((m = dictlookup(modules, name)) == NULL)
m = load_module(name);
return m;
}
object *
reload_module(m)
object *m;
{
if (m == NULL || !is_moduleobject(m)) {
err_setstr(TypeError, "reload() argument must be module");
return NULL;
}
/* XXX Ought to check for builtin modules -- can't reload these... */
return get_module(m, getmodulename(m), (object **)NULL);
}
static void
cleardict(d)
object *d;
{
int i;
for (i = getdictsize(d); --i >= 0; ) {
char *k;
k = getdictkey(d, i);
if (k != NULL)
(void) dictremove(d, k);
}
}
void
doneimport()
{
if (modules != NULL) {
int i;
/* Explicitly erase all modules; this is the safest way
to get rid of at least *some* circular dependencies */
for (i = getdictsize(modules); --i >= 0; ) {
char *k;
k = getdictkey(modules, i);
if (k != NULL) {
object *m;
m = dictlookup(modules, k);
if (m != NULL && is_moduleobject(m)) {
object *d;
d = getmoduledict(m);
if (d != NULL && is_dictobject(d)) {
cleardict(d);
}
}
}
}
cleardict(modules);
}
DECREF(modules);
}

View file

@ -1,36 +1,8 @@
/* Module support implementation */
#include <stdio.h>
#include "PROTO.h"
#include "object.h"
#include "intobject.h"
#include "stringobject.h"
#include "tupleobject.h"
#include "listobject.h"
#include "methodobject.h"
#include "moduleobject.h"
#include "allobjects.h"
#include "modsupport.h"
#include "import.h"
#include "errors.h"
/* Find a method in a module's method table.
Usually called from a module's getattr method. */
object *
findmethod(ml, op, name)
struct methodlist *ml;
object *op;
char *name;
{
for (; ml->ml_name != NULL; ml++) {
if (strcmp(name, ml->ml_name) == 0)
return newmethodobject(ml->ml_name, ml->ml_meth, op);
}
err_setstr(NameError, name);
return NULL;
}
object *
@ -40,21 +12,24 @@ initmodule(name, methods)
{
object *m, *d, *v;
struct methodlist *ml;
if ((m = new_module(name)) == NULL) {
char namebuf[256];
if ((m = add_module(name)) == NULL) {
fprintf(stderr, "initializing module: %s\n", name);
fatal("can't create a module");
}
d = getmoduledict(m);
for (ml = methods; ml->ml_name != NULL; ml++) {
v = newmethodobject(ml->ml_name, ml->ml_meth, (object *)NULL);
sprintf(namebuf, "%s.%s", name, ml->ml_name);
v = newmethodobject(strdup(namebuf), ml->ml_meth,
(object *)NULL);
/* XXX The strdup'ed memory is never freed */
if (v == NULL || dictinsert(d, ml->ml_name, v) != 0) {
fprintf(stderr, "initializing module: %s\n", name);
fatal("can't initialize module");
}
DECREF(v);
}
DECREF(m);
return m; /* Yes, it still exists, in sys.modules... */
return m;
}

View file

@ -1,24 +1,25 @@
/* Python interpreter main program */
#include <stdio.h>
#include <ctype.h>
#include "string.h"
#include "allobjects.h"
extern char *getpythonpath();
#include "PROTO.h"
#include "grammar.h"
#include "node.h"
#include "parsetok.h"
#include "graminit.h"
#include "errcode.h"
#include "object.h"
#include "stringobject.h"
#include "sysmodule.h"
#include "compile.h"
#include "ceval.h"
#include "pythonrun.h"
#include "import.h"
extern char *getpythonpath();
extern grammar gram; /* From graminit.c */
int debugging;
#ifdef DEBUG
int debugging; /* Needed by parser.c */
#endif
main(argc, argv)
int argc;
@ -26,15 +27,12 @@ main(argc, argv)
{
char *filename = NULL;
FILE *fp = stdin;
int ret;
initargs(&argc, &argv);
initintr(); /* For intrcheck() */
initargs(&argc, &argv);
if (argc > 1 && strcmp(argv[1], "-") != 0)
filename = argv[1];
if (filename != NULL) {
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "python: can't open file '%s'\n",
@ -43,89 +41,339 @@ main(argc, argv)
}
}
/* XXX what is the ideal initialization order? */
/* XXX exceptions are initialized by initrun but this
may be too late */
initsys(argc-1, argv+1);
inittime();
initmath();
initall();
setpythonpath(getpythonpath());
setpythonargv(argc-1, argv+1);
initrun();
initcalls();
if (!isatty(fileno(fp))) {
ret = runfile(fp, file_input, (char *)NULL, (char *)NULL);
}
else {
object *v, *w;
sysset("ps1", v = newstringobject(">>> "));
sysset("ps2", w = newstringobject("... "));
DECREF(v);
DECREF(w);
for (;;) {
char *ps1 = NULL, *ps2 = NULL;
v = sysget("ps1");
w = sysget("ps2");
if (v != NULL && is_stringobject(v)) {
INCREF(v);
ps1 = getstringvalue(v);
}
else
v = NULL;
if (w != NULL && is_stringobject(w)) {
INCREF(w);
ps2 = getstringvalue(w);
}
else
w = NULL;
ret = runfile(fp, single_input, ps1, ps2);
if (v != NULL)
DECREF(v);
if (w != NULL)
DECREF(w);
if (ret == E_EOF || ret == E_NOMEM)
break;
}
}
goaway(ret == E_DONE || ret == E_EOF ? 0 : 1);
goaway(run(fp, filename == NULL ? "<stdin>" : filename));
/*NOTREACHED*/
}
goaway(sts)
int sts;
/* Initialize all */
void
initall()
{
#ifdef THINK_C
if (sts == 0)
Click_On(0);
#endif
closerun();
donecalls();
exit(sts);
/*NOTREACHED*/
static int inited;
if (inited)
return;
initimport();
initbuiltin(); /* Also initializes builtin exceptions */
initsys();
inittime();
initmath();
initcalls(); /* Configuration-dependent initializations */
initintr(); /* For intrcheck() */
inited = 1;
}
/* Parse input from a file and execute it */
static int
runfile(fp, start, ps1, ps2)
int
run(fp, filename)
FILE *fp;
int start;
char *filename;
{
if (filename == NULL)
filename = "???";
if (isatty(fileno(fp)))
return run_tty_loop(fp, filename);
else
return run_script(fp, filename);
}
int
run_tty_loop(fp, filename)
FILE *fp;
char *filename;
{
object *v;
int ret;
v = sysget("ps1");
if (v == NULL) {
sysset("ps1", v = newstringobject(">>> "));
XDECREF(v);
}
v = sysget("ps2");
if (v == NULL) {
sysset("ps2", v = newstringobject("... "));
XDECREF(v);
}
for (;;) {
ret = run_tty_1(fp, filename);
#ifdef REF_DEBUG
fprintf(stderr, "[%ld refs]\n", ref_total);
#endif
if (ret == E_EOF)
return 0;
/*
if (ret == E_NOMEM)
return -1;
*/
}
}
int
run_tty_1(fp, filename)
FILE *fp;
char *filename;
{
object *m, *d, *v, *w;
node *n;
char *ps1, *ps2;
int err;
v = sysget("ps1");
w = sysget("ps2");
if (v != NULL && is_stringobject(v)) {
INCREF(v);
ps1 = getstringvalue(v);
}
else {
v = NULL;
ps1 = "";
}
if (w != NULL && is_stringobject(w)) {
INCREF(w);
ps2 = getstringvalue(w);
}
else {
w = NULL;
ps2 = "";
}
err = parsefile(fp, filename, &gram, single_input, ps1, ps2, &n);
XDECREF(v);
XDECREF(w);
if (err == E_EOF)
return E_EOF;
if (err != E_DONE) {
err_input(err);
print_error();
return err;
}
m = add_module("__main__");
if (m == NULL)
return -1;
d = getmoduledict(m);
v = run_node(n, filename, d, d);
flushline();
if (v == NULL) {
print_error();
return -1;
}
DECREF(v);
return 0;
}
int
run_script(fp, filename)
FILE *fp;
char *filename;
{
object *m, *d, *v;
m = add_module("__main__");
if (m == NULL)
return -1;
d = getmoduledict(m);
v = run_file(fp, filename, file_input, d, d);
flushline();
if (v == NULL) {
print_error();
return -1;
}
DECREF(v);
return 0;
}
void
print_error()
{
object *exception, *v;
err_get(&exception, &v);
fprintf(stderr, "Unhandled exception: ");
printobject(exception, stderr, PRINT_RAW);
if (v != NULL && v != None) {
fprintf(stderr, ": ");
printobject(v, stderr, PRINT_RAW);
}
fprintf(stderr, "\n");
XDECREF(exception);
XDECREF(v);
printtraceback(stderr);
}
object *
run_string(str, start, globals, locals)
char *str;
int start;
/*dict*/object *globals, *locals;
{
node *n;
int ret;
ret = parsefile(fp, &gram, start, ps1, ps2, &n);
if (ret != E_DONE)
return ret;
return execute(n) == 0 ? E_DONE : E_ERROR;
int err;
err = parse_string(str, start, &n);
return run_err_node(err, n, "<string>", globals, locals);
}
object *
run_file(fp, filename, start, globals, locals)
FILE *fp;
char *filename;
int start;
/*dict*/object *globals, *locals;
{
node *n;
int err;
err = parse_file(fp, filename, start, &n);
return run_err_node(err, n, filename, globals, locals);
}
object *
run_err_node(err, n, filename, globals, locals)
int err;
node *n;
char *filename;
/*dict*/object *globals, *locals;
{
if (err != E_DONE) {
err_input(err);
return NULL;
}
return run_node(n, filename, globals, locals);
}
object *
run_node(n, filename, globals, locals)
node *n;
char *filename;
/*dict*/object *globals, *locals;
{
if (globals == NULL) {
globals = getglobals();
if (locals == NULL)
locals = getlocals();
}
else {
if (locals == NULL)
locals = globals;
}
return eval_node(n, filename, globals, locals);
}
object *
eval_node(n, filename, globals, locals)
node *n;
char *filename;
object *globals;
object *locals;
{
codeobject *co;
object *v;
co = compile(n, filename);
freetree(n);
if (co == NULL)
return NULL;
v = eval_code(co, globals, locals, (object *)NULL);
DECREF(co);
return v;
}
/* Simplified interface to parsefile */
int
parse_file(fp, filename, start, n_ret)
FILE *fp;
char *filename;
int start;
node **n_ret;
{
return parsefile(fp, filename, &gram, start,
(char *)0, (char *)0, n_ret);
}
/* Simplified interface to parsestring */
int
parse_string(str, start, n_ret)
char *str;
int start;
node **n_ret;
{
int err = parsestring(str, &gram, start, n_ret);
/* Don't confuse early end of string with early end of input */
if (err == E_EOF)
err = E_SYNTAX;
return err;
}
/* Print fatal error message and abort */
void
fatal(msg)
char *msg;
{
fprintf(stderr, "Fatal error: %s\n", msg);
abort();
}
/* Clean up and exit */
void
goaway(sts)
int sts;
{
flushline();
/* XXX Call doneimport() before donecalls(), since donecalls()
calls wdone(), and doneimport() may close windows */
doneimport();
donecalls();
err_clear();
#ifdef REF_DEBUG
fprintf(stderr, "[%ld refs]\n", ref_total);
#endif
#ifdef THINK_C
if (sts == 0)
Click_On(0);
#endif
#ifdef TRACE_REFS
if (askyesno("Print left references?")) {
#ifdef THINK_C
Click_On(1);
#endif
printrefs(stderr);
}
#endif /* TRACE_REFS */
exit(sts);
/*NOTREACHED*/
}
static
finaloutput()
{
#ifdef TRACE_REFS
if (!askyesno("Print left references?"))
return;
#ifdef THINK_C
Click_On(1);
#endif /* THINK_C */
printrefs(stderr);
#endif /* TRACE_REFS */
}
/* Ask a yes/no question */
int
static int
askyesno(prompt)
char *prompt;
{
@ -151,15 +399,14 @@ isatty(fd)
#endif
/* WISH LIST
/* XXX WISH LIST
- improved per-module error handling; different use of errno
- possible new types:
- iterator (for range, keys, ...)
- improve interpreter error handling, e.g., true tracebacks
- release parse trees when no longer needed (make them objects?)
- faster parser (for long modules)
- save precompiled modules on file?
- fork threads, locking
- allow syntax extensions
*/
/* "Floccinaucinihilipilification" */

134
Python/structmember.c Normal file
View file

@ -0,0 +1,134 @@
/* Map C struct members to Python object attributes */
#include "allobjects.h"
#include "structmember.h"
object *
getmember(addr, mlist, name)
char *addr;
struct memberlist *mlist;
char *name;
{
struct memberlist *l;
for (l = mlist; l->name != NULL; l++) {
if (strcmp(l->name, name) == 0) {
object *v;
addr += l->offset;
switch (l->type) {
case T_SHORT:
v = newintobject((long) *(short*)addr);
break;
case T_INT:
v = newintobject((long) *(int*)addr);
break;
case T_LONG:
v = newintobject(*(long*)addr);
break;
case T_FLOAT:
v = newfloatobject((double)*(float*)addr);
break;
case T_DOUBLE:
v = newfloatobject(*(double*)addr);
break;
case T_STRING:
if (*(char**)addr == NULL) {
INCREF(None);
v = None;
}
else
v = newstringobject(*(char**)addr);
break;
case T_OBJECT:
v = *(object **)addr;
if (v == NULL)
v = None;
INCREF(v);
break;
default:
err_setstr(SystemError, "bad memberlist type");
v = NULL;
}
return v;
}
}
err_setstr(NameError, name);
return NULL;
}
int
setmember(addr, mlist, name, v)
char *addr;
struct memberlist *mlist;
char *name;
object *v;
{
struct memberlist *l;
for (l = mlist; l->name != NULL; l++) {
if (strcmp(l->name, name) == 0) {
if (l->readonly || l->type == T_STRING) {
err_setstr(RuntimeError, "readonly attribute");
return -1;
}
addr += l->offset;
switch (l->type) {
case T_SHORT:
if (!is_intobject(v)) {
err_badarg();
return -1;
}
*(short*)addr = getintvalue(v);
break;
case T_INT:
if (!is_intobject(v)) {
err_badarg();
return -1;
}
*(int*)addr = getintvalue(v);
break;
case T_LONG:
if (!is_intobject(v)) {
err_badarg();
return -1;
}
*(long*)addr = getintvalue(v);
break;
case T_FLOAT:
if (is_intobject(v))
*(float*)addr = getintvalue(v);
else if (is_floatobject(v))
*(float*)addr = getfloatvalue(v);
else {
err_badarg();
return -1;
}
break;
case T_DOUBLE:
if (is_intobject(v))
*(double*)addr = getintvalue(v);
else if (is_floatobject(v))
*(double*)addr = getfloatvalue(v);
else {
err_badarg();
return -1;
}
break;
case T_OBJECT:
XDECREF(*(object **)addr);
XINCREF(v);
*(object **)addr = v;
break;
default:
err_setstr(SystemError, "bad memberlist type");
return -1;
}
return 0;
}
}
err_setstr(NameError, name);
return NULL;
}

View file

@ -3,31 +3,31 @@
/*
Various bits of information used by the interpreter are collected in
module 'sys'.
Function member:
- exit(sts): call (C, POSIX) exit(sts)
Data members:
- stdin, stdout, stderr: standard file objects
- ps1, ps2: primary and secondary prompts (strings)
- path: module search path (list of strings)
- modules: the table of modules (dictionary)
Function members:
- exit(sts): call exit()
- path: module search path (list of strings)
- argv: script arguments (list of strings)
- ps1, ps2: optional primary and secondary prompts (strings)
*/
#include <stdio.h>
#include "allobjects.h"
#include "PROTO.h"
#include "object.h"
#include "stringobject.h"
#include "listobject.h"
#include "dictobject.h"
#include "fileobject.h"
#include "moduleobject.h"
#include "sysmodule.h"
#include "node.h" /* For context.h */
#include "context.h" /* For import.h */
#include "import.h"
#include "methodobject.h"
#include "modsupport.h"
#include "errors.h"
/* Define delimiter used in $PYTHONPATH */
#ifdef THINK_C
#define DELIM ' '
#endif
#ifndef DELIM
#define DELIM ':'
#endif
static object *sysdict;
@ -63,34 +63,6 @@ sysset(name, v)
return dictinsert(sysdict, name, v);
}
static object *
makeargv(argc, argv)
int argc;
char **argv;
{
int i;
object *av, *v;
if (argc < 0 || argv == NULL)
argc = 0;
av = newlistobject(argc);
if (av != NULL) {
for (i = 0; i < argc; i++) {
v = newstringobject(argv[i]);
if (v == NULL) {
DECREF(av);
av = NULL;
break;
}
setlistitem(av, i, v);
}
}
if (av == NULL)
fatal("no mem for sys.argv");
return av;
}
/* sys.exit method */
static object *
sys_exit(self, args)
object *self;
@ -104,88 +76,115 @@ sys_exit(self, args)
/* NOTREACHED */
}
static struct methodlist sys_methods[] = {
{"exit", sys_exit},
{NULL, NULL} /* sentinel */
};
static object *sysin, *sysout, *syserr;
void
initsys(argc, argv)
int argc;
char **argv;
initsys()
{
object *v;
object *exit;
if ((sysdict = newdictobject()) == NULL)
fatal("can't create sys dict");
object *m = initmodule("sys", sys_methods);
sysdict = getmoduledict(m);
INCREF(sysdict);
/* NB keep an extra ref to the std files to avoid closing them
when the user deletes them */
/* XXX File objects should have a "don't close" flag instead */
sysin = newopenfileobject(stdin, "<stdin>", "r");
sysout = newopenfileobject(stdout, "<stdout>", "w");
syserr = newopenfileobject(stderr, "<stderr>", "w");
v = makeargv(argc, argv);
exit = newmethodobject("exit", sys_exit, (object *)NULL);
if (err_occurred())
fatal("can't create sys.* objects");
fatal("can't create sys.std* file objects");
dictinsert(sysdict, "stdin", sysin);
dictinsert(sysdict, "stdout", sysout);
dictinsert(sysdict, "stderr", syserr);
dictinsert(sysdict, "argv", v);
dictinsert(sysdict, "exit", exit);
dictinsert(sysdict, "modules", get_modules());
if (err_occurred())
fatal("can't insert sys.* objects in sys dict");
DECREF(exit);
DECREF(v);
/* The other symbols are added elsewhere */
/* Only now can we initialize the import stuff, after which
we can turn ourselves into a module */
initimport();
if ((v = new_module("sys")) == NULL)
fatal("can't create sys module");
if (setmoduledict(v, sysdict) != 0)
fatal("can't assign sys dict to sys module");
DECREF(v);
}
static void
cleardict(d)
object *d;
static object *
makepathobject(path, delim)
char *path;
int delim;
{
int i;
for (i = getdictsize(d); --i >= 0; ) {
char *k;
k = getdictkey(d, i);
if (k != NULL) {
(void) dictremove(d, k);
}
int i, n;
char *p;
object *v, *w;
n = 1;
p = path;
while ((p = strchr(p, delim)) != NULL) {
n++;
p++;
}
v = newlistobject(n);
if (v == NULL)
return NULL;
for (i = 0; ; i++) {
p = strchr(path, delim);
if (p == NULL)
p = strchr(path, '\0'); /* End of string */
w = newsizedstringobject(path, (int) (p - path));
if (w == NULL) {
DECREF(v);
return NULL;
}
setlistitem(v, i, w);
if (*p == '\0')
break;
path = p+1;
}
return v;
}
void
closesys()
setpythonpath(path)
char *path;
{
object *modules;
modules = sysget("modules");
if (modules != NULL && is_dictobject(modules)) {
int i;
/* Explicitly erase all modules; this is the safest way
to get rid of at least *some* circular dependencies */
INCREF(modules);
for (i = getdictsize(modules); --i >= 0; ) {
char *k;
k = getdictkey(modules, i);
if (k != NULL) {
object *m;
m = dictlookup(modules, k);
if (m != NULL && is_moduleobject(m)) {
object *d;
d = getmoduledict(m);
if (d != NULL && is_dictobject(d)) {
cleardict(d);
}
}
}
}
cleardict(modules);
DECREF(modules);
}
DECREF(sysdict);
object *v;
if ((v = makepathobject(path, DELIM)) == NULL)
fatal("can't create sys.path");
if (sysset("path", v) != 0)
fatal("can't assign sys.path");
DECREF(v);
}
static object *
makeargvobject(argc, argv)
int argc;
char **argv;
{
object *av;
if (argc < 0 || argv == NULL)
argc = 0;
av = newlistobject(argc);
if (av != NULL) {
int i;
for (i = 0; i < argc; i++) {
object *v = newstringobject(argv[i]);
if (v == NULL) {
DECREF(av);
av = NULL;
break;
}
setlistitem(av, i, v);
}
}
return av;
}
void
setpythonargv(argc, argv)
int argc;
char **argv;
{
object *av = makeargvobject(argc, argv);
if (av == NULL)
fatal("no mem for sys.argv");
if (sysset("argv", av) != 0)
fatal("can't assign sys.argv");
DECREF(av);
}

193
Python/traceback.c Normal file
View file

@ -0,0 +1,193 @@
/* Traceback implementation */
#include "allobjects.h"
#include "compile.h"
#include "frameobject.h"
#include "traceback.h"
#include "structmember.h"
typedef struct _tracebackobject {
OB_HEAD
struct _tracebackobject *tb_next;
frameobject *tb_frame;
int tb_lasti;
int tb_lineno;
} tracebackobject;
#define OFF(x) offsetof(tracebackobject, x)
static struct memberlist tb_memberlist[] = {
{"tb_next", T_OBJECT, OFF(tb_next)},
{"tb_frame", T_OBJECT, OFF(tb_frame)},
{"tb_lasti", T_INT, OFF(tb_lasti)},
{"tb_lineno", T_INT, OFF(tb_lineno)},
{NULL} /* Sentinel */
};
static object *
tb_getattr(tb, name)
tracebackobject *tb;
char *name;
{
return getmember((char *)tb, tb_memberlist, name);
}
static void
tb_dealloc(tb)
tracebackobject *tb;
{
XDECREF(tb->tb_next);
XDECREF(tb->tb_frame);
DEL(tb);
}
static typeobject Tracebacktype = {
OB_HEAD_INIT(&Typetype)
0,
"traceback",
sizeof(tracebackobject),
0,
tb_dealloc, /*tp_dealloc*/
0, /*tp_print*/
tb_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
};
#define is_tracebackobject(v) ((v)->ob_type == &Tracebacktype)
static tracebackobject *
newtracebackobject(next, frame, lasti, lineno)
tracebackobject *next;
frameobject *frame;
int lasti, lineno;
{
tracebackobject *tb;
if ((next != NULL && !is_tracebackobject(next)) ||
frame == NULL || !is_frameobject(frame)) {
err_badcall();
return NULL;
}
tb = NEWOBJ(tracebackobject, &Tracebacktype);
if (tb != NULL) {
XINCREF(next);
tb->tb_next = next;
XINCREF(frame);
tb->tb_frame = frame;
tb->tb_lasti = lasti;
tb->tb_lineno = lineno;
}
return tb;
}
static tracebackobject *tb_current = NULL;
int
tb_here(frame, lasti, lineno)
frameobject *frame;
int lasti;
int lineno;
{
tracebackobject *tb;
tb = newtracebackobject(tb_current, frame, lasti, lineno);
if (tb == NULL)
return -1;
XDECREF(tb_current);
tb_current = tb;
return 0;
}
object *
tb_fetch()
{
object *v;
v = (object *)tb_current;
tb_current = NULL;
return v;
}
int
tb_store(v)
object *v;
{
if (v != NULL && !is_tracebackobject(v)) {
err_badcall();
return -1;
}
XDECREF(tb_current);
XINCREF(v);
tb_current = (tracebackobject *)v;
return 0;
}
static void
tb_displayline(fp, filename, lineno)
FILE *fp;
char *filename;
int lineno;
{
FILE *xfp;
char buf[1000];
int i;
if (filename[0] == '<' && filename[strlen(filename)-1] == '>')
return;
xfp = fopen(filename, "r");
if (xfp == NULL) {
fprintf(fp, " (cannot open \"%s\")\n", filename);
return;
}
for (i = 0; i < lineno; i++) {
if (fgets(buf, sizeof buf, xfp) == NULL)
break;
}
if (i == lineno) {
char *p = buf;
while (*p == ' ' || *p == '\t')
p++;
fprintf(fp, " %s", p);
if (strchr(p, '\n') == NULL)
fprintf(fp, "\n");
}
fclose(xfp);
}
static void
tb_printinternal(tb, fp)
tracebackobject *tb;
FILE *fp;
{
while (tb != NULL) {
if (intrcheck()) {
fprintf(fp, "[interrupted]\n");
break;
}
fprintf(fp, " File \"");
printobject(tb->tb_frame->f_code->co_filename, fp, PRINT_RAW);
fprintf(fp, "\", line %d\n", tb->tb_lineno);
tb_displayline(fp,
getstringvalue(tb->tb_frame->f_code->co_filename),
tb->tb_lineno);
tb = tb->tb_next;
}
}
int
tb_print(v, fp)
object *v;
FILE *fp;
{
if (v == NULL)
return 0;
if (!is_tracebackobject(v)) {
err_badcall();
return -1;
}
sysset("last_traceback", v);
tb_printinternal((tracebackobject *)v, fp);
return 0;
}