1991-02-19 12:39:46 +00:00
|
|
|
/***********************************************************
|
1993-03-16 12:15:04 +00:00
|
|
|
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
|
|
|
Amsterdam, The Netherlands.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior permission.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
|
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
/* Built-in functions */
|
|
|
|
|
|
|
|
#include "allobjects.h"
|
|
|
|
|
|
|
|
#include "node.h"
|
|
|
|
#include "graminit.h"
|
|
|
|
#include "errcode.h"
|
|
|
|
#include "sysmodule.h"
|
1991-01-21 15:12:35 +00:00
|
|
|
#include "bltinmodule.h"
|
1990-12-20 15:06:42 +00:00
|
|
|
#include "import.h"
|
|
|
|
#include "pythonrun.h"
|
|
|
|
#include "ceval.h"
|
|
|
|
#include "modsupport.h"
|
1993-03-30 17:46:03 +00:00
|
|
|
#include "compile.h"
|
|
|
|
#include "eval.h"
|
1990-12-20 15:06:42 +00:00
|
|
|
|
|
|
|
static object *
|
|
|
|
builtin_abs(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
1991-05-05 20:00:36 +00:00
|
|
|
number_methods *nm;
|
|
|
|
if (v == NULL || (nm = v->ob_type->tp_as_number) == NULL) {
|
|
|
|
err_setstr(TypeError, "abs() requires numeric argument");
|
|
|
|
return NULL;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
1991-05-05 20:00:36 +00:00
|
|
|
return (*nm->nb_absolute)(v);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1991-12-16 13:03:00 +00:00
|
|
|
static object *
|
1992-08-14 15:14:30 +00:00
|
|
|
builtin_apply(self, args)
|
1991-12-16 13:03:00 +00:00
|
|
|
object *self;
|
1992-08-14 15:14:30 +00:00
|
|
|
object *args;
|
1991-12-16 13:03:00 +00:00
|
|
|
{
|
1992-08-14 15:14:30 +00:00
|
|
|
object *func, *arglist;
|
|
|
|
if (!getargs(args, "(OO)", &func, &arglist))
|
1991-12-16 13:03:00 +00:00
|
|
|
return NULL;
|
1992-08-14 15:14:30 +00:00
|
|
|
return call_object(func, arglist);
|
1991-12-16 13:03:00 +00:00
|
|
|
}
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
static object *
|
1992-08-14 15:14:30 +00:00
|
|
|
builtin_chr(self, args)
|
1990-12-20 15:06:42 +00:00
|
|
|
object *self;
|
1992-08-14 15:14:30 +00:00
|
|
|
object *args;
|
1990-12-20 15:06:42 +00:00
|
|
|
{
|
|
|
|
long x;
|
|
|
|
char s[1];
|
1992-08-14 15:14:30 +00:00
|
|
|
if (!getargs(args, "l", &x))
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
|
|
|
if (x < 0 || x >= 256) {
|
1991-12-10 13:52:31 +00:00
|
|
|
err_setstr(ValueError, "chr() arg not in range(256)");
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s[0] = x;
|
|
|
|
return newsizedstringobject(s, 1);
|
|
|
|
}
|
|
|
|
|
1992-10-18 18:53:57 +00:00
|
|
|
static object *
|
|
|
|
builtin_cmp(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *a, *b;
|
|
|
|
if (!getargs(args, "(OO)", &a, &b))
|
|
|
|
return NULL;
|
|
|
|
return newintobject((long)cmpobject(a, b));
|
|
|
|
}
|
|
|
|
|
1992-08-12 15:35:34 +00:00
|
|
|
static object *
|
|
|
|
builtin_coerce(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v, *w;
|
|
|
|
object *res;
|
|
|
|
|
|
|
|
if (!getargs(args, "(OO)", &v, &w))
|
|
|
|
return NULL;
|
|
|
|
if (coerce(&v, &w) < 0)
|
|
|
|
return NULL;
|
|
|
|
res = mkvalue("(OO)", v, w);
|
|
|
|
DECREF(v);
|
|
|
|
DECREF(w);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1993-03-30 17:46:03 +00:00
|
|
|
static object *
|
|
|
|
builtin_compile(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
char *filename;
|
|
|
|
char *startstr;
|
|
|
|
int start;
|
|
|
|
if (!getargs(args, "(sss)", &str, &filename, &startstr))
|
|
|
|
return NULL;
|
|
|
|
if (strcmp(startstr, "exec") == 0)
|
|
|
|
start = file_input;
|
|
|
|
else if (strcmp(startstr, "eval") == 0)
|
|
|
|
start = eval_input;
|
|
|
|
else {
|
|
|
|
err_setstr(ValueError,
|
|
|
|
"compile() mode must be 'exec' or 'eval'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return compile_string(str, filename, start);
|
|
|
|
}
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
static object *
|
|
|
|
builtin_dir(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
object *d;
|
|
|
|
if (v == NULL) {
|
|
|
|
d = getlocals();
|
1991-10-20 20:11:03 +00:00
|
|
|
INCREF(d);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
else {
|
1991-10-20 20:11:03 +00:00
|
|
|
d = getattr(v, "__dict__");
|
|
|
|
if (d == NULL) {
|
1990-12-20 15:06:42 +00:00
|
|
|
err_setstr(TypeError,
|
1991-10-24 14:54:44 +00:00
|
|
|
"dir() argument must have __dict__ attribute");
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1991-10-24 14:54:44 +00:00
|
|
|
if (is_dictobject(d)) {
|
1991-10-20 20:11:03 +00:00
|
|
|
v = getdictkeys(d);
|
|
|
|
if (sortlist(v) != 0) {
|
|
|
|
DECREF(v);
|
|
|
|
v = NULL;
|
|
|
|
}
|
|
|
|
}
|
1991-10-24 14:54:44 +00:00
|
|
|
else {
|
|
|
|
v = newlistobject(0);
|
|
|
|
}
|
1991-10-20 20:11:03 +00:00
|
|
|
DECREF(d);
|
1990-12-20 15:06:42 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1991-07-01 18:42:41 +00:00
|
|
|
builtin_divmod(self, args)
|
1990-12-20 15:06:42 +00:00
|
|
|
object *self;
|
1991-07-01 18:42:41 +00:00
|
|
|
object *args;
|
1990-12-20 15:06:42 +00:00
|
|
|
{
|
1991-07-01 18:42:41 +00:00
|
|
|
object *v, *w, *x;
|
1992-08-14 15:14:30 +00:00
|
|
|
if (!getargs(args, "(OO)", &v, &w))
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
1991-07-01 18:42:41 +00:00
|
|
|
if (v->ob_type->tp_as_number == NULL ||
|
|
|
|
w->ob_type->tp_as_number == NULL) {
|
1991-05-05 20:00:36 +00:00
|
|
|
err_setstr(TypeError, "divmod() requires numeric arguments");
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-07-01 18:42:41 +00:00
|
|
|
if (coerce(&v, &w) != 0)
|
|
|
|
return NULL;
|
|
|
|
x = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
|
|
|
|
DECREF(v);
|
|
|
|
DECREF(w);
|
|
|
|
return x;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
exec_eval(v, start)
|
|
|
|
object *v;
|
|
|
|
int start;
|
|
|
|
{
|
|
|
|
object *str = NULL, *globals = NULL, *locals = NULL;
|
1992-03-04 16:41:41 +00:00
|
|
|
char *s;
|
1990-12-20 15:06:42 +00:00
|
|
|
int n;
|
|
|
|
if (v != NULL) {
|
1993-03-30 17:46:03 +00:00
|
|
|
if (is_tupleobject(v) &&
|
1990-12-20 15:06:42 +00:00
|
|
|
((n = gettuplesize(v)) == 2 || n == 3)) {
|
|
|
|
str = gettupleitem(v, 0);
|
|
|
|
globals = gettupleitem(v, 1);
|
|
|
|
if (n == 3)
|
|
|
|
locals = gettupleitem(v, 2);
|
|
|
|
}
|
1993-03-30 17:46:03 +00:00
|
|
|
else
|
|
|
|
str = v;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
1993-03-30 17:46:03 +00:00
|
|
|
if (str == NULL || (!is_stringobject(str) && !is_codeobject(str)) ||
|
1990-12-20 15:06:42 +00:00
|
|
|
globals != NULL && !is_dictobject(globals) ||
|
|
|
|
locals != NULL && !is_dictobject(locals)) {
|
|
|
|
err_setstr(TypeError,
|
1993-03-30 17:46:03 +00:00
|
|
|
"exec/eval arguments must be (string|code)[,dict[,dict]]");
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1993-03-30 17:46:03 +00:00
|
|
|
if (is_codeobject(str))
|
|
|
|
return eval_code((codeobject *) str, globals, locals,
|
|
|
|
(object *)NULL);
|
1992-03-04 16:41:41 +00:00
|
|
|
s = getstringvalue(str);
|
1992-08-14 15:14:30 +00:00
|
|
|
if (strlen(s) != getstringsize(str)) {
|
|
|
|
err_setstr(ValueError, "embedded '\\0' in string arg");
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-03-04 16:41:41 +00:00
|
|
|
if (start == eval_input) {
|
|
|
|
while (*s == ' ' || *s == '\t')
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return run_string(s, start, globals, locals);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1992-02-25 18:55:05 +00:00
|
|
|
static object *
|
|
|
|
builtin_execfile(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
object *str = NULL, *globals = NULL, *locals = NULL, *w;
|
|
|
|
FILE* fp;
|
1992-08-14 15:14:30 +00:00
|
|
|
char *s;
|
1992-02-25 18:55:05 +00:00
|
|
|
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,
|
|
|
|
"execfile arguments must be filename[,dict[,dict]]");
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-19 16:40:53 +00:00
|
|
|
s = getstringvalue(str);
|
1992-08-14 15:14:30 +00:00
|
|
|
if (strlen(s) != getstringsize(str)) {
|
|
|
|
err_setstr(ValueError, "embedded '\\0' in string arg");
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
1992-08-14 15:14:30 +00:00
|
|
|
fp = fopen(s, "r");
|
1992-08-05 19:58:53 +00:00
|
|
|
END_SAVE
|
1992-02-25 18:55:05 +00:00
|
|
|
if (fp == NULL) {
|
|
|
|
err_setstr(IOError, "execfile cannot open the file argument");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
w = run_file(fp, getstringvalue(str), file_input, globals, locals);
|
1992-08-05 19:58:53 +00:00
|
|
|
BGN_SAVE
|
1992-02-25 18:55:05 +00:00
|
|
|
fclose(fp);
|
1992-08-05 19:58:53 +00:00
|
|
|
END_SAVE
|
1992-02-25 18:55:05 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
static object *
|
|
|
|
builtin_float(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
1992-09-12 11:09:23 +00:00
|
|
|
number_methods *nb;
|
|
|
|
|
|
|
|
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
|
|
|
|
nb->nb_float == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"float() argument can't be converted to float");
|
|
|
|
return NULL;
|
1992-08-12 15:35:34 +00:00
|
|
|
}
|
1992-09-12 11:09:23 +00:00
|
|
|
return (*nb->nb_float)(v);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1992-01-27 16:53:09 +00:00
|
|
|
static object *
|
1992-08-14 15:14:30 +00:00
|
|
|
builtin_getattr(self, args)
|
1992-01-27 16:53:09 +00:00
|
|
|
object *self;
|
1992-08-14 15:14:30 +00:00
|
|
|
object *args;
|
1992-01-27 16:53:09 +00:00
|
|
|
{
|
1992-08-14 15:14:30 +00:00
|
|
|
object *v;
|
1993-03-29 10:43:31 +00:00
|
|
|
object *name;
|
|
|
|
if (!getargs(args, "(OS)", &v, &name))
|
1992-01-27 16:53:09 +00:00
|
|
|
return NULL;
|
1993-03-29 10:43:31 +00:00
|
|
|
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);
|
1992-01-27 16:53:09 +00:00
|
|
|
}
|
|
|
|
|
1993-03-30 17:46:03 +00:00
|
|
|
static object *
|
|
|
|
builtin_id(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v;
|
|
|
|
if (!getargs(args, "O", &v))
|
|
|
|
return NULL;
|
|
|
|
return newintobject((long)v);
|
|
|
|
}
|
|
|
|
|
1992-01-27 16:53:09 +00:00
|
|
|
static object *
|
1992-08-14 15:14:30 +00:00
|
|
|
builtin_setattr(self, args)
|
1992-01-27 16:53:09 +00:00
|
|
|
object *self;
|
1992-08-14 15:14:30 +00:00
|
|
|
object *args;
|
1992-01-27 16:53:09 +00:00
|
|
|
{
|
1992-08-14 15:14:30 +00:00
|
|
|
object *v;
|
1993-03-29 10:43:31 +00:00
|
|
|
object *name;
|
1992-08-14 15:14:30 +00:00
|
|
|
object *value;
|
1993-03-29 10:43:31 +00:00
|
|
|
if (!getargs(args, "(OSO)", &v, &name, &value))
|
1992-01-27 16:53:09 +00:00
|
|
|
return NULL;
|
1993-03-29 10:43:31 +00:00
|
|
|
if (setattro(v, name, value) != 0)
|
1992-01-27 16:53:09 +00:00
|
|
|
return NULL;
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1993-03-29 10:43:31 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1991-10-24 14:54:44 +00:00
|
|
|
static object *
|
|
|
|
builtin_hex(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
1992-09-12 11:09:23 +00:00
|
|
|
number_methods *nb;
|
|
|
|
|
|
|
|
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
|
|
|
|
nb->nb_hex == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"hex() argument can't be converted to hex");
|
|
|
|
return NULL;
|
1991-10-24 14:54:44 +00:00
|
|
|
}
|
1992-09-12 11:09:23 +00:00
|
|
|
return (*nb->nb_hex)(v);
|
1991-10-24 14:54:44 +00:00
|
|
|
}
|
|
|
|
|
1992-09-25 21:59:05 +00:00
|
|
|
static object *builtin_raw_input PROTO((object *, object *));
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
static object *
|
|
|
|
builtin_input(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
1992-09-25 21:59:05 +00:00
|
|
|
object *line = builtin_raw_input(self, v);
|
|
|
|
if (line == NULL)
|
|
|
|
return line;
|
|
|
|
v = exec_eval(line, eval_input);
|
|
|
|
DECREF(line);
|
|
|
|
return v;
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
builtin_int(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
1992-09-12 11:09:23 +00:00
|
|
|
number_methods *nb;
|
|
|
|
|
|
|
|
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
|
|
|
|
nb->nb_int == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"int() argument can't be converted to int");
|
|
|
|
return NULL;
|
1992-08-12 15:35:34 +00:00
|
|
|
}
|
1992-09-12 11:09:23 +00:00
|
|
|
return (*nb->nb_int)(v);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
1992-08-12 15:35:34 +00:00
|
|
|
if (len < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return newintobject(len);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1991-05-05 20:00:36 +00:00
|
|
|
static object *
|
|
|
|
builtin_long(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
1992-09-12 11:09:23 +00:00
|
|
|
number_methods *nb;
|
|
|
|
|
|
|
|
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
|
|
|
|
nb->nb_long == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"long() argument can't be converted to long");
|
|
|
|
return NULL;
|
1992-08-12 15:35:34 +00:00
|
|
|
}
|
1992-09-12 11:09:23 +00:00
|
|
|
return (*nb->nb_long)(v);
|
1991-05-05 20:00:36 +00:00
|
|
|
}
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
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);
|
1992-11-26 10:30:26 +00:00
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
1990-12-20 15:06:42 +00:00
|
|
|
if (n == 0) {
|
1991-12-10 13:52:31 +00:00
|
|
|
err_setstr(ValueError, "min() or max() of empty sequence");
|
1990-12-20 15:06:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1991-10-24 14:54:44 +00:00
|
|
|
static object *
|
|
|
|
builtin_oct(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
1992-09-12 11:09:23 +00:00
|
|
|
number_methods *nb;
|
|
|
|
|
|
|
|
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
|
|
|
|
nb->nb_oct == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"oct() argument can't be converted to oct");
|
|
|
|
return NULL;
|
1991-10-24 14:54:44 +00:00
|
|
|
}
|
1992-09-12 11:09:23 +00:00
|
|
|
return (*nb->nb_oct)(v);
|
1991-10-24 14:54:44 +00:00
|
|
|
}
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
static object *
|
1992-08-14 15:14:30 +00:00
|
|
|
builtin_open(self, args)
|
1990-12-20 15:06:42 +00:00
|
|
|
object *self;
|
1992-08-14 15:14:30 +00:00
|
|
|
object *args;
|
1990-12-20 15:06:42 +00:00
|
|
|
{
|
1992-08-14 15:14:30 +00:00
|
|
|
char *name, *mode;
|
|
|
|
if (!getargs(args, "(ss)", &name, &mode))
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
1992-08-14 15:14:30 +00:00
|
|
|
return newfileobject(name, mode);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1992-08-14 15:14:30 +00:00
|
|
|
builtin_ord(self, args)
|
1990-12-20 15:06:42 +00:00
|
|
|
object *self;
|
1992-08-14 15:14:30 +00:00
|
|
|
object *args;
|
1990-12-20 15:06:42 +00:00
|
|
|
{
|
1992-08-14 15:14:30 +00:00
|
|
|
char *s;
|
|
|
|
int len;
|
|
|
|
if (!getargs(args, "s#", &s, &len))
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
1992-08-14 15:14:30 +00:00
|
|
|
if (len != 1) {
|
1991-12-10 13:52:31 +00:00
|
|
|
err_setstr(ValueError, "ord() arg must have length 1");
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-14 15:14:30 +00:00
|
|
|
return newintobject((long)(s[0] & 0xff));
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1991-05-05 20:00:36 +00:00
|
|
|
static object *
|
1991-07-01 18:42:41 +00:00
|
|
|
builtin_pow(self, args)
|
1991-05-05 20:00:36 +00:00
|
|
|
object *self;
|
1991-07-01 18:42:41 +00:00
|
|
|
object *args;
|
1991-05-05 20:00:36 +00:00
|
|
|
{
|
1991-07-01 18:42:41 +00:00
|
|
|
object *v, *w, *x;
|
1992-08-14 15:14:30 +00:00
|
|
|
if (!getargs(args, "(OO)", &v, &w))
|
1991-05-05 20:00:36 +00:00
|
|
|
return NULL;
|
1991-07-01 18:42:41 +00:00
|
|
|
if (v->ob_type->tp_as_number == NULL ||
|
|
|
|
w->ob_type->tp_as_number == NULL) {
|
1991-05-05 20:00:36 +00:00
|
|
|
err_setstr(TypeError, "pow() requires numeric arguments");
|
|
|
|
return NULL;
|
|
|
|
}
|
1991-07-01 18:42:41 +00:00
|
|
|
if (coerce(&v, &w) != 0)
|
|
|
|
return NULL;
|
|
|
|
x = (*v->ob_type->tp_as_number->nb_power)(v, w);
|
|
|
|
DECREF(v);
|
|
|
|
DECREF(w);
|
|
|
|
return x;
|
1991-05-05 20:00:36 +00:00
|
|
|
}
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
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) {
|
1991-12-10 13:52:31 +00:00
|
|
|
err_setstr(ValueError, "zero step for range()");
|
1990-12-20 15:06:42 +00:00
|
|
|
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;
|
|
|
|
{
|
1992-09-25 21:59:05 +00:00
|
|
|
object *f = sysget("stdout");
|
|
|
|
if (f == NULL) {
|
|
|
|
err_setstr(RuntimeError, "lost sys.stdout");
|
|
|
|
return NULL;
|
|
|
|
}
|
1990-12-20 15:06:42 +00:00
|
|
|
flushline();
|
1991-06-07 16:10:43 +00:00
|
|
|
if (v != NULL) {
|
1992-09-25 21:59:05 +00:00
|
|
|
if (writeobject(v, f, PRINT_RAW) != 0)
|
1991-06-07 16:10:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-04-04 15:20:41 +00:00
|
|
|
return filegetline(sysget("stdin"), -1);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
builtin_reload(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
return reload_module(v);
|
|
|
|
}
|
|
|
|
|
1992-11-26 08:54:07 +00:00
|
|
|
static object *
|
|
|
|
builtin_repr(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (v == NULL) {
|
|
|
|
err_badarg();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return reprobject(v);
|
|
|
|
}
|
|
|
|
|
1993-02-12 16:29:05 +00:00
|
|
|
static object *
|
|
|
|
builtin_round(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
extern double floor PROTO((double));
|
|
|
|
extern double ceil PROTO((double));
|
|
|
|
double x;
|
|
|
|
double f;
|
|
|
|
int ndigits = 0;
|
|
|
|
int sign = 1;
|
|
|
|
int i;
|
|
|
|
if (!getargs(args, "d", &x)) {
|
|
|
|
err_clear();
|
|
|
|
if (!getargs(args, "(di)", &x, &ndigits))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
f = 1.0;
|
|
|
|
for (i = ndigits; --i >= 0; )
|
|
|
|
f = f*10.0;
|
|
|
|
for (i = ndigits; ++i <= 0; )
|
|
|
|
f = f*0.1;
|
|
|
|
if (x >= 0.0)
|
|
|
|
return newfloatobject(floor(x*f + 0.5) / f);
|
|
|
|
else
|
|
|
|
return newfloatobject(ceil(x*f - 0.5) / f);
|
|
|
|
}
|
|
|
|
|
1992-11-26 08:54:07 +00:00
|
|
|
static object *
|
|
|
|
builtin_str(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (v == NULL) {
|
|
|
|
err_badarg();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (is_stringobject(v)) {
|
|
|
|
INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return reprobject(v);
|
|
|
|
}
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
static object *
|
|
|
|
builtin_type(self, v)
|
|
|
|
object *self;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (v == NULL) {
|
1992-11-26 08:54:07 +00:00
|
|
|
err_setstr(TypeError, "type() requires an argument");
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
v = (object *)v->ob_type;
|
|
|
|
INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct methodlist builtin_methods[] = {
|
1991-12-16 13:03:00 +00:00
|
|
|
{"abs", builtin_abs},
|
|
|
|
{"apply", builtin_apply},
|
|
|
|
{"chr", builtin_chr},
|
1992-10-18 18:53:57 +00:00
|
|
|
{"cmp", builtin_cmp},
|
1992-08-12 15:35:34 +00:00
|
|
|
{"coerce", builtin_coerce},
|
1993-03-30 17:46:03 +00:00
|
|
|
{"compile", builtin_compile},
|
1991-12-16 13:03:00 +00:00
|
|
|
{"dir", builtin_dir},
|
|
|
|
{"divmod", builtin_divmod},
|
|
|
|
{"eval", builtin_eval},
|
|
|
|
{"exec", builtin_exec},
|
1992-02-25 18:55:05 +00:00
|
|
|
{"execfile", builtin_execfile},
|
1991-12-16 13:03:00 +00:00
|
|
|
{"float", builtin_float},
|
1992-01-27 16:53:09 +00:00
|
|
|
{"getattr", builtin_getattr},
|
1993-03-29 10:43:31 +00:00
|
|
|
{"hasattr", builtin_hasattr},
|
|
|
|
{"hash", builtin_hash},
|
1991-12-16 13:03:00 +00:00
|
|
|
{"hex", builtin_hex},
|
1993-03-30 17:46:03 +00:00
|
|
|
{"id", builtin_id},
|
1991-12-16 13:03:00 +00:00
|
|
|
{"input", builtin_input},
|
|
|
|
{"int", builtin_int},
|
|
|
|
{"len", builtin_len},
|
|
|
|
{"long", builtin_long},
|
|
|
|
{"max", builtin_max},
|
|
|
|
{"min", builtin_min},
|
|
|
|
{"oct", builtin_oct},
|
1992-02-05 11:17:52 +00:00
|
|
|
{"open", builtin_open},
|
1991-12-16 13:03:00 +00:00
|
|
|
{"ord", builtin_ord},
|
|
|
|
{"pow", builtin_pow},
|
|
|
|
{"range", builtin_range},
|
|
|
|
{"raw_input", builtin_raw_input},
|
|
|
|
{"reload", builtin_reload},
|
1992-11-26 08:54:07 +00:00
|
|
|
{"repr", builtin_repr},
|
1993-02-12 16:29:05 +00:00
|
|
|
{"round", builtin_round},
|
1992-01-27 16:53:09 +00:00
|
|
|
{"setattr", builtin_setattr},
|
1992-11-26 08:54:07 +00:00
|
|
|
{"str", builtin_str},
|
1991-12-16 13:03:00 +00:00
|
|
|
{"type", builtin_type},
|
|
|
|
{NULL, NULL},
|
1990-12-20 15:06:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static object *builtin_dict;
|
|
|
|
|
|
|
|
object *
|
|
|
|
getbuiltin(name)
|
1991-08-16 08:54:58 +00:00
|
|
|
object *name;
|
1990-12-20 15:06:42 +00:00
|
|
|
{
|
1991-08-16 08:54:58 +00:00
|
|
|
return dict2lookup(builtin_dict, name);
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Predefined exceptions */
|
|
|
|
|
1991-12-10 13:52:31 +00:00
|
|
|
object *AttributeError;
|
1991-12-16 15:42:38 +00:00
|
|
|
object *EOFError;
|
1991-12-10 13:52:31 +00:00
|
|
|
object *IOError;
|
1991-12-24 13:24:53 +00:00
|
|
|
object *ImportError;
|
1991-12-10 13:52:31 +00:00
|
|
|
object *IndexError;
|
|
|
|
object *KeyError;
|
1991-12-16 15:42:38 +00:00
|
|
|
object *KeyboardInterrupt;
|
|
|
|
object *MemoryError;
|
|
|
|
object *NameError;
|
1991-12-10 13:52:31 +00:00
|
|
|
object *OverflowError;
|
1991-12-16 15:42:38 +00:00
|
|
|
object *RuntimeError;
|
1991-12-16 13:03:00 +00:00
|
|
|
object *SyntaxError;
|
1991-12-16 15:42:38 +00:00
|
|
|
object *SystemError;
|
1991-12-31 13:13:47 +00:00
|
|
|
object *SystemExit;
|
1991-12-16 15:42:38 +00:00
|
|
|
object *TypeError;
|
|
|
|
object *ValueError;
|
|
|
|
object *ZeroDivisionError;
|
1991-12-10 13:52:31 +00:00
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
static object *
|
1991-12-16 15:42:38 +00:00
|
|
|
newstdexception(name)
|
|
|
|
char *name;
|
1990-12-20 15:06:42 +00:00
|
|
|
{
|
1991-12-16 15:42:38 +00:00
|
|
|
object *v = newstringobject(name);
|
1990-12-20 15:06:42 +00:00
|
|
|
if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
|
|
|
|
fatal("no mem for new standard exception");
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initerrors()
|
|
|
|
{
|
1991-12-16 15:42:38 +00:00
|
|
|
AttributeError = newstdexception("AttributeError");
|
1991-12-24 13:24:53 +00:00
|
|
|
EOFError = newstdexception("EOFError");
|
1991-12-16 15:42:38 +00:00
|
|
|
IOError = newstdexception("IOError");
|
1991-12-24 13:24:53 +00:00
|
|
|
ImportError = newstdexception("ImportError");
|
1991-12-16 15:42:38 +00:00
|
|
|
IndexError = newstdexception("IndexError");
|
|
|
|
KeyError = newstdexception("KeyError");
|
1991-12-24 13:24:53 +00:00
|
|
|
KeyboardInterrupt = newstdexception("KeyboardInterrupt");
|
|
|
|
MemoryError = newstdexception("MemoryError");
|
|
|
|
NameError = newstdexception("NameError");
|
1991-12-16 15:42:38 +00:00
|
|
|
OverflowError = newstdexception("OverflowError");
|
1991-12-24 13:24:53 +00:00
|
|
|
RuntimeError = newstdexception("RuntimeError");
|
1991-12-16 15:42:38 +00:00
|
|
|
SyntaxError = newstdexception("SyntaxError");
|
1991-12-24 13:24:53 +00:00
|
|
|
SystemError = newstdexception("SystemError");
|
1991-12-31 13:13:47 +00:00
|
|
|
SystemExit = newstdexception("SystemExit");
|
1991-12-24 13:24:53 +00:00
|
|
|
TypeError = newstdexception("TypeError");
|
|
|
|
ValueError = newstdexception("ValueError");
|
|
|
|
ZeroDivisionError = newstdexception("ZeroDivisionError");
|
1990-12-20 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
initbuiltin()
|
|
|
|
{
|
|
|
|
object *m;
|
|
|
|
m = initmodule("builtin", builtin_methods);
|
|
|
|
builtin_dict = getmoduledict(m);
|
|
|
|
INCREF(builtin_dict);
|
|
|
|
initerrors();
|
|
|
|
(void) dictinsert(builtin_dict, "None", None);
|
|
|
|
}
|
1991-07-01 18:42:41 +00:00
|
|
|
|
|
|
|
/* Coerce two numeric types to the "larger" one.
|
|
|
|
Increment the reference count on each argument.
|
|
|
|
Return -1 and raise an exception if no coercion is possible
|
|
|
|
(and then no reference count is incremented).
|
1992-08-14 12:06:52 +00:00
|
|
|
*/
|
1991-07-01 18:42:41 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
coerce(pv, pw)
|
|
|
|
object **pv, **pw;
|
|
|
|
{
|
|
|
|
register object *v = *pv;
|
|
|
|
register object *w = *pw;
|
1992-08-14 12:06:52 +00:00
|
|
|
int res;
|
|
|
|
|
1993-02-12 16:29:05 +00:00
|
|
|
if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
|
1991-07-01 18:42:41 +00:00
|
|
|
INCREF(v);
|
|
|
|
INCREF(w);
|
|
|
|
return 0;
|
|
|
|
}
|
1992-08-14 12:06:52 +00:00
|
|
|
if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
|
|
|
|
res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
|
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
1991-07-01 18:42:41 +00:00
|
|
|
}
|
1992-08-14 12:06:52 +00:00
|
|
|
if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
|
|
|
|
res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
|
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
1991-07-01 18:42:41 +00:00
|
|
|
}
|
1992-08-14 12:06:52 +00:00
|
|
|
err_setstr(TypeError, "number coercion failed");
|
|
|
|
return -1;
|
1991-07-01 18:42:41 +00:00
|
|
|
}
|