1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
1995-01-04 15:12:13 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 08:39:46 -04: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 11:06:42 -04:00
|
|
|
/* Built-in functions */
|
|
|
|
|
|
|
|
#include "allobjects.h"
|
|
|
|
|
|
|
|
#include "node.h"
|
|
|
|
#include "graminit.h"
|
1991-01-21 11:12:35 -04:00
|
|
|
#include "bltinmodule.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "import.h"
|
1993-03-30 13:46:03 -04:00
|
|
|
#include "compile.h"
|
|
|
|
#include "eval.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1996-08-08 15:49:41 -03:00
|
|
|
#include "mymath.h"
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
/* Forward */
|
|
|
|
static object *filterstring PROTO((object *, object *));
|
|
|
|
static object *filtertuple PROTO((object *, object *));
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin___import__(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *name;
|
1995-02-14 05:42:43 -04:00
|
|
|
object *globals = NULL;
|
|
|
|
object *locals = NULL;
|
|
|
|
object *fromlist = NULL;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1995-02-14 05:42:43 -04:00
|
|
|
if (!newgetargs(args, "s|OOO:__import__",
|
|
|
|
&name, &globals, &locals, &fromlist))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1995-01-20 12:53:12 -04:00
|
|
|
return import_module(name);
|
1995-01-02 15:04:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static object *
|
|
|
|
builtin_abs(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
1991-05-05 17:00:36 -03:00
|
|
|
number_methods *nm;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:abs", &v))
|
|
|
|
return NULL;
|
|
|
|
if ((nm = v->ob_type->tp_as_number) == NULL) {
|
1991-05-05 17:00:36 -03:00
|
|
|
err_setstr(TypeError, "abs() requires numeric argument");
|
|
|
|
return NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1991-05-05 17:00:36 -03:00
|
|
|
return (*nm->nb_absolute)(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1991-12-16 09:03:00 -04:00
|
|
|
static object *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_apply(self, args)
|
1991-12-16 09:03:00 -04:00
|
|
|
object *self;
|
1992-08-14 12:14:30 -03:00
|
|
|
object *args;
|
1991-12-16 09:03:00 -04:00
|
|
|
{
|
1996-01-26 16:44:30 -04:00
|
|
|
object *func, *alist = NULL, *kwdict = NULL;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1995-07-18 11:51:37 -03:00
|
|
|
if (!newgetargs(args, "O|OO:apply", &func, &alist, &kwdict))
|
1991-12-16 09:03:00 -04:00
|
|
|
return NULL;
|
1995-07-18 11:51:37 -03:00
|
|
|
if (alist != NULL && !is_tupleobject(alist)) {
|
1994-08-29 09:52:16 -03:00
|
|
|
err_setstr(TypeError, "apply() 2nd argument must be tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-07-18 11:51:37 -03:00
|
|
|
if (kwdict != NULL && !is_dictobject(kwdict)) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"apply() 3rd argument must be dictionary");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyEval_CallObjectWithKeywords(func, alist, kwdict);
|
1991-12-16 09:03:00 -04:00
|
|
|
}
|
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
static object *
|
|
|
|
builtin_callable(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "O:callable", &v))
|
1994-08-29 09:52:16 -03:00
|
|
|
return NULL;
|
1995-01-02 15:04:15 -04:00
|
|
|
return newintobject((long)callable(v));
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
static object *
|
1993-11-03 11:01:26 -04:00
|
|
|
builtin_filter(self, args)
|
1993-10-26 14:58:25 -03:00
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
1993-10-27 11:56:44 -03:00
|
|
|
object *func, *seq, *result;
|
1993-10-26 14:58:25 -03:00
|
|
|
sequence_methods *sqf;
|
1993-10-27 11:56:44 -03:00
|
|
|
int len;
|
|
|
|
register int i, j;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1995-01-02 15:04:15 -04:00
|
|
|
if (!newgetargs(args, "OO:filter", &func, &seq))
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (is_stringobject(seq)) {
|
|
|
|
object *r = filterstring(func, seq);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1993-10-27 11:56:44 -03:00
|
|
|
if (is_tupleobject(seq)) {
|
1993-10-26 14:58:25 -03:00
|
|
|
object *r = filtertuple(func, seq);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1993-10-27 11:56:44 -03:00
|
|
|
if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
|
1993-10-26 14:58:25 -03:00
|
|
|
err_setstr(TypeError,
|
1993-11-03 11:01:26 -04:00
|
|
|
"argument 2 to filter() must be a sequence type");
|
1993-10-26 14:58:25 -03:00
|
|
|
goto Fail_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((len = (*sqf->sq_length)(seq)) < 0)
|
|
|
|
goto Fail_2;
|
|
|
|
|
|
|
|
if (is_listobject(seq) && seq->ob_refcnt == 1) {
|
|
|
|
INCREF(seq);
|
|
|
|
result = seq;
|
|
|
|
}
|
1993-10-27 11:56:44 -03:00
|
|
|
else {
|
1993-10-26 14:58:25 -03:00
|
|
|
if ((result = newlistobject(len)) == NULL)
|
|
|
|
goto Fail_2;
|
1993-10-27 11:56:44 -03:00
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
for (i = j = 0; ; ++i) {
|
1993-10-27 11:56:44 -03:00
|
|
|
object *item, *good;
|
|
|
|
int ok;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
|
|
|
|
if (i < len)
|
|
|
|
goto Fail_1;
|
|
|
|
if (err_occurred() == IndexError) {
|
|
|
|
err_clear();
|
|
|
|
break;
|
|
|
|
}
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_1;
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1993-10-27 11:56:44 -03:00
|
|
|
if (func == None) {
|
|
|
|
good = item;
|
1995-01-10 13:40:55 -04:00
|
|
|
INCREF(good);
|
1993-10-27 11:56:44 -03:00
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
else {
|
1993-10-27 11:56:44 -03:00
|
|
|
object *arg = mkvalue("(O)", item);
|
|
|
|
if (arg == NULL)
|
|
|
|
goto Fail_1;
|
|
|
|
good = call_object(func, arg);
|
|
|
|
DECREF(arg);
|
1995-01-10 13:40:55 -04:00
|
|
|
if (good == NULL) {
|
|
|
|
DECREF(item);
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_1;
|
1995-01-10 13:40:55 -04:00
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
1993-10-27 11:56:44 -03:00
|
|
|
ok = testbool(good);
|
|
|
|
DECREF(good);
|
|
|
|
if (ok) {
|
1994-08-29 09:52:16 -03:00
|
|
|
if (j < len) {
|
|
|
|
if (setlistitem(result, j++, item) < 0)
|
|
|
|
goto Fail_1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
j++;
|
|
|
|
if (addlistitem(result, item) < 0)
|
|
|
|
goto Fail_1;
|
|
|
|
}
|
1995-01-10 13:40:55 -04:00
|
|
|
} else {
|
|
|
|
DECREF(item);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
if (j < len && setlistslice(result, j, len, NULL) < 0)
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_1;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail_1:
|
|
|
|
DECREF(result);
|
|
|
|
Fail_2:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_chr(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1992-08-14 12:14:30 -03:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
long x;
|
|
|
|
char s[1];
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "l:chr", &x))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
if (x < 0 || x >= 256) {
|
1991-12-10 09:52:31 -04:00
|
|
|
err_setstr(ValueError, "chr() arg not in range(256)");
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s[0] = x;
|
|
|
|
return newsizedstringobject(s, 1);
|
|
|
|
}
|
|
|
|
|
1992-10-18 15:53:57 -03:00
|
|
|
static object *
|
|
|
|
builtin_cmp(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *a, *b;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "OO:cmp", &a, &b))
|
1992-10-18 15:53:57 -03:00
|
|
|
return NULL;
|
|
|
|
return newintobject((long)cmpobject(a, b));
|
|
|
|
}
|
|
|
|
|
1995-01-07 08:39:01 -04:00
|
|
|
static object *
|
|
|
|
builtin_coerce(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v, *w;
|
1995-01-10 11:26:20 -04:00
|
|
|
object *res;
|
1995-01-07 08:39:01 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "OO:coerce", &v, &w))
|
|
|
|
return NULL;
|
1995-01-10 11:26:20 -04:00
|
|
|
if (coerce(&v, &w) < 0)
|
|
|
|
return NULL;
|
|
|
|
res = mkvalue("(OO)", v, w);
|
|
|
|
DECREF(v);
|
|
|
|
DECREF(w);
|
|
|
|
return res;
|
1995-01-07 08:39:01 -04:00
|
|
|
}
|
|
|
|
|
1993-03-30 13:46:03 -04:00
|
|
|
static object *
|
|
|
|
builtin_compile(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
char *filename;
|
|
|
|
char *startstr;
|
|
|
|
int start;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "sss:compile", &str, &filename, &startstr))
|
1993-03-30 13:46:03 -04:00
|
|
|
return NULL;
|
|
|
|
if (strcmp(startstr, "exec") == 0)
|
|
|
|
start = file_input;
|
|
|
|
else if (strcmp(startstr, "eval") == 0)
|
|
|
|
start = eval_input;
|
1995-07-07 19:43:42 -03:00
|
|
|
else if (strcmp(startstr, "single") == 0)
|
|
|
|
start = single_input;
|
1993-03-30 13:46:03 -04:00
|
|
|
else {
|
|
|
|
err_setstr(ValueError,
|
1995-07-07 19:43:42 -03:00
|
|
|
"compile() mode must be 'exec' or 'eval' or 'single'");
|
1993-03-30 13:46:03 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return compile_string(str, filename, start);
|
|
|
|
}
|
|
|
|
|
1996-01-11 21:09:56 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
|
|
|
|
static object *
|
|
|
|
builtin_complex(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
1996-08-08 15:49:41 -03:00
|
|
|
object *r, *i, *tmp;
|
1996-01-11 21:09:56 -04:00
|
|
|
number_methods *nbr, *nbi;
|
1996-07-20 23:27:43 -03:00
|
|
|
Py_complex cr, ci;
|
1996-01-11 21:09:56 -04:00
|
|
|
|
|
|
|
i = NULL;
|
|
|
|
if (!newgetargs(args, "O|O:complex", &r, &i))
|
|
|
|
return NULL;
|
|
|
|
if ((nbr = r->ob_type->tp_as_number) == NULL ||
|
|
|
|
nbr->nb_float == NULL || (i != NULL &&
|
|
|
|
((nbi = i->ob_type->tp_as_number) == NULL ||
|
|
|
|
nbi->nb_float == NULL))) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"complex() argument can't be converted to complex");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (is_complexobject(r))
|
|
|
|
cr = ((complexobject*)r)->cval;
|
|
|
|
else {
|
1996-08-08 15:49:41 -03:00
|
|
|
tmp = (*nbr->nb_float)(r);
|
|
|
|
if (tmp == NULL)
|
|
|
|
return NULL;
|
|
|
|
cr.real = getfloatvalue(tmp);
|
|
|
|
DECREF(tmp);
|
1996-01-11 21:09:56 -04:00
|
|
|
cr.imag = 0.;
|
|
|
|
}
|
|
|
|
if (i == NULL) {
|
|
|
|
ci.real = 0.;
|
|
|
|
ci.imag = 0.;
|
|
|
|
}
|
|
|
|
else if (is_complexobject(i))
|
|
|
|
ci = ((complexobject*)i)->cval;
|
|
|
|
else {
|
1996-09-07 12:55:27 -03:00
|
|
|
tmp = (*nbr->nb_float)(i);
|
1996-08-08 15:49:41 -03:00
|
|
|
if (tmp == NULL)
|
|
|
|
return NULL;
|
|
|
|
ci.real = getfloatvalue(tmp);
|
|
|
|
DECREF(tmp);
|
1996-01-11 21:09:56 -04:00
|
|
|
ci.imag = 0.;
|
|
|
|
}
|
|
|
|
cr.real -= ci.imag;
|
|
|
|
cr.imag += ci.real;
|
|
|
|
return newcomplexobject(cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_dir(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v = NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
object *d;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "|O:dir", &v))
|
|
|
|
return NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL) {
|
|
|
|
d = getlocals();
|
1991-10-20 17:11:03 -03:00
|
|
|
INCREF(d);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
else {
|
1991-10-20 17:11:03 -03:00
|
|
|
d = getattr(v, "__dict__");
|
|
|
|
if (d == NULL) {
|
1990-12-20 11:06:42 -04:00
|
|
|
err_setstr(TypeError,
|
1991-10-24 11:54:44 -03:00
|
|
|
"dir() argument must have __dict__ attribute");
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1991-10-24 11:54:44 -03:00
|
|
|
if (is_dictobject(d)) {
|
1991-10-20 17:11:03 -03:00
|
|
|
v = getdictkeys(d);
|
|
|
|
if (sortlist(v) != 0) {
|
|
|
|
DECREF(v);
|
|
|
|
v = NULL;
|
|
|
|
}
|
|
|
|
}
|
1991-10-24 11:54:44 -03:00
|
|
|
else {
|
1996-05-23 19:49:07 -03:00
|
|
|
v = PyObject_CallMethod(d, "keys", NULL);
|
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
v = newlistobject(0);
|
|
|
|
}
|
1991-10-24 11:54:44 -03:00
|
|
|
}
|
1991-10-20 17:11:03 -03:00
|
|
|
DECREF(d);
|
1990-12-20 11:06:42 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1995-01-07 08:39:01 -04:00
|
|
|
do_divmod(v, w)
|
|
|
|
object *v, *w;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-07 08:39:01 -04:00
|
|
|
object *res;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1994-09-29 06:45:57 -03:00
|
|
|
if (is_instanceobject(v) || is_instanceobject(w))
|
1995-01-07 08:39:01 -04:00
|
|
|
return instancebinop(v, w, "__divmod__", "__rdivmod__",
|
|
|
|
do_divmod);
|
1991-07-01 15:42:41 -03:00
|
|
|
if (v->ob_type->tp_as_number == NULL ||
|
|
|
|
w->ob_type->tp_as_number == NULL) {
|
1995-01-02 15:04:15 -04:00
|
|
|
err_setstr(TypeError,
|
|
|
|
"divmod() requires numeric or class instance arguments");
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-07-01 15:42:41 -03:00
|
|
|
if (coerce(&v, &w) != 0)
|
|
|
|
return NULL;
|
1995-01-07 08:39:01 -04:00
|
|
|
res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
|
1991-07-01 15:42:41 -03:00
|
|
|
DECREF(v);
|
|
|
|
DECREF(w);
|
1995-01-07 08:39:01 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
builtin_divmod(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v, *w;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "OO:divmod", &v, &w))
|
|
|
|
return NULL;
|
|
|
|
return do_divmod(v, w);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_eval(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *cmd;
|
1995-01-10 06:47:05 -04:00
|
|
|
object *globals = None, *locals = None;
|
1995-01-02 15:04:15 -04:00
|
|
|
char *str;
|
1993-11-30 09:40:46 -04:00
|
|
|
|
1995-01-02 15:04:15 -04:00
|
|
|
if (!newgetargs(args, "O|O!O!:eval",
|
|
|
|
&cmd,
|
|
|
|
&Mappingtype, &globals,
|
|
|
|
&Mappingtype, &locals))
|
|
|
|
return NULL;
|
1995-01-10 06:47:05 -04:00
|
|
|
if (globals == None) {
|
1995-01-09 13:53:26 -04:00
|
|
|
globals = getglobals();
|
1995-01-10 06:47:05 -04:00
|
|
|
if (locals == None)
|
|
|
|
locals = getlocals();
|
1995-01-09 13:53:26 -04:00
|
|
|
}
|
1995-01-10 06:47:05 -04:00
|
|
|
else if (locals == None)
|
1995-01-09 13:53:26 -04:00
|
|
|
locals = globals;
|
|
|
|
if (dictlookup(globals, "__builtins__") == NULL) {
|
|
|
|
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-02 15:04:15 -04:00
|
|
|
if (is_codeobject(cmd))
|
1995-07-18 11:51:37 -03:00
|
|
|
return eval_code((codeobject *) cmd, globals, locals);
|
1995-01-02 15:04:15 -04:00
|
|
|
if (!is_stringobject(cmd)) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"eval() argument 1 must be string or code object");
|
1992-08-14 12:14:30 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-02 15:04:15 -04:00
|
|
|
str = getstringvalue(cmd);
|
|
|
|
if (strlen(str) != getstringsize(cmd)) {
|
|
|
|
err_setstr(ValueError,
|
|
|
|
"embedded '\\0' in string arg");
|
|
|
|
return NULL;
|
1992-03-04 12:41:41 -04:00
|
|
|
}
|
1995-01-02 15:04:15 -04:00
|
|
|
while (*str == ' ' || *str == '\t')
|
|
|
|
str++;
|
|
|
|
return run_string(str, eval_input, globals, locals);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1992-02-25 14:55:05 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_execfile(self, args)
|
1992-02-25 14:55:05 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1992-02-25 14:55:05 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
char *filename;
|
1995-01-10 06:47:05 -04:00
|
|
|
object *globals = None, *locals = None;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *res;
|
1992-02-25 14:55:05 -04:00
|
|
|
FILE* fp;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "s|O!O!:execfile",
|
|
|
|
&filename,
|
|
|
|
&Mappingtype, &globals,
|
|
|
|
&Mappingtype, &locals))
|
1992-08-14 12:14:30 -03:00
|
|
|
return NULL;
|
1995-01-10 06:47:05 -04:00
|
|
|
if (globals == None) {
|
1995-01-09 13:53:26 -04:00
|
|
|
globals = getglobals();
|
1995-01-10 06:47:05 -04:00
|
|
|
if (locals == None)
|
|
|
|
locals = getlocals();
|
1995-01-09 13:53:26 -04:00
|
|
|
}
|
1995-01-10 06:47:05 -04:00
|
|
|
else if (locals == None)
|
1995-01-09 13:53:26 -04:00
|
|
|
locals = globals;
|
|
|
|
if (dictlookup(globals, "__builtins__") == NULL) {
|
|
|
|
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-05 16:58:53 -03:00
|
|
|
BGN_SAVE
|
1995-01-02 15:04:15 -04:00
|
|
|
fp = fopen(filename, "r");
|
1992-08-05 16:58:53 -03:00
|
|
|
END_SAVE
|
1992-02-25 14:55:05 -04:00
|
|
|
if (fp == NULL) {
|
1995-01-02 15:04:15 -04:00
|
|
|
err_errno(IOError);
|
1992-02-25 14:55:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-02 15:04:15 -04:00
|
|
|
res = run_file(fp, filename, file_input, globals, locals);
|
1992-08-05 16:58:53 -03:00
|
|
|
BGN_SAVE
|
1992-02-25 14:55:05 -04:00
|
|
|
fclose(fp);
|
1992-08-05 16:58:53 -03:00
|
|
|
END_SAVE
|
1995-01-02 15:04:15 -04:00
|
|
|
return res;
|
1992-02-25 14:55:05 -04:00
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_float(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
number_methods *nb;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:float", &v))
|
|
|
|
return NULL;
|
|
|
|
if ((nb = v->ob_type->tp_as_number) == NULL ||
|
1992-09-12 08:09:23 -03:00
|
|
|
nb->nb_float == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"float() argument can't be converted to float");
|
|
|
|
return NULL;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1992-09-12 08:09:23 -03:00
|
|
|
return (*nb->nb_float)(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1992-01-27 12:53:09 -04:00
|
|
|
static object *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_getattr(self, args)
|
1992-01-27 12:53:09 -04:00
|
|
|
object *self;
|
1992-08-14 12:14:30 -03:00
|
|
|
object *args;
|
1992-01-27 12:53:09 -04:00
|
|
|
{
|
1992-08-14 12:14:30 -03:00
|
|
|
object *v;
|
1993-03-29 06:43:31 -04:00
|
|
|
object *name;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "OS:getattr", &v, &name))
|
1992-01-27 12:53:09 -04:00
|
|
|
return NULL;
|
1993-03-29 06:43:31 -04:00
|
|
|
return getattro(v, name);
|
|
|
|
}
|
|
|
|
|
1995-07-07 19:43:42 -03:00
|
|
|
static object *
|
|
|
|
builtin_globals(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *d;
|
|
|
|
|
|
|
|
if (!newgetargs(args, ""))
|
|
|
|
return NULL;
|
|
|
|
d = getglobals();
|
|
|
|
INCREF(d);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static object *
|
|
|
|
builtin_hasattr(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v;
|
|
|
|
object *name;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "OS:hasattr", &v, &name))
|
1993-03-29 06:43:31 -04:00
|
|
|
return NULL;
|
|
|
|
v = getattro(v, name);
|
|
|
|
if (v == NULL) {
|
|
|
|
err_clear();
|
|
|
|
return newintobject(0L);
|
|
|
|
}
|
|
|
|
DECREF(v);
|
|
|
|
return newintobject(1L);
|
1992-01-27 12:53:09 -04:00
|
|
|
}
|
|
|
|
|
1993-03-30 13:46:03 -04:00
|
|
|
static object *
|
|
|
|
builtin_id(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:id", &v))
|
1993-03-30 13:46:03 -04:00
|
|
|
return NULL;
|
|
|
|
return newintobject((long)v);
|
|
|
|
}
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
static object *
|
|
|
|
builtin_map(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
typedef struct {
|
|
|
|
object *seq;
|
|
|
|
sequence_methods *sqf;
|
|
|
|
int len;
|
|
|
|
} sequence;
|
|
|
|
|
|
|
|
object *func, *result;
|
|
|
|
sequence *seqs = NULL, *sqp;
|
1995-01-02 15:04:15 -04:00
|
|
|
int n, len;
|
1993-10-26 14:58:25 -03:00
|
|
|
register int i, j;
|
|
|
|
|
1995-01-02 15:04:15 -04:00
|
|
|
n = gettuplesize(args);
|
|
|
|
if (n < 2) {
|
1993-10-26 14:58:25 -03:00
|
|
|
err_setstr(TypeError, "map() requires at least two args");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
func = gettupleitem(args, 0);
|
1995-01-02 15:04:15 -04:00
|
|
|
n--;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1993-10-27 11:56:44 -03:00
|
|
|
if ((seqs = NEW(sequence, n)) == NULL) {
|
|
|
|
err_nomem();
|
|
|
|
goto Fail_2;
|
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
|
1993-10-26 14:58:25 -03:00
|
|
|
int curlen;
|
|
|
|
|
|
|
|
if ((sqp->seq = gettupleitem(args, i + 1)) == NULL)
|
|
|
|
goto Fail_2;
|
|
|
|
|
|
|
|
if (! (sqp->sqf = sqp->seq->ob_type->tp_as_sequence)) {
|
|
|
|
static char errmsg[] =
|
|
|
|
"argument %d to map() must be a sequence object";
|
|
|
|
char errbuf[sizeof(errmsg) + 3];
|
|
|
|
|
|
|
|
sprintf(errbuf, errmsg, i+2);
|
|
|
|
err_setstr(TypeError, errbuf);
|
|
|
|
goto Fail_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
|
|
|
|
goto Fail_2;
|
|
|
|
|
|
|
|
if (curlen > len)
|
|
|
|
len = curlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((result = (object *) newlistobject(len)) == NULL)
|
|
|
|
goto Fail_2;
|
|
|
|
|
1993-10-27 11:56:44 -03:00
|
|
|
/* XXX Special case map(None, single_list) could be more efficient */
|
1994-08-29 09:52:16 -03:00
|
|
|
for (i = 0; ; ++i) {
|
1995-07-10 10:52:21 -03:00
|
|
|
object *alist, *item, *value;
|
1994-08-29 09:52:16 -03:00
|
|
|
int any = 0;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
if (func == None && n == 1)
|
1995-07-10 10:52:21 -03:00
|
|
|
alist = NULL;
|
1994-08-29 09:52:16 -03:00
|
|
|
else {
|
1995-07-10 10:52:21 -03:00
|
|
|
if ((alist = newtupleobject(n)) == NULL)
|
1994-08-29 09:52:16 -03:00
|
|
|
goto Fail_1;
|
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
|
1994-08-29 09:52:16 -03:00
|
|
|
if (sqp->len < 0) {
|
1993-10-26 14:58:25 -03:00
|
|
|
INCREF(None);
|
1993-10-27 11:56:44 -03:00
|
|
|
item = None;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
else {
|
1993-10-27 11:56:44 -03:00
|
|
|
item = (*sqp->sqf->sq_item)(sqp->seq, i);
|
1994-08-29 09:52:16 -03:00
|
|
|
if (item == NULL) {
|
|
|
|
if (i < sqp->len)
|
|
|
|
goto Fail_0;
|
|
|
|
if (err_occurred() == IndexError) {
|
|
|
|
err_clear();
|
|
|
|
INCREF(None);
|
|
|
|
item = None;
|
|
|
|
sqp->len = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto Fail_0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
any = 1;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
}
|
1995-07-10 10:52:21 -03:00
|
|
|
if (!alist)
|
1994-08-29 09:52:16 -03:00
|
|
|
break;
|
1995-07-10 10:52:21 -03:00
|
|
|
if (settupleitem(alist, j, item) < 0) {
|
1994-08-29 09:52:16 -03:00
|
|
|
DECREF(item);
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_0;
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
1993-10-27 11:56:44 -03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Fail_0:
|
1995-07-10 10:52:21 -03:00
|
|
|
XDECREF(alist);
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_1;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1995-07-10 10:52:21 -03:00
|
|
|
if (!alist)
|
|
|
|
alist = item;
|
1994-08-29 09:52:16 -03:00
|
|
|
|
|
|
|
if (!any) {
|
1995-07-10 10:52:21 -03:00
|
|
|
DECREF(alist);
|
1994-08-29 09:52:16 -03:00
|
|
|
break;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
1994-08-29 09:52:16 -03:00
|
|
|
|
|
|
|
if (func == None)
|
1995-07-10 10:52:21 -03:00
|
|
|
value = alist;
|
1993-10-26 14:58:25 -03:00
|
|
|
else {
|
1995-07-10 10:52:21 -03:00
|
|
|
value = call_object(func, alist);
|
|
|
|
DECREF(alist);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (value == NULL)
|
|
|
|
goto Fail_1;
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
|
|
|
if (i >= len) {
|
|
|
|
if (addlistitem(result, value) < 0)
|
|
|
|
goto Fail_1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (setlistitem(result, i, value) < 0)
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_1;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
DEL(seqs);
|
1993-10-26 14:58:25 -03:00
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail_1:
|
|
|
|
DECREF(result);
|
|
|
|
Fail_2:
|
1993-10-27 11:56:44 -03:00
|
|
|
if (seqs) DEL(seqs);
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1992-01-27 12:53:09 -04:00
|
|
|
static object *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_setattr(self, args)
|
1992-01-27 12:53:09 -04:00
|
|
|
object *self;
|
1992-08-14 12:14:30 -03:00
|
|
|
object *args;
|
1992-01-27 12:53:09 -04:00
|
|
|
{
|
1992-08-14 12:14:30 -03:00
|
|
|
object *v;
|
1993-03-29 06:43:31 -04:00
|
|
|
object *name;
|
1992-08-14 12:14:30 -03:00
|
|
|
object *value;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "OSO:setattr", &v, &name, &value))
|
1992-01-27 12:53:09 -04:00
|
|
|
return NULL;
|
1993-03-29 06:43:31 -04:00
|
|
|
if (setattro(v, name, value) != 0)
|
1992-01-27 12:53:09 -04:00
|
|
|
return NULL;
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:53:40 -03:00
|
|
|
static object *
|
|
|
|
builtin_delattr(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v;
|
|
|
|
object *name;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "OS:delattr", &v, &name))
|
1994-08-29 09:53:40 -03:00
|
|
|
return NULL;
|
|
|
|
if (setattro(v, name, (object *)NULL) != 0)
|
|
|
|
return NULL;
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static object *
|
|
|
|
builtin_hash(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v;
|
|
|
|
long x;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:hash", &v))
|
1993-03-29 06:43:31 -04:00
|
|
|
return NULL;
|
|
|
|
x = hashobject(v);
|
|
|
|
if (x == -1)
|
|
|
|
return NULL;
|
|
|
|
return newintobject(x);
|
|
|
|
}
|
|
|
|
|
1991-10-24 11:54:44 -03:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_hex(self, args)
|
1991-10-24 11:54:44 -03:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1991-10-24 11:54:44 -03:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
number_methods *nb;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:hex", &v))
|
|
|
|
return NULL;
|
1992-09-12 08:09:23 -03:00
|
|
|
|
1995-01-02 15:04:15 -04:00
|
|
|
if ((nb = v->ob_type->tp_as_number) == NULL ||
|
1992-09-12 08:09:23 -03:00
|
|
|
nb->nb_hex == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"hex() argument can't be converted to hex");
|
|
|
|
return NULL;
|
1991-10-24 11:54:44 -03:00
|
|
|
}
|
1992-09-12 08:09:23 -03:00
|
|
|
return (*nb->nb_hex)(v);
|
1991-10-24 11:54:44 -03:00
|
|
|
}
|
|
|
|
|
1992-09-25 18:59:05 -03:00
|
|
|
static object *builtin_raw_input PROTO((object *, object *));
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_input(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *line;
|
|
|
|
char *str;
|
|
|
|
object *res;
|
1995-01-09 13:53:26 -04:00
|
|
|
object *globals, *locals;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
line = builtin_raw_input(self, args);
|
1992-09-25 18:59:05 -03:00
|
|
|
if (line == NULL)
|
|
|
|
return line;
|
1995-01-02 15:04:15 -04:00
|
|
|
if (!getargs(line, "s;embedded '\\0' in input line", &str))
|
|
|
|
return NULL;
|
|
|
|
while (*str == ' ' || *str == '\t')
|
|
|
|
str++;
|
1995-01-09 13:53:26 -04:00
|
|
|
globals = getglobals();
|
|
|
|
locals = getlocals();
|
|
|
|
if (dictlookup(globals, "__builtins__") == NULL) {
|
|
|
|
if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res = run_string(str, eval_input, globals, locals);
|
1992-09-25 18:59:05 -03:00
|
|
|
DECREF(line);
|
1995-01-02 15:04:15 -04:00
|
|
|
return res;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_int(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
number_methods *nb;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:int", &v))
|
|
|
|
return NULL;
|
|
|
|
if ((nb = v->ob_type->tp_as_number) == NULL ||
|
1992-09-12 08:09:23 -03:00
|
|
|
nb->nb_int == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"int() argument can't be converted to int");
|
|
|
|
return NULL;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1992-09-12 08:09:23 -03:00
|
|
|
return (*nb->nb_int)(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_len(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
1990-12-20 11:06:42 -04:00
|
|
|
long len;
|
|
|
|
typeobject *tp;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:len", &v))
|
1990-12-20 11:06:42 -04:00
|
|
|
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 12:35:34 -03:00
|
|
|
if (len < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return newintobject(len);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1996-04-08 23:41:06 -03:00
|
|
|
static object *
|
|
|
|
builtin_list(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *v;
|
|
|
|
sequence_methods *sqf;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "O:list", &v))
|
|
|
|
return NULL;
|
|
|
|
if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
|
|
|
|
int n = (*sqf->sq_length)(v);
|
|
|
|
int i;
|
|
|
|
object *l;
|
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
|
|
|
l = newlistobject(n);
|
|
|
|
if (l == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
object *item = (*sqf->sq_item)(v, i);
|
|
|
|
if (item == NULL) {
|
|
|
|
DECREF(l);
|
|
|
|
l = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
setlistitem(l, i, item);
|
|
|
|
}
|
|
|
|
/* XXX Should support indefinite-length sequences */
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
err_setstr(TypeError, "list() argument must be a sequence");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-07-30 13:49:37 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
builtin_slice(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyObject *start, *stop, *step;
|
|
|
|
|
|
|
|
start = stop = step = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*This swapping of stop and start is to maintain compatibility with
|
|
|
|
the range builtin.*/
|
|
|
|
if (stop == NULL) {
|
|
|
|
stop = start;
|
|
|
|
start = NULL;
|
|
|
|
}
|
|
|
|
return PySlice_New(start, stop, step);
|
|
|
|
}
|
|
|
|
|
1995-07-07 19:43:42 -03:00
|
|
|
static object *
|
|
|
|
builtin_locals(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *d;
|
|
|
|
|
|
|
|
if (!newgetargs(args, ""))
|
|
|
|
return NULL;
|
|
|
|
d = getlocals();
|
|
|
|
INCREF(d);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1991-05-05 17:00:36 -03:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_long(self, args)
|
1991-05-05 17:00:36 -03:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1991-05-05 17:00:36 -03:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
number_methods *nb;
|
|
|
|
|
1995-01-02 15:04:15 -04:00
|
|
|
if (!newgetargs(args, "O:long", &v))
|
|
|
|
return NULL;
|
|
|
|
if ((nb = v->ob_type->tp_as_number) == NULL ||
|
1992-09-12 08:09:23 -03:00
|
|
|
nb->nb_long == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"long() argument can't be converted to long");
|
|
|
|
return NULL;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1992-09-12 08:09:23 -03:00
|
|
|
return (*nb->nb_long)(v);
|
1991-05-05 17:00:36 -03:00
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
min_max(args, sign)
|
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
int sign;
|
|
|
|
{
|
1994-08-29 09:52:16 -03:00
|
|
|
int i;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v, *w, *x;
|
1990-12-20 11:06:42 -04:00
|
|
|
sequence_methods *sq;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (gettuplesize(args) > 1)
|
|
|
|
v = args;
|
|
|
|
else if (!newgetargs(args, "O:min/max", &v))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
sq = v->ob_type->tp_as_sequence;
|
|
|
|
if (sq == NULL) {
|
|
|
|
err_setstr(TypeError, "min() or max() of non-sequence");
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-29 09:52:16 -03:00
|
|
|
w = NULL;
|
|
|
|
for (i = 0; ; i++) {
|
1990-12-20 11:06:42 -04:00
|
|
|
x = (*sq->sq_item)(v, i); /* Implies INCREF */
|
1994-08-29 09:52:16 -03:00
|
|
|
if (x == NULL) {
|
|
|
|
if (err_occurred() == IndexError) {
|
|
|
|
err_clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
XDECREF(w);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (w == NULL)
|
1990-12-20 11:06:42 -04:00
|
|
|
w = x;
|
1994-08-29 09:52:16 -03:00
|
|
|
else {
|
|
|
|
if (cmpobject(x, w) * sign > 0) {
|
|
|
|
DECREF(w);
|
|
|
|
w = x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
DECREF(x);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
}
|
1994-08-29 09:52:16 -03:00
|
|
|
if (w == NULL)
|
|
|
|
err_setstr(ValueError, "min() or max() of empty sequence");
|
1990-12-20 11:06:42 -04:00
|
|
|
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 11:54:44 -03:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_oct(self, args)
|
1991-10-24 11:54:44 -03:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1991-10-24 11:54:44 -03:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
number_methods *nb;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:oct", &v))
|
|
|
|
return NULL;
|
1992-09-12 08:09:23 -03:00
|
|
|
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 11:54:44 -03:00
|
|
|
}
|
1992-09-12 08:09:23 -03:00
|
|
|
return (*nb->nb_oct)(v);
|
1991-10-24 11:54:44 -03:00
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_open(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1992-08-14 12:14:30 -03:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1994-08-29 09:52:16 -03:00
|
|
|
char *name;
|
|
|
|
char *mode = "r";
|
|
|
|
int bufsize = -1;
|
|
|
|
object *f;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "s|si:open", &name, &mode, &bufsize))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
1994-08-29 09:52:16 -03:00
|
|
|
f = newfileobject(name, mode);
|
|
|
|
if (f != NULL)
|
|
|
|
setfilebufsize(f, bufsize);
|
|
|
|
return f;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_ord(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1992-08-14 12:14:30 -03:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
char c;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "c:ord", &c))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
1995-01-02 15:04:15 -04:00
|
|
|
return newintobject((long)(c & 0xff));
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1995-01-07 08:39:01 -04:00
|
|
|
static object *
|
|
|
|
do_pow(v, w)
|
|
|
|
object *v, *w;
|
|
|
|
{
|
|
|
|
object *res;
|
|
|
|
if (is_instanceobject(v) || is_instanceobject(w))
|
|
|
|
return instancebinop(v, w, "__pow__", "__rpow__", do_pow);
|
|
|
|
if (v->ob_type->tp_as_number == NULL ||
|
|
|
|
w->ob_type->tp_as_number == NULL) {
|
|
|
|
err_setstr(TypeError, "pow() requires numeric arguments");
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-01-11 21:09:56 -04:00
|
|
|
if (
|
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
!is_complexobject(v) &&
|
|
|
|
#endif
|
|
|
|
is_floatobject(w) && getfloatvalue(v) < 0.0) {
|
1995-02-18 10:51:32 -04:00
|
|
|
if (!err_occurred())
|
|
|
|
err_setstr(ValueError, "negative number to float power");
|
1995-02-10 12:57:16 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-07 08:39:01 -04:00
|
|
|
if (coerce(&v, &w) != 0)
|
|
|
|
return NULL;
|
|
|
|
res = (*v->ob_type->tp_as_number->nb_power)(v, w, None);
|
|
|
|
DECREF(v);
|
|
|
|
DECREF(w);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1991-05-05 17:00:36 -03:00
|
|
|
static object *
|
1991-07-01 15:42:41 -03:00
|
|
|
builtin_pow(self, args)
|
1991-05-05 17:00:36 -03:00
|
|
|
object *self;
|
1991-07-01 15:42:41 -03:00
|
|
|
object *args;
|
1991-05-05 17:00:36 -03:00
|
|
|
{
|
1995-01-07 08:39:01 -04:00
|
|
|
object *v, *w, *z = None, *res;
|
|
|
|
object *v1, *z1, *w2, *z2;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "OO|O:pow", &v, &w, &z))
|
1994-09-29 06:45:57 -03:00
|
|
|
return NULL;
|
1995-01-07 08:39:01 -04:00
|
|
|
if (z == None)
|
|
|
|
return do_pow(v, w);
|
|
|
|
/* XXX The ternary version doesn't do class instance coercions */
|
|
|
|
if (is_instanceobject(v))
|
|
|
|
return v->ob_type->tp_as_number->nb_power(v, w, z);
|
1991-07-01 15:42:41 -03:00
|
|
|
if (v->ob_type->tp_as_number == NULL ||
|
1995-01-07 08:39:01 -04:00
|
|
|
z->ob_type->tp_as_number == NULL ||
|
1994-08-29 09:52:37 -03:00
|
|
|
w->ob_type->tp_as_number == NULL) {
|
1991-05-05 17:00:36 -03:00
|
|
|
err_setstr(TypeError, "pow() requires numeric arguments");
|
|
|
|
return NULL;
|
|
|
|
}
|
1991-07-01 15:42:41 -03:00
|
|
|
if (coerce(&v, &w) != 0)
|
|
|
|
return NULL;
|
1995-01-07 08:39:01 -04:00
|
|
|
res = NULL;
|
|
|
|
v1 = v;
|
|
|
|
z1 = z;
|
|
|
|
if (coerce(&v1, &z1) != 0)
|
|
|
|
goto error2;
|
|
|
|
w2 = w;
|
|
|
|
z2 = z1;
|
|
|
|
if (coerce(&w2, &z2) != 0)
|
|
|
|
goto error1;
|
|
|
|
res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
|
|
|
|
DECREF(w2);
|
|
|
|
DECREF(z2);
|
|
|
|
error1:
|
|
|
|
DECREF(v1);
|
|
|
|
DECREF(z1);
|
|
|
|
error2:
|
1991-07-01 15:42:41 -03:00
|
|
|
DECREF(v);
|
|
|
|
DECREF(w);
|
1995-01-07 08:39:01 -04:00
|
|
|
return res;
|
1991-05-05 17:00:36 -03:00
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_range(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
long ilow = 0, ihigh = 0, istep = 1;
|
1990-12-20 11:06:42 -04:00
|
|
|
int i, n;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
|
|
|
|
|
|
|
if (gettuplesize(args) <= 1) {
|
|
|
|
if (!newgetargs(args,
|
1995-01-17 12:30:22 -04:00
|
|
|
"l;range() requires 1-3 int arguments",
|
1995-01-02 15:04:15 -04:00
|
|
|
&ihigh))
|
|
|
|
return NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
else {
|
1995-01-02 15:04:15 -04:00
|
|
|
if (!newgetargs(args,
|
1995-01-17 12:30:22 -04:00
|
|
|
"ll|l;range() requires 1-3 int arguments",
|
1995-01-02 15:04:15 -04:00
|
|
|
&ilow, &ihigh, &istep))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (istep == 0) {
|
1991-12-10 09:52:31 -04:00
|
|
|
err_setstr(ValueError, "zero step for range()");
|
1990-12-20 11:06:42 -04: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;
|
|
|
|
}
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_xrange(self, args)
|
1993-10-26 14:58:25 -03:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
long ilow = 0, ihigh = 0, istep = 1;
|
1995-01-17 12:30:22 -04:00
|
|
|
long n;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1995-01-02 15:04:15 -04:00
|
|
|
if (gettuplesize(args) <= 1) {
|
|
|
|
if (!newgetargs(args,
|
1995-01-17 12:30:22 -04:00
|
|
|
"l;xrange() requires 1-3 int arguments",
|
1995-01-02 15:04:15 -04:00
|
|
|
&ihigh))
|
|
|
|
return NULL;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
else {
|
1995-01-02 15:04:15 -04:00
|
|
|
if (!newgetargs(args,
|
1995-01-17 12:30:22 -04:00
|
|
|
"ll|l;xrange() requires 1-3 int arguments",
|
1995-01-02 15:04:15 -04:00
|
|
|
&ilow, &ihigh, &istep))
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-02 15:04:15 -04:00
|
|
|
if (istep == 0) {
|
1993-10-26 14:58:25 -03:00
|
|
|
err_setstr(ValueError, "zero step for xrange()");
|
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-02 15:04:15 -04:00
|
|
|
/* 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;
|
|
|
|
return newrangeobject(ilow, n, istep, 1);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1995-07-07 19:43:42 -03:00
|
|
|
extern char *my_readline PROTO((char *));
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_raw_input(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v = NULL;
|
|
|
|
object *f;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "|O:[raw_]input", &v))
|
1992-09-25 18:59:05 -03:00
|
|
|
return NULL;
|
1995-07-07 19:43:42 -03:00
|
|
|
if (getfilefile(sysget("stdin")) == stdin &&
|
1995-07-26 13:26:31 -03:00
|
|
|
getfilefile(sysget("stdout")) == stdout &&
|
|
|
|
isatty(fileno(stdin)) && isatty(fileno(stdout))) {
|
1995-07-07 19:43:42 -03:00
|
|
|
object *po;
|
|
|
|
char *prompt;
|
|
|
|
char *s;
|
|
|
|
object *result;
|
|
|
|
if (v != NULL) {
|
|
|
|
po = strobject(v);
|
|
|
|
if (po == NULL)
|
|
|
|
return NULL;
|
|
|
|
prompt = getstringvalue(po);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
po = NULL;
|
|
|
|
prompt = "";
|
|
|
|
}
|
|
|
|
s = my_readline(prompt);
|
|
|
|
XDECREF(po);
|
|
|
|
if (s == NULL) {
|
|
|
|
err_set(KeyboardInterrupt);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (*s == '\0') {
|
|
|
|
err_set(EOFError);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
else { /* strip trailing '\n' */
|
|
|
|
result = newsizedstringobject(s, strlen(s)-1);
|
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
return result;
|
|
|
|
}
|
1991-06-07 13:10:43 -03:00
|
|
|
if (v != NULL) {
|
1995-01-02 15:04:15 -04:00
|
|
|
f = sysget("stdout");
|
|
|
|
if (f == NULL) {
|
|
|
|
err_setstr(RuntimeError, "lost sys.stdout");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
flushline();
|
1992-09-25 18:59:05 -03:00
|
|
|
if (writeobject(v, f, PRINT_RAW) != 0)
|
1991-06-07 13:10:43 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-02 15:04:15 -04:00
|
|
|
f = sysget("stdin");
|
|
|
|
if (f == NULL) {
|
|
|
|
err_setstr(RuntimeError, "lost sys.stdin");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return filegetline(f, -1);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
static object *
|
|
|
|
builtin_reduce(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *seq, *func, *result = NULL;
|
1993-10-26 14:58:25 -03:00
|
|
|
sequence_methods *sqf;
|
|
|
|
register int i;
|
|
|
|
|
1995-01-02 15:04:15 -04:00
|
|
|
if (!newgetargs(args, "OO|O:reduce", &func, &seq, &result))
|
|
|
|
return NULL;
|
|
|
|
if (result != NULL)
|
1994-08-29 09:52:16 -03:00
|
|
|
INCREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"2nd argument to reduce() must be a sequence object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((args = newtupleobject(2)) == NULL)
|
1994-08-29 09:52:16 -03:00
|
|
|
goto Fail;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
for (i = 0; ; ++i) {
|
1993-10-26 14:58:25 -03:00
|
|
|
object *op2;
|
|
|
|
|
|
|
|
if (args->ob_refcnt > 1) {
|
|
|
|
DECREF(args);
|
|
|
|
if ((args = newtupleobject(2)) == NULL)
|
1994-08-29 09:52:16 -03:00
|
|
|
goto Fail;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
|
|
|
|
if (err_occurred() == IndexError) {
|
|
|
|
err_clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto Fail;
|
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
if (result == NULL)
|
|
|
|
result = op2;
|
|
|
|
else {
|
|
|
|
settupleitem(args, 0, result);
|
|
|
|
settupleitem(args, 1, op2);
|
|
|
|
if ((result = call_object(func, args)) == NULL)
|
|
|
|
goto Fail;
|
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
DECREF(args);
|
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
if (result == NULL)
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"reduce of empty sequence with no initial value");
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
return result;
|
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
Fail:
|
|
|
|
XDECREF(args);
|
|
|
|
XDECREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_reload(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "O:reload", &v))
|
|
|
|
return NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
return reload_module(v);
|
|
|
|
}
|
|
|
|
|
1992-11-26 04:54:07 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_repr(self, args)
|
1992-11-26 04:54:07 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1992-11-26 04:54:07 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "O:repr", &v))
|
1992-11-26 04:54:07 -04:00
|
|
|
return NULL;
|
|
|
|
return reprobject(v);
|
|
|
|
}
|
|
|
|
|
1993-02-12 12:29:05 -04:00
|
|
|
static object *
|
|
|
|
builtin_round(self, args)
|
|
|
|
object *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
double x;
|
|
|
|
double f;
|
|
|
|
int ndigits = 0;
|
|
|
|
int i;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "d|i:round", &x, &ndigits))
|
1993-02-12 12:29:05 -04:00
|
|
|
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 04:54:07 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_str(self, args)
|
1992-11-26 04:54:07 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1992-11-26 04:54:07 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "O:str", &v))
|
1992-11-26 04:54:07 -04:00
|
|
|
return NULL;
|
1993-11-05 06:22:19 -04:00
|
|
|
return strobject(v);
|
1992-11-26 04:54:07 -04:00
|
|
|
}
|
|
|
|
|
1994-08-29 09:53:11 -03:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_tuple(self, args)
|
1994-08-29 09:53:11 -03:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1994-08-29 09:53:11 -03:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
1994-08-29 09:53:11 -03:00
|
|
|
sequence_methods *sqf;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "O:tuple", &v))
|
|
|
|
return NULL;
|
1994-08-29 09:53:11 -03:00
|
|
|
if (is_tupleobject(v)) {
|
|
|
|
INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
1994-08-30 09:38:05 -03:00
|
|
|
if (is_listobject(v))
|
|
|
|
return listtuple(v);
|
1994-08-29 09:53:11 -03:00
|
|
|
if (is_stringobject(v)) {
|
|
|
|
int n = getstringsize(v);
|
|
|
|
object *t = newtupleobject(n);
|
|
|
|
if (t != NULL) {
|
|
|
|
int i;
|
|
|
|
char *p = getstringvalue(v);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
object *item = newsizedstringobject(p+i, 1);
|
|
|
|
if (item == NULL) {
|
|
|
|
DECREF(t);
|
|
|
|
t = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
settupleitem(t, i, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
/* Generic sequence object */
|
|
|
|
if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
|
|
|
|
int n = (*sqf->sq_length)(v);
|
|
|
|
int i;
|
|
|
|
object *t;
|
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
|
|
|
t = newtupleobject(n);
|
|
|
|
if (t == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
object *item = (*sqf->sq_item)(v, i);
|
|
|
|
if (item == NULL) {
|
|
|
|
DECREF(t);
|
|
|
|
t = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
settupleitem(t, i, item);
|
|
|
|
}
|
|
|
|
/* XXX Should support indefinite-length sequences */
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
/* None of the above */
|
|
|
|
err_setstr(TypeError, "tuple() argument must be a sequence");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_type(self, args)
|
1990-12-20 11:06:42 -04:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v;
|
|
|
|
|
|
|
|
if (!newgetargs(args, "O:type", &v))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
v = (object *)v->ob_type;
|
|
|
|
INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
static object *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_vars(self, args)
|
1994-08-29 09:52:16 -03:00
|
|
|
object *self;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *args;
|
1994-08-29 09:52:16 -03:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
object *v = NULL;
|
1994-08-29 09:52:16 -03:00
|
|
|
object *d;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
if (!newgetargs(args, "|O:vars", &v))
|
|
|
|
return NULL;
|
1994-08-29 09:52:16 -03:00
|
|
|
if (v == NULL) {
|
|
|
|
d = getlocals();
|
1995-07-26 13:26:31 -03:00
|
|
|
if (d == NULL) {
|
|
|
|
if (!err_occurred())
|
|
|
|
err_setstr(SystemError, "no locals!?");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
INCREF(d);
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
d = getattr(v, "__dict__");
|
|
|
|
if (d == NULL) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"vars() argument must have __dict__ attribute");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static struct methodlist builtin_methods[] = {
|
1995-01-02 15:04:15 -04:00
|
|
|
{"__import__", builtin___import__, 1},
|
|
|
|
{"abs", builtin_abs, 1},
|
|
|
|
{"apply", builtin_apply, 1},
|
|
|
|
{"callable", builtin_callable, 1},
|
|
|
|
{"chr", builtin_chr, 1},
|
|
|
|
{"cmp", builtin_cmp, 1},
|
|
|
|
{"coerce", builtin_coerce, 1},
|
|
|
|
{"compile", builtin_compile, 1},
|
1996-01-11 21:09:56 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
{"complex", builtin_complex, 1},
|
|
|
|
#endif
|
1995-01-02 15:04:15 -04:00
|
|
|
{"delattr", builtin_delattr, 1},
|
|
|
|
{"dir", builtin_dir, 1},
|
|
|
|
{"divmod", builtin_divmod, 1},
|
|
|
|
{"eval", builtin_eval, 1},
|
|
|
|
{"execfile", builtin_execfile, 1},
|
|
|
|
{"filter", builtin_filter, 1},
|
|
|
|
{"float", builtin_float, 1},
|
|
|
|
{"getattr", builtin_getattr, 1},
|
1995-07-07 19:43:42 -03:00
|
|
|
{"globals", builtin_globals, 1},
|
1995-01-02 15:04:15 -04:00
|
|
|
{"hasattr", builtin_hasattr, 1},
|
|
|
|
{"hash", builtin_hash, 1},
|
|
|
|
{"hex", builtin_hex, 1},
|
|
|
|
{"id", builtin_id, 1},
|
|
|
|
{"input", builtin_input, 1},
|
|
|
|
{"int", builtin_int, 1},
|
|
|
|
{"len", builtin_len, 1},
|
1996-04-08 23:41:06 -03:00
|
|
|
{"list", builtin_list, 1},
|
1995-07-07 19:43:42 -03:00
|
|
|
{"locals", builtin_locals, 1},
|
1995-01-02 15:04:15 -04:00
|
|
|
{"long", builtin_long, 1},
|
|
|
|
{"map", builtin_map, 1},
|
|
|
|
{"max", builtin_max, 1},
|
|
|
|
{"min", builtin_min, 1},
|
|
|
|
{"oct", builtin_oct, 1},
|
|
|
|
{"open", builtin_open, 1},
|
|
|
|
{"ord", builtin_ord, 1},
|
1994-11-10 18:33:19 -04:00
|
|
|
{"pow", builtin_pow, 1},
|
1995-01-02 15:04:15 -04:00
|
|
|
{"range", builtin_range, 1},
|
|
|
|
{"raw_input", builtin_raw_input, 1},
|
|
|
|
{"reduce", builtin_reduce, 1},
|
|
|
|
{"reload", builtin_reload, 1},
|
|
|
|
{"repr", builtin_repr, 1},
|
|
|
|
{"round", builtin_round, 1},
|
|
|
|
{"setattr", builtin_setattr, 1},
|
1996-07-30 13:49:37 -03:00
|
|
|
{"slice", builtin_slice, 1},
|
1995-01-02 15:04:15 -04:00
|
|
|
{"str", builtin_str, 1},
|
|
|
|
{"tuple", builtin_tuple, 1},
|
|
|
|
{"type", builtin_type, 1},
|
|
|
|
{"vars", builtin_vars, 1},
|
|
|
|
{"xrange", builtin_xrange, 1},
|
1991-12-16 09:03:00 -04:00
|
|
|
{NULL, NULL},
|
1990-12-20 11:06:42 -04:00
|
|
|
};
|
|
|
|
|
1995-01-17 12:30:22 -04:00
|
|
|
static object *builtin_mod;
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *builtin_dict;
|
|
|
|
|
1995-01-17 12:30:22 -04:00
|
|
|
object *
|
|
|
|
getbuiltinmod()
|
|
|
|
{
|
|
|
|
return builtin_mod;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
object *
|
1995-01-09 13:53:26 -04:00
|
|
|
getbuiltindict()
|
1994-08-29 09:52:16 -03:00
|
|
|
{
|
1995-01-09 13:53:26 -04:00
|
|
|
return builtin_dict;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Predefined exceptions */
|
|
|
|
|
1993-05-19 11:50:45 -03:00
|
|
|
object *AccessError;
|
1991-12-10 09:52:31 -04:00
|
|
|
object *AttributeError;
|
1993-05-19 11:50:45 -03:00
|
|
|
object *ConflictError;
|
1991-12-16 11:42:38 -04:00
|
|
|
object *EOFError;
|
1991-12-10 09:52:31 -04:00
|
|
|
object *IOError;
|
1991-12-24 09:24:53 -04:00
|
|
|
object *ImportError;
|
1991-12-10 09:52:31 -04:00
|
|
|
object *IndexError;
|
|
|
|
object *KeyError;
|
1991-12-16 11:42:38 -04:00
|
|
|
object *KeyboardInterrupt;
|
|
|
|
object *MemoryError;
|
|
|
|
object *NameError;
|
1991-12-10 09:52:31 -04:00
|
|
|
object *OverflowError;
|
1991-12-16 11:42:38 -04:00
|
|
|
object *RuntimeError;
|
1991-12-16 09:03:00 -04:00
|
|
|
object *SyntaxError;
|
1991-12-16 11:42:38 -04:00
|
|
|
object *SystemError;
|
1991-12-31 09:13:47 -04:00
|
|
|
object *SystemExit;
|
1991-12-16 11:42:38 -04:00
|
|
|
object *TypeError;
|
|
|
|
object *ValueError;
|
|
|
|
object *ZeroDivisionError;
|
1991-12-10 09:52:31 -04:00
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
static object *
|
1991-12-16 11:42:38 -04:00
|
|
|
newstdexception(name)
|
|
|
|
char *name;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1991-12-16 11:42:38 -04:00
|
|
|
object *v = newstringobject(name);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
|
|
|
|
fatal("no mem for new standard exception");
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initerrors()
|
|
|
|
{
|
1993-05-19 11:50:45 -03:00
|
|
|
AccessError = newstdexception("AccessError");
|
1991-12-16 11:42:38 -04:00
|
|
|
AttributeError = newstdexception("AttributeError");
|
1993-05-19 11:50:45 -03:00
|
|
|
ConflictError = newstdexception("ConflictError");
|
1991-12-24 09:24:53 -04:00
|
|
|
EOFError = newstdexception("EOFError");
|
1991-12-16 11:42:38 -04:00
|
|
|
IOError = newstdexception("IOError");
|
1991-12-24 09:24:53 -04:00
|
|
|
ImportError = newstdexception("ImportError");
|
1991-12-16 11:42:38 -04:00
|
|
|
IndexError = newstdexception("IndexError");
|
|
|
|
KeyError = newstdexception("KeyError");
|
1991-12-24 09:24:53 -04:00
|
|
|
KeyboardInterrupt = newstdexception("KeyboardInterrupt");
|
|
|
|
MemoryError = newstdexception("MemoryError");
|
|
|
|
NameError = newstdexception("NameError");
|
1991-12-16 11:42:38 -04:00
|
|
|
OverflowError = newstdexception("OverflowError");
|
1991-12-24 09:24:53 -04:00
|
|
|
RuntimeError = newstdexception("RuntimeError");
|
1991-12-16 11:42:38 -04:00
|
|
|
SyntaxError = newstdexception("SyntaxError");
|
1991-12-24 09:24:53 -04:00
|
|
|
SystemError = newstdexception("SystemError");
|
1991-12-31 09:13:47 -04:00
|
|
|
SystemExit = newstdexception("SystemExit");
|
1991-12-24 09:24:53 -04:00
|
|
|
TypeError = newstdexception("TypeError");
|
|
|
|
ValueError = newstdexception("ValueError");
|
|
|
|
ZeroDivisionError = newstdexception("ZeroDivisionError");
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
initbuiltin()
|
|
|
|
{
|
1995-01-17 12:30:22 -04:00
|
|
|
builtin_mod = initmodule("__builtin__", builtin_methods);
|
|
|
|
builtin_dict = getmoduledict(builtin_mod);
|
1990-12-20 11:06:42 -04:00
|
|
|
INCREF(builtin_dict);
|
|
|
|
initerrors();
|
|
|
|
(void) dictinsert(builtin_dict, "None", None);
|
1996-10-11 13:25:41 -03:00
|
|
|
(void) dictinsert(builtin_dict, "Ellipsis", Py_Ellipsis);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1991-07-01 15:42:41 -03:00
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1993-11-03 11:01:26 -04:00
|
|
|
/* Helper for filter(): filter a tuple through a function */
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
static object *
|
|
|
|
filtertuple(func, tuple)
|
|
|
|
object *func;
|
|
|
|
object *tuple;
|
|
|
|
{
|
1993-10-27 11:56:44 -03:00
|
|
|
object *result;
|
1993-10-26 14:58:25 -03:00
|
|
|
register int i, j;
|
1993-11-01 12:21:44 -04:00
|
|
|
int len = gettuplesize(tuple);
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1995-08-04 01:07:45 -03:00
|
|
|
if (len == 0) {
|
|
|
|
INCREF(tuple);
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
1993-11-01 12:21:44 -04:00
|
|
|
if ((result = newtupleobject(len)) == NULL)
|
|
|
|
return NULL;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
for (i = j = 0; i < len; ++i) {
|
1993-10-27 11:56:44 -03:00
|
|
|
object *item, *good;
|
|
|
|
int ok;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1993-10-27 11:56:44 -03:00
|
|
|
if ((item = gettupleitem(tuple, i)) == NULL)
|
|
|
|
goto Fail_1;
|
|
|
|
if (func == None) {
|
|
|
|
INCREF(item);
|
|
|
|
good = item;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
else {
|
1993-10-27 11:56:44 -03:00
|
|
|
object *arg = mkvalue("(O)", item);
|
|
|
|
if (arg == NULL)
|
|
|
|
goto Fail_1;
|
|
|
|
good = call_object(func, arg);
|
|
|
|
DECREF(arg);
|
|
|
|
if (good == NULL)
|
|
|
|
goto Fail_1;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
1993-10-27 11:56:44 -03:00
|
|
|
ok = testbool(good);
|
|
|
|
DECREF(good);
|
|
|
|
if (ok) {
|
|
|
|
INCREF(item);
|
|
|
|
if (settupleitem(result, j++, item) < 0)
|
|
|
|
goto Fail_1;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-11-01 09:46:50 -04:00
|
|
|
if (resizetuple(&result, j, 0) < 0)
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail_1:
|
1993-11-01 12:21:44 -04:00
|
|
|
DECREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-11-03 11:01:26 -04:00
|
|
|
/* Helper for filter(): filter a string through a function */
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
static object *
|
|
|
|
filterstring(func, strobj)
|
|
|
|
object *func;
|
|
|
|
object *strobj;
|
|
|
|
{
|
1993-10-27 11:56:44 -03:00
|
|
|
object *result;
|
1993-10-26 14:58:25 -03:00
|
|
|
register int i, j;
|
1993-11-01 12:21:44 -04:00
|
|
|
int len = getstringsize(strobj);
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1993-11-01 12:21:44 -04:00
|
|
|
if (func == None) {
|
|
|
|
/* No character is ever false -- share input string */
|
1994-08-29 09:52:16 -03:00
|
|
|
INCREF(strobj);
|
|
|
|
return strobj;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
1993-11-01 12:21:44 -04:00
|
|
|
if ((result = newsizedstringobject(NULL, len)) == NULL)
|
|
|
|
return NULL;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
for (i = j = 0; i < len; ++i) {
|
1993-10-27 11:56:44 -03:00
|
|
|
object *item, *arg, *good;
|
|
|
|
int ok;
|
|
|
|
|
|
|
|
item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
|
|
|
|
if (item == NULL)
|
|
|
|
goto Fail_1;
|
|
|
|
arg = mkvalue("(O)", item);
|
|
|
|
DECREF(item);
|
|
|
|
if (arg == NULL)
|
|
|
|
goto Fail_1;
|
|
|
|
good = call_object(func, arg);
|
|
|
|
DECREF(arg);
|
|
|
|
if (good == NULL)
|
|
|
|
goto Fail_1;
|
|
|
|
ok = testbool(good);
|
|
|
|
DECREF(good);
|
|
|
|
if (ok)
|
1993-10-26 14:58:25 -03:00
|
|
|
GETSTRINGVALUE((stringobject *)result)[j++] =
|
1993-10-27 11:56:44 -03:00
|
|
|
GETSTRINGVALUE((stringobject *)item)[0];
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1996-08-16 17:44:34 -03:00
|
|
|
if (j < len && resizestring(&result, j) < 0)
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail_1:
|
1993-11-01 12:21:44 -04:00
|
|
|
DECREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|