cpython/Python/bltinmodule.c

2629 lines
58 KiB
C
Raw Normal View History

1991-02-19 20:39:46 +08:00
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
1991-02-19 20:39:46 +08:00
All Rights Reserved
1996-10-25 22:44:06 +08:00
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
1991-02-19 20:39:46 +08:00
provided that the above copyright notice appear in all copies and that
1996-10-25 22:44:06 +08:00
both that copyright notice and this permission notice appear in
1991-02-19 20:39:46 +08:00
supporting documentation, and that the names of Stichting Mathematisch
1996-10-25 22:44:06 +08:00
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI 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.
1991-02-19 20:39:46 +08:00
******************************************************************/
1990-12-20 23:06:42 +08:00
/* Built-in functions */
#include "Python.h"
1990-12-20 23:06:42 +08:00
#include "node.h"
#include "compile.h"
#include "eval.h"
1990-12-20 23:06:42 +08:00
#include "mymath.h"
1997-04-12 04:37:35 +08:00
#include <ctype.h>
1996-12-10 23:37:36 +08:00
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/* Forward */
static PyObject *filterstring Py_PROTO((PyObject *, PyObject *));
static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *));
static PyObject *
builtin___import__(self, args)
PyObject *self;
PyObject *args;
{
char *name;
PyObject *globals = NULL;
PyObject *locals = NULL;
PyObject *fromlist = NULL;
if (!PyArg_ParseTuple(args, "s|OOO:__import__",
&name, &globals, &locals, &fromlist))
return NULL;
First part of package support. This doesn't yet support "import a.b.c" or "from a.b.c import x", but it does recognize directories. When importing a directory, it initializes __path__ to a list containing the directory name, and loads the __init__ module if found. The (internal) find_module() and load_module() functions are restructured so that they both also handle built-in and frozen modules and Mac resources (and directories of course). The imp module's find_module() and (new) load_module() also have this functionality. Moreover, imp unconditionally defines constants for all module types, and has two more new functions: find_module_in_package() and find_module_in_directory(). There's also a new API function, PyImport_ImportModuleEx(), which takes all four __import__ arguments (name, globals, locals, fromlist). The last three may be NULL. This is currently the same as PyImport_ImportModule() but in the future it will be able to do relative dotted-path imports. Other changes: - bltinmodule.c: in __import__, call PyImport_ImportModuleEx(). - ceval.c: always pass the fromlist to __import__, even if it is a C function, so PyImport_ImportModuleEx() is useful. - getmtime.c: the function has a second argument, the FILE*, on which it applies fstat(). According to Sjoerd this is much faster. The first (pathname) argument is ignored, but remains for backward compatibility (so the Mac version still works without changes). By cleverly combining the new imp functionality, the full support for dotted names in Python (mini.py, not checked in) is now about 7K, lavishly commented (vs. 14K for ni plus 11K for ihooks, also lavishly commented). Good night!
1997-09-05 15:33:22 +08:00
return PyImport_ImportModuleEx(name, globals, locals, fromlist);
}
1998-06-27 05:23:49 +08:00
static char import_doc[] =
"__import__(name, globals, locals, fromlist) -> module\n\
\n\
Import a module. The globals are only used to determine the context;\n\
they are not modified. The locals are currently unused. The fromlist\n\
should be a list of names to emulate ``from name import ...'', or an\n\
empty list to emulate ``import name''.\n\
When importing a module from a package, note that __import__('A.B', ...)\n\
returns package A when fromlist is empty, but its submodule B when\n\
fromlist is not empty.";
static PyObject *
builtin_abs(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:abs", &v))
return NULL;
return PyNumber_Absolute(v);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char abs_doc[] =
"abs(number) -> number\n\
\n\
Return the absolute value of the argument.";
static PyObject *
builtin_apply(self, args)
PyObject *self;
PyObject *args;
{
PyObject *func, *alist = NULL, *kwdict = NULL;
PyObject *t = NULL, *retval = NULL;
if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
return NULL;
if (alist != NULL) {
if (!PyTuple_Check(alist)) {
if (!PySequence_Check(alist)) {
PyErr_SetString(PyExc_TypeError,
"apply() 2nd argument must be a sequence");
return NULL;
}
t = PySequence_Tuple(alist);
if (t == NULL)
return NULL;
alist = t;
}
}
if (kwdict != NULL && !PyDict_Check(kwdict)) {
PyErr_SetString(PyExc_TypeError,
1995-07-18 22:51:37 +08:00
"apply() 3rd argument must be dictionary");
goto finally;
1995-07-18 22:51:37 +08:00
}
retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
finally:
Py_XDECREF(t);
return retval;
}
1998-06-27 05:23:49 +08:00
static char apply_doc[] =
"apply(function, args[, kwargs]) -> value\n\
\n\
Call a function with positional arguments taken from the tuple args,\n\
and keyword arguments taken from the optional dictionary kwargs.";
static PyObject *
builtin_buffer(self, args)
PyObject *self;
PyObject *args;
{
PyObject *ob;
int offset = 0;
int size = Py_END_OF_BUFFER;
if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
return NULL;
return PyBuffer_FromObject(ob, offset, size);
}
static char buffer_doc[] =
"buffer(object [, offset[, size]) -> object\n\
\n\
Creates a new buffer object which references the given object.\n\
The buffer will reference a slice of the target object from the\n\
start of the object (or at the specified offset). The slice will\n\
extend to the end of the target object (or with the specified size).";
static PyObject *
builtin_callable(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:callable", &v))
return NULL;
return PyInt_FromLong((long)PyCallable_Check(v));
}
1998-06-27 05:23:49 +08:00
static char callable_doc[] =
"callable(object) -> Boolean\n\
\n\
Return whether the object is callable (i.e., some kind of function).\n\
Note that classes are callable, as are instances with a __call__() method.";
static PyObject *
builtin_filter(self, args)
PyObject *self;
PyObject *args;
{
PyObject *func, *seq, *result;
PySequenceMethods *sqf;
int len;
register int i, j;
if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
return NULL;
if (PyString_Check(seq)) {
PyObject *r = filterstring(func, seq);
return r;
}
if (PyTuple_Check(seq)) {
PyObject *r = filtertuple(func, seq);
return r;
}
sqf = seq->ob_type->tp_as_sequence;
if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
PyErr_SetString(PyExc_TypeError,
"argument 2 to filter() must be a sequence type");
goto Fail_2;
}
if ((len = (*sqf->sq_length)(seq)) < 0)
goto Fail_2;
if (PyList_Check(seq) && seq->ob_refcnt == 1) {
Py_INCREF(seq);
result = seq;
}
else {
if ((result = PyList_New(len)) == NULL)
goto Fail_2;
}
for (i = j = 0; ; ++i) {
PyObject *item, *good;
int ok;
if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
if (PyErr_ExceptionMatches(PyExc_IndexError)) {
PyErr_Clear();
break;
}
goto Fail_1;
}
if (func == Py_None) {
good = item;
Py_INCREF(good);
}
else {
PyObject *arg = Py_BuildValue("(O)", item);
if (arg == NULL)
goto Fail_1;
good = PyEval_CallObject(func, arg);
Py_DECREF(arg);
if (good == NULL) {
Py_DECREF(item);
goto Fail_1;
}
}
ok = PyObject_IsTrue(good);
Py_DECREF(good);
if (ok) {
if (j < len) {
if (PyList_SetItem(result, j++, item) < 0)
goto Fail_1;
}
else {
int status = PyList_Append(result, item);
j++;
Py_DECREF(item);
if (status < 0)
goto Fail_1;
}
} else {
Py_DECREF(item);
}
}
if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
goto Fail_1;
return result;
Fail_1:
Py_DECREF(result);
Fail_2:
return NULL;
}
1998-06-27 05:23:49 +08:00
static char filter_doc[] =
"filter(function, sequence) -> list\n\
\n\
Return a list containing those items of sequence for which function(item)\n\
is true. If function is None, return a list of items that are true.";
static PyObject *
builtin_chr(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
long x;
char s[1];
if (!PyArg_ParseTuple(args, "l:chr", &x))
1990-12-20 23:06:42 +08:00
return NULL;
if (x < 0 || x >= 256) {
PyErr_SetString(PyExc_ValueError,
"chr() arg not in range(256)");
1990-12-20 23:06:42 +08:00
return NULL;
}
1997-04-12 04:37:35 +08:00
s[0] = (char)x;
return PyString_FromStringAndSize(s, 1);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char chr_doc[] =
"chr(i) -> character\n\
\n\
Return a string of one character with ordinal i; 0 <= i < 256.";
static PyObject *
builtin_cmp(self, args)
PyObject *self;
PyObject *args;
{
PyObject *a, *b;
int c;
if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
return NULL;
if (PyObject_Cmp(a, b, &c) < 0)
return NULL;
return PyInt_FromLong((long)c);
}
1998-06-27 05:23:49 +08:00
static char cmp_doc[] =
"cmp(x, y) -> integer\n\
\n\
Return negative if x<y, zero if x==y, positive if x>y.";
static PyObject *
builtin_coerce(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v, *w;
PyObject *res;
if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
return NULL;
if (PyNumber_Coerce(&v, &w) < 0)
return NULL;
res = Py_BuildValue("(OO)", v, w);
Py_DECREF(v);
Py_DECREF(w);
return res;
}
1998-06-27 05:23:49 +08:00
static char coerce_doc[] =
"coerce(x, y) -> None or (x1, y1)\n\
\n\
When x and y can be coerced to values of the same type, return a tuple\n\
containing the coerced values. When they can't be coerced, return None.";
static PyObject *
builtin_compile(self, args)
PyObject *self;
PyObject *args;
{
char *str;
char *filename;
char *startstr;
int start;
if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
return NULL;
if (strcmp(startstr, "exec") == 0)
start = Py_file_input;
else if (strcmp(startstr, "eval") == 0)
start = Py_eval_input;
else if (strcmp(startstr, "single") == 0)
start = Py_single_input;
else {
PyErr_SetString(PyExc_ValueError,
"compile() mode must be 'exec' or 'eval' or 'single'");
return NULL;
}
return Py_CompileString(str, filename, start);
}
1998-06-27 05:23:49 +08:00
static char compile_doc[] =
"compile(source, filename, mode) -> code object\n\
\n\
Compile the source string (a Python module, statement or expression)\n\
into a code object that can be executed by the exec statement or eval().\n\
The filename will be used for run-time error messages.\n\
The mode must be 'exec' to compile a module, 'single' to compile a\n\
single (interactive) statement, or 'eval' to compile an expression.";
1996-01-12 09:09:56 +08:00
#ifndef WITHOUT_COMPLEX
static PyObject *
complex_from_string(v)
PyObject *v;
{
extern double strtod Py_PROTO((const char *, char **));
char *s, *start, *end;
double x=0.0, y=0.0, z;
int got_re=0, got_im=0, done=0;
int digit_or_dot;
int sw_error=0;
int sign;
char buffer[256]; /* For errors */
start = s = PyString_AS_STRING(v);
/* position on first nonblank */
while (*s && isspace(Py_CHARMASK(*s)))
s++;
if (s[0] == '\0') {
PyErr_SetString(PyExc_ValueError,
"empty string for complex()");
return NULL;
}
z = -1.0;
sign = 1;
do {
switch (*s) {
case '\0':
if (s-start != PyString_GET_SIZE(v)) {
PyErr_SetString(
PyExc_ValueError,
"null byte in argument for complex()");
return NULL;
}
if(!done) sw_error=1;
break;
case '-':
sign = -1;
/* Fallthrough */
case '+':
if (done) sw_error=1;
s++;
if ( *s=='\0'||*s=='+'||*s=='-' ||
isspace(Py_CHARMASK(*s)) ) sw_error=1;
break;
case 'J':
case 'j':
if (got_im || done) {
sw_error = 1;
break;
}
if (z<0.0) {
y=sign;
}
else{
y=sign*z;
}
got_im=1;
s++;
if (*s!='+' && *s!='-' )
done=1;
break;
default:
if (isspace(Py_CHARMASK(*s))) {
while (*s && isspace(Py_CHARMASK(*s)))
s++;
if (s[0] != '\0')
sw_error=1;
else
done = 1;
break;
}
digit_or_dot =
(*s=='.' || isdigit(Py_CHARMASK(*s)));
if (done||!digit_or_dot) {
sw_error=1;
break;
}
errno = 0;
PyFPE_START_PROTECT("strtod", return 0)
z = strtod(s, &end) ;
PyFPE_END_PROTECT(z)
if (errno != 0) {
sprintf(buffer,
"float() out of range: %.150s", s);
PyErr_SetString(
PyExc_ValueError,
buffer);
return NULL;
}
s=end;
if (*s=='J' || *s=='j') {
break;
}
if (got_re) {
sw_error=1;
break;
}
/* accept a real part */
x=sign*z;
got_re=1;
if (got_im) done=1;
z = -1.0;
sign = 1;
break;
} /* end of switch */
} while (*s!='\0' && !sw_error);
if (sw_error) {
PyErr_SetString(PyExc_ValueError,
"malformed string for complex()");
return NULL;
}
return PyComplex_FromDoubles(x,y);
}
static PyObject *
1996-01-12 09:09:56 +08:00
builtin_complex(self, args)
PyObject *self;
PyObject *args;
1996-01-12 09:09:56 +08:00
{
PyObject *r, *i, *tmp;
PyNumberMethods *nbr, *nbi = NULL;
Py_complex cr, ci;
int own_r = 0;
1996-01-12 09:09:56 +08:00
i = NULL;
if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
1996-01-12 09:09:56 +08:00
return NULL;
if (PyString_Check(r))
return complex_from_string(r);
1996-01-12 09:09:56 +08:00
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))) {
PyErr_SetString(PyExc_TypeError,
1996-01-12 09:09:56 +08:00
"complex() argument can't be converted to complex");
return NULL;
}
/* XXX Hack to support classes with __complex__ method */
if (PyInstance_Check(r)) {
static PyObject *complexstr;
PyObject *f;
if (complexstr == NULL) {
1997-01-18 16:04:16 +08:00
complexstr = PyString_InternFromString("__complex__");
if (complexstr == NULL)
return NULL;
}
f = PyObject_GetAttr(r, complexstr);
if (f == NULL)
PyErr_Clear();
else {
PyObject *args = Py_BuildValue("()");
if (args == NULL)
return NULL;
r = PyEval_CallObject(f, args);
Py_DECREF(args);
Py_DECREF(f);
if (r == NULL)
return NULL;
own_r = 1;
}
}
if (PyComplex_Check(r)) {
cr = ((PyComplexObject*)r)->cval;
1998-04-11 06:27:42 +08:00
if (own_r) {
Py_DECREF(r);
1998-04-11 06:27:42 +08:00
}
}
1996-01-12 09:09:56 +08:00
else {
tmp = (*nbr->nb_float)(r);
1998-04-11 06:27:42 +08:00
if (own_r) {
Py_DECREF(r);
1998-04-11 06:27:42 +08:00
}
if (tmp == NULL)
return NULL;
cr.real = PyFloat_AsDouble(tmp);
Py_DECREF(tmp);
cr.imag = 0.0;
1996-01-12 09:09:56 +08:00
}
if (i == NULL) {
ci.real = 0.0;
ci.imag = 0.0;
1996-01-12 09:09:56 +08:00
}
else if (PyComplex_Check(i))
ci = ((PyComplexObject*)i)->cval;
1996-01-12 09:09:56 +08:00
else {
tmp = (*nbi->nb_float)(i);
if (tmp == NULL)
return NULL;
ci.real = PyFloat_AsDouble(tmp);
Py_DECREF(tmp);
1996-01-12 09:09:56 +08:00
ci.imag = 0.;
}
cr.real -= ci.imag;
cr.imag += ci.real;
return PyComplex_FromCComplex(cr);
1996-01-12 09:09:56 +08:00
}
1998-06-27 05:23:49 +08:00
static char complex_doc[] =
"complex(real[, imag]) -> complex number\n\
\n\
Create a complex number from a real part and an optional imaginary part.\n\
This is equivalent to (real + imag*1j) where imag defaults to 0.";
1996-01-12 09:09:56 +08:00
#endif
static PyObject *
builtin_dir(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
static char *attrlist[] = {"__members__", "__methods__", NULL};
PyObject *v = NULL, *l = NULL, *m = NULL;
PyObject *d, *x;
int i;
char **s;
if (!PyArg_ParseTuple(args, "|O:dir", &v))
return NULL;
1990-12-20 23:06:42 +08:00
if (v == NULL) {
x = PyEval_GetLocals();
if (x == NULL)
goto error;
l = PyMapping_Keys(x);
if (l == NULL)
goto error;
1990-12-20 23:06:42 +08:00
}
else {
d = PyObject_GetAttrString(v, "__dict__");
if (d == NULL)
PyErr_Clear();
else {
l = PyMapping_Keys(d);
if (l == NULL)
PyErr_Clear();
Py_DECREF(d);
1990-12-20 23:06:42 +08:00
}
if (l == NULL) {
l = PyList_New(0);
if (l == NULL)
goto error;
}
for (s = attrlist; *s != NULL; s++) {
m = PyObject_GetAttrString(v, *s);
if (m == NULL) {
PyErr_Clear();
continue;
}
for (i = 0; ; i++) {
x = PySequence_GetItem(m, i);
if (x == NULL) {
PyErr_Clear();
break;
}
if (PyList_Append(l, x) != 0) {
Py_DECREF(x);
Py_DECREF(m);
goto error;
}
Py_DECREF(x);
}
Py_DECREF(m);
}
}
if (PyList_Sort(l) != 0)
goto error;
return l;
error:
Py_XDECREF(l);
return NULL;
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char dir_doc[] =
"dir([object]) -> list of strings\n\
\n\
Return an alphabetized list of names comprising (some of) the attributes\n\
of the given object. Without an argument, the names in the current scope\n\
are listed. With an instance argument, only the instance attributes are\n\
returned. With a class argument, attributes of the base class are not\n\
returned. For other types or arguments, this may list members or methods.";
static PyObject *
builtin_divmod(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v, *w;
if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
return NULL;
return PyNumber_Divmod(v, w);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char divmod_doc[] =
"divmod(x, y) -> (div, mod)\n\
\n\
Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
static PyObject *
builtin_eval(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *cmd;
PyObject *globals = Py_None, *locals = Py_None;
char *str;
if (!PyArg_ParseTuple(args, "O|O!O!:eval",
&cmd,
&PyDict_Type, &globals,
&PyDict_Type, &locals))
return NULL;
if (globals == Py_None) {
globals = PyEval_GetGlobals();
if (locals == Py_None)
locals = PyEval_GetLocals();
}
else if (locals == Py_None)
locals = globals;
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
if (PyDict_SetItemString(globals, "__builtins__",
PyEval_GetBuiltins()) != 0)
return NULL;
}
if (PyCode_Check(cmd))
return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
if (!PyString_Check(cmd)) {
PyErr_SetString(PyExc_TypeError,
"eval() argument 1 must be string or code object");
return NULL;
}
str = PyString_AsString(cmd);
if ((int)strlen(str) != PyString_Size(cmd)) {
PyErr_SetString(PyExc_ValueError,
"embedded '\\0' in string arg");
return NULL;
}
while (*str == ' ' || *str == '\t')
str++;
return PyRun_String(str, Py_eval_input, globals, locals);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char eval_doc[] =
"eval(source[, globals[, locals]]) -> value\n\
\n\
Evaluate the source in the context of globals and locals.\n\
The source may be a string representing a Python expression\n\
or a code object as returned by compile().\n\
The globals and locals are dictionaries, defaulting to the current\n\
globals and locals. If only globals is given, locals defaults to it.";
static PyObject *
builtin_execfile(self, args)
PyObject *self;
PyObject *args;
1992-02-26 02:55:05 +08:00
{
char *filename;
PyObject *globals = Py_None, *locals = Py_None;
PyObject *res;
1992-02-26 02:55:05 +08:00
FILE* fp;
if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
&filename,
&PyDict_Type, &globals,
&PyDict_Type, &locals))
return NULL;
if (globals == Py_None) {
globals = PyEval_GetGlobals();
if (locals == Py_None)
locals = PyEval_GetLocals();
}
else if (locals == Py_None)
locals = globals;
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
if (PyDict_SetItemString(globals, "__builtins__",
PyEval_GetBuiltins()) != 0)
return NULL;
}
Py_BEGIN_ALLOW_THREADS
fp = fopen(filename, "r");
Py_END_ALLOW_THREADS
1992-02-26 02:55:05 +08:00
if (fp == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
1992-02-26 02:55:05 +08:00
return NULL;
}
res = PyRun_File(fp, filename, Py_file_input, globals, locals);
Py_BEGIN_ALLOW_THREADS
1992-02-26 02:55:05 +08:00
fclose(fp);
Py_END_ALLOW_THREADS
return res;
1992-02-26 02:55:05 +08:00
}
1998-06-27 05:23:49 +08:00
static char execfile_doc[] =
"execfile(filename[, globals[, locals]])\n\
\n\
Read and execute a Python script from a file.\n\
The globals and locals are dictionaries, defaulting to the current\n\
globals and locals. If only globals is given, locals defaults to it.";
static PyObject *
builtin_float(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:float", &v))
return NULL;
return PyNumber_Float(v);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char float_doc[] =
"float(x) -> floating point number\n\
\n\
Convert a string or number to a floating point number, if possible.";
static PyObject *
builtin_getattr(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v, *result, *dflt = NULL;
PyObject *name;
if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
return NULL;
result = PyObject_GetAttr(v, name);
if (result == NULL && dflt != NULL) {
PyErr_Clear();
Py_INCREF(dflt);
result = dflt;
}
return result;
}
1998-06-27 05:23:49 +08:00
static char getattr_doc[] =
"getattr(object, name[, default]) -> value\n\
1998-06-27 05:23:49 +08:00
\n\
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
When a default argument is given, it is returned when the attribute doesn't\n\
exist; without it, an exception is raised in that case.";
1998-06-27 05:23:49 +08:00
static PyObject *
builtin_globals(self, args)
PyObject *self;
PyObject *args;
{
PyObject *d;
if (!PyArg_ParseTuple(args, ""))
return NULL;
d = PyEval_GetGlobals();
Py_INCREF(d);
return d;
}
1998-06-27 05:23:49 +08:00
static char globals_doc[] =
"globals() -> dictionary\n\
\n\
Return the dictionary containing the current scope's global variables.";
static PyObject *
builtin_hasattr(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
PyObject *name;
if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
return NULL;
v = PyObject_GetAttr(v, name);
if (v == NULL) {
PyErr_Clear();
Py_INCREF(Py_False);
return Py_False;
}
Py_DECREF(v);
Py_INCREF(Py_True);
return Py_True;
}
1998-06-27 05:23:49 +08:00
static char hasattr_doc[] =
"hasattr(object, name) -> Boolean\n\
\n\
Return whether the object has an attribute with the given name.\n\
(This is done by calling getattr(object, name) and catching exceptions.)";
static PyObject *
builtin_id(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:id", &v))
return NULL;
return PyInt_FromLong((long)v);
}
1998-06-27 05:23:49 +08:00
static char id_doc[] =
"id(object) -> integer\n\
\n\
Return the identity of an object. This is guaranteed to be unique among\n\
simultaneously existing objects. (Hint: it's the object's memory address.)";
static PyObject *
builtin_map(self, args)
PyObject *self;
PyObject *args;
{
typedef struct {
PyObject *seq;
PySequenceMethods *sqf;
int len;
} sequence;
PyObject *func, *result;
sequence *seqs = NULL, *sqp;
int n, len;
register int i, j;
n = PyTuple_Size(args);
if (n < 2) {
PyErr_SetString(PyExc_TypeError,
"map() requires at least two args");
return NULL;
}
func = PyTuple_GetItem(args, 0);
n--;
if (func == Py_None && n == 1) {
/* map(None, S) is the same as list(S). */
return PySequence_List(PyTuple_GetItem(args, 1));
}
if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
PyErr_NoMemory();
goto Fail_2;
}
for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
int curlen;
PySequenceMethods *sqf;
if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
goto Fail_2;
sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
if (sqf == NULL ||
sqf->sq_length == NULL ||
sqf->sq_item == NULL)
{
static char errmsg[] =
"argument %d to map() must be a sequence object";
char errbuf[sizeof(errmsg) + 25];
sprintf(errbuf, errmsg, i+2);
PyErr_SetString(PyExc_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 = (PyObject *) PyList_New(len)) == NULL)
goto Fail_2;
for (i = 0; ; ++i) {
PyObject *alist, *item=NULL, *value;
int any = 0;
if (func == Py_None && n == 1)
alist = NULL;
else {
if ((alist = PyTuple_New(n)) == NULL)
goto Fail_1;
}
for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
if (sqp->len < 0) {
Py_INCREF(Py_None);
item = Py_None;
}
else {
item = (*sqp->sqf->sq_item)(sqp->seq, i);
if (item == NULL) {
if (PyErr_ExceptionMatches(
PyExc_IndexError))
{
PyErr_Clear();
Py_INCREF(Py_None);
item = Py_None;
sqp->len = -1;
}
else {
goto Fail_0;
}
}
else
any = 1;
}
if (!alist)
break;
if (PyTuple_SetItem(alist, j, item) < 0) {
Py_DECREF(item);
goto Fail_0;
}
continue;
Fail_0:
Py_XDECREF(alist);
goto Fail_1;
}
if (!alist)
alist = item;
if (!any) {
Py_DECREF(alist);
break;
}
if (func == Py_None)
value = alist;
else {
value = PyEval_CallObject(func, alist);
Py_DECREF(alist);
if (value == NULL)
goto Fail_1;
}
if (i >= len) {
int status = PyList_Append(result, value);
Py_DECREF(value);
if (status < 0)
goto Fail_1;
}
else {
if (PyList_SetItem(result, i, value) < 0)
goto Fail_1;
}
}
if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
goto Fail_1;
PyMem_DEL(seqs);
return result;
Fail_1:
Py_DECREF(result);
Fail_2:
if (seqs) PyMem_DEL(seqs);
return NULL;
}
1998-06-27 05:23:49 +08:00
static char map_doc[] =
"map(function, sequence[, sequence, ...]) -> list\n\
\n\
Return a list of the results of applying the function to the items of\n\
the argument sequence(s). If more than one sequence is given, the\n\
function is called with an argument list consisting of the corresponding\n\
item of each sequence, substituting None for missing values when not all\n\
sequences have the same length. If the function is None, return a list of\n\
the items of the sequence (or a list of tuples if more than one sequence).";
static PyObject *
builtin_setattr(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
PyObject *name;
PyObject *value;
if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
return NULL;
if (PyObject_SetAttr(v, name, value) != 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
1998-06-27 05:23:49 +08:00
static char setattr_doc[] =
"setattr(object, name, value)\n\
\n\
Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
``x.y = v''.";
static PyObject *
1994-08-29 20:53:40 +08:00
builtin_delattr(self, args)
PyObject *self;
PyObject *args;
1994-08-29 20:53:40 +08:00
{
PyObject *v;
PyObject *name;
if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
1994-08-29 20:53:40 +08:00
return NULL;
if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
1994-08-29 20:53:40 +08:00
return NULL;
Py_INCREF(Py_None);
return Py_None;
1994-08-29 20:53:40 +08:00
}
1998-06-27 05:23:49 +08:00
static char delattr_doc[] =
"delattr(object, name)\n\
1998-06-27 05:23:49 +08:00
\n\
Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
``del x.y''.";
static PyObject *
builtin_hash(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
long x;
if (!PyArg_ParseTuple(args, "O:hash", &v))
return NULL;
x = PyObject_Hash(v);
if (x == -1)
return NULL;
return PyInt_FromLong(x);
}
1998-06-27 05:23:49 +08:00
static char hash_doc[] =
"hash(object) -> integer\n\
\n\
Return a hash value for the object. Two objects with the same value have\n\
the same hash value. The reverse is not necessarily true, but likely.";
static PyObject *
builtin_hex(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
PyNumberMethods *nb;
if (!PyArg_ParseTuple(args, "O:hex", &v))
return NULL;
if ((nb = v->ob_type->tp_as_number) == NULL ||
nb->nb_hex == NULL) {
PyErr_SetString(PyExc_TypeError,
"hex() argument can't be converted to hex");
return NULL;
}
return (*nb->nb_hex)(v);
}
1998-06-27 05:23:49 +08:00
static char hex_doc[] =
"hex(number) -> string\n\
\n\
Return the hexadecimal representation of an integer or long integer.";
static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
static PyObject *
builtin_input(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *line;
char *str;
PyObject *res;
PyObject *globals, *locals;
line = builtin_raw_input(self, args);
if (line == NULL)
return line;
if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
return NULL;
while (*str == ' ' || *str == '\t')
str++;
globals = PyEval_GetGlobals();
locals = PyEval_GetLocals();
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
if (PyDict_SetItemString(globals, "__builtins__",
PyEval_GetBuiltins()) != 0)
return NULL;
}
res = PyRun_String(str, Py_eval_input, globals, locals);
Py_DECREF(line);
return res;
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char input_doc[] =
"input([prompt]) -> value\n\
\n\
Equivalent to eval(raw_input(prompt)).";
1997-02-14 23:48:05 +08:00
static PyObject *
builtin_intern(self, args)
PyObject *self;
PyObject *args;
{
PyObject *s;
if (!PyArg_ParseTuple(args, "S", &s))
return NULL;
Py_INCREF(s);
PyString_InternInPlace(&s);
return s;
}
1998-06-27 05:23:49 +08:00
static char intern_doc[] =
"intern(string) -> string\n\
\n\
``Intern'' the given string. This enters the string in the (global)\n\
table of interned strings whose purpose is to speed up dictionary lookups.\n\
Return the string itself or the previously interned string object with the\n\
same value.";
static PyObject *
builtin_int(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:int", &v))
return NULL;
return PyNumber_Int(v);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char int_doc[] =
"int(x) -> integer\n\
\n\
Convert a string or number to an integer, if possible.\n\
A floating point argument will be truncated towards zero.";
static PyObject *
builtin_len(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *v;
long res;
if (!PyArg_ParseTuple(args, "O:len", &v))
1990-12-20 23:06:42 +08:00
return NULL;
res = PyObject_Length(v);
if (res < 0 && PyErr_Occurred())
return NULL;
return PyInt_FromLong(res);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char len_doc[] =
"len(object) -> integer\n\
\n\
Return the number of items of a sequence or mapping.";
static PyObject *
builtin_list(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:list", &v))
return NULL;
return PySequence_List(v);
}
1998-06-27 05:23:49 +08:00
static char list_doc[] =
"list(sequence) -> list\n\
\n\
Return a new list whose items are the same as those of the argument sequence.";
1996-07-31 00:49:37 +08:00
static PyObject *
builtin_slice(self, args)
PyObject *self;
PyObject *args;
{
PyObject *start, *stop, *step;
1996-07-31 00:49:37 +08:00
start = stop = step = NULL;
1996-07-31 00:49:37 +08:00
if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
return NULL;
1996-07-31 00:49:37 +08:00
/* This swapping of stop and start is to maintain similarity with
range(). */
if (stop == NULL) {
stop = start;
start = NULL;
}
return PySlice_New(start, stop, step);
1996-07-31 00:49:37 +08:00
}
1998-06-27 05:23:49 +08:00
static char slice_doc[] =
"slice([start,] step[, stop]) -> slice object\n\
\n\
Create a slice object. This is used for slicing by the Numeric extensions.";
static PyObject *
builtin_locals(self, args)
PyObject *self;
PyObject *args;
{
PyObject *d;
if (!PyArg_ParseTuple(args, ""))
return NULL;
d = PyEval_GetLocals();
Py_INCREF(d);
return d;
}
1998-06-27 05:23:49 +08:00
static char locals_doc[] =
"locals() -> dictionary\n\
\n\
Return the dictionary containing the current scope's local variables.";
static PyObject *
builtin_long(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:long", &v))
return NULL;
return PyNumber_Long(v);
}
1998-06-27 05:23:49 +08:00
static char long_doc[] =
"long(x) -> long integer\n\
\n\
Convert a string or number to a long integer, if possible.\n\
A floating point argument will be truncated towards zero.";
static PyObject *
min_max(args, sign)
PyObject *args;
1990-12-20 23:06:42 +08:00
int sign;
{
int i;
PyObject *v, *w, *x;
PySequenceMethods *sq;
if (PyTuple_Size(args) > 1)
v = args;
else if (!PyArg_ParseTuple(args, "O:min/max", &v))
1990-12-20 23:06:42 +08:00
return NULL;
sq = v->ob_type->tp_as_sequence;
if (sq == NULL || sq->sq_item == NULL) {
PyErr_SetString(PyExc_TypeError,
"min() or max() of non-sequence");
1990-12-20 23:06:42 +08:00
return NULL;
}
w = NULL;
for (i = 0; ; i++) {
1990-12-20 23:06:42 +08:00
x = (*sq->sq_item)(v, i); /* Implies INCREF */
if (x == NULL) {
if (PyErr_ExceptionMatches(PyExc_IndexError)) {
PyErr_Clear();
break;
}
Py_XDECREF(w);
return NULL;
}
if (w == NULL)
1990-12-20 23:06:42 +08:00
w = x;
else {
int c = PyObject_Compare(x, w);
if (c && PyErr_Occurred()) {
Py_DECREF(x);
Py_XDECREF(w);
return NULL;
}
if (c * sign > 0) {
Py_DECREF(w);
w = x;
}
else
Py_DECREF(x);
1990-12-20 23:06:42 +08:00
}
}
if (w == NULL)
PyErr_SetString(PyExc_ValueError,
"min() or max() of empty sequence");
1990-12-20 23:06:42 +08:00
return w;
}
static PyObject *
1990-12-20 23:06:42 +08:00
builtin_min(self, v)
PyObject *self;
PyObject *v;
1990-12-20 23:06:42 +08:00
{
return min_max(v, -1);
}
1998-06-27 05:23:49 +08:00
static char min_doc[] =
"min(sequence) -> value\n\
min(a, b, c, ...) -> value\n\
\n\
With a single sequence argument, return its smallest item.\n\
With two or more arguments, return the smallest argument.";
static PyObject *
1990-12-20 23:06:42 +08:00
builtin_max(self, v)
PyObject *self;
PyObject *v;
1990-12-20 23:06:42 +08:00
{
return min_max(v, 1);
}
1998-06-27 05:23:49 +08:00
static char max_doc[] =
"max(sequence) -> value\n\
max(a, b, c, ...) -> value\n\
\n\
With a single sequence argument, return its largest item.\n\
With two or more arguments, return the largest argument.";
static PyObject *
builtin_oct(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
PyNumberMethods *nb;
if (!PyArg_ParseTuple(args, "O:oct", &v))
return NULL;
if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
nb->nb_oct == NULL) {
PyErr_SetString(PyExc_TypeError,
"oct() argument can't be converted to oct");
return NULL;
}
return (*nb->nb_oct)(v);
}
1998-06-27 05:23:49 +08:00
static char oct_doc[] =
"oct(number) -> string\n\
\n\
Return the octal representation of an integer or long integer.";
static PyObject *
builtin_open(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
char *name;
char *mode = "r";
int bufsize = -1;
PyObject *f;
if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
1990-12-20 23:06:42 +08:00
return NULL;
f = PyFile_FromString(name, mode);
if (f != NULL)
PyFile_SetBufSize(f, bufsize);
return f;
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char open_doc[] =
"open(filename[, mode[, buffering]]) -> file object\n\
\n\
Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
writing or appending. The file will be created if it doesn't exist\n\
when opened for writing or appending; it will be truncated when\n\
opened for writing. Add a 'b' to the mode for binary files.\n\
Add a '+' to the mode to allow simultaneous reading and writing.\n\
If the buffering argument is given, 0 means unbuffered, 1 means line\n\
buffered, and larger numbers specify the buffer size.";
static PyObject *
builtin_ord(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
char c;
if (!PyArg_ParseTuple(args, "c:ord", &c))
1990-12-20 23:06:42 +08:00
return NULL;
return PyInt_FromLong((long)(c & 0xff));
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char ord_doc[] =
"ord(c) -> integer\n\
\n\
Return the integer ordinal of a one character string.";
static PyObject *
builtin_pow(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v, *w, *z = Py_None;
if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
return NULL;
return PyNumber_Power(v, w, z);
}
1998-06-27 05:23:49 +08:00
static char pow_doc[] =
"pow(x, y[, z]) -> number\n\
\n\
With two arguments, equivalent to x**y. With three arguments,\n\
equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
/* Return number of items in range/xrange (lo, hi, step). step > 0
* required. Return a value < 0 if & only if the true value is too
* large to fit in a signed long.
*/
static long
get_len_of_range(lo, hi, step)
long lo;
long hi;
long step; /* must be > 0 */
{
/* -------------------------------------------------------------
If lo >= hi, the range is empty.
Else if n values are in the range, the last one is
lo + (n-1)*step, which must be <= hi-1. Rearranging,
n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
the RHS is non-negative and so truncation is the same as the
floor. Letting M be the largest positive long, the worst case
for the RHS numerator is hi=M, lo=-M-1, and then
hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
precision to compute the RHS exactly.
---------------------------------------------------------------*/
long n = 0;
if (lo < hi) {
unsigned long uhi = (unsigned long)hi;
unsigned long ulo = (unsigned long)lo;
unsigned long diff = uhi - ulo - 1;
n = (long)(diff / (unsigned long)step + 1);
}
return n;
}
static PyObject *
builtin_range(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
long ilow = 0, ihigh = 0, istep = 1;
long bign;
1990-12-20 23:06:42 +08:00
int i, n;
PyObject *v;
if (PyTuple_Size(args) <= 1) {
if (!PyArg_ParseTuple(args,
"l;range() requires 1-3 int arguments",
&ihigh))
return NULL;
1990-12-20 23:06:42 +08:00
}
else {
if (!PyArg_ParseTuple(args,
"ll|l;range() requires 1-3 int arguments",
&ilow, &ihigh, &istep))
1990-12-20 23:06:42 +08:00
return NULL;
}
if (istep == 0) {
PyErr_SetString(PyExc_ValueError, "zero step for range()");
1990-12-20 23:06:42 +08:00
return NULL;
}
if (istep > 0)
bign = get_len_of_range(ilow, ihigh, istep);
else
bign = get_len_of_range(ihigh, ilow, -istep);
n = (int)bign;
if (bign < 0 || (long)n != bign) {
PyErr_SetString(PyExc_OverflowError,
"range() has too many items");
return NULL;
}
v = PyList_New(n);
1990-12-20 23:06:42 +08:00
if (v == NULL)
return NULL;
for (i = 0; i < n; i++) {
PyObject *w = PyInt_FromLong(ilow);
1990-12-20 23:06:42 +08:00
if (w == NULL) {
Py_DECREF(v);
1990-12-20 23:06:42 +08:00
return NULL;
}
PyList_SET_ITEM(v, i, w);
1990-12-20 23:06:42 +08:00
ilow += istep;
}
return v;
}
1998-06-27 05:23:49 +08:00
static char range_doc[] =
"range([start,] stop[, step]) -> list of integers\n\
\n\
Return a list containing an arithmetic progression of integers.\n\
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
When step is given, it specifies the increment (or decrement).\n\
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
These are exactly the valid indices for a list of 4 elements.";
static PyObject *
builtin_xrange(self, args)
PyObject *self;
PyObject *args;
{
long ilow = 0, ihigh = 0, istep = 1;
long n;
if (PyTuple_Size(args) <= 1) {
if (!PyArg_ParseTuple(args,
"l;xrange() requires 1-3 int arguments",
&ihigh))
return NULL;
}
else {
if (!PyArg_ParseTuple(args,
"ll|l;xrange() requires 1-3 int arguments",
&ilow, &ihigh, &istep))
return NULL;
}
if (istep == 0) {
PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
return NULL;
}
if (istep > 0)
n = get_len_of_range(ilow, ihigh, istep);
else
n = get_len_of_range(ihigh, ilow, -istep);
if (n < 0) {
PyErr_SetString(PyExc_OverflowError,
"xrange() has more than sys.maxint items");
return NULL;
}
return PyRange_New(ilow, n, istep, 1);
}
1998-06-27 05:23:49 +08:00
static char xrange_doc[] =
"xrange([start,] stop[, step]) -> xrange object\n\
\n\
Like range(), but instead of returning a list, returns an object that\n\
generates the numbers in the range on demand. This is slightly slower\n\
than range() but more memory efficient.";
static PyObject *
builtin_raw_input(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *v = NULL;
PyObject *f;
if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
return NULL;
if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
1995-07-27 00:26:31 +08:00
isatty(fileno(stdin)) && isatty(fileno(stdout))) {
PyObject *po;
char *prompt;
char *s;
PyObject *result;
if (v != NULL) {
po = PyObject_Str(v);
if (po == NULL)
return NULL;
prompt = PyString_AsString(po);
if (prompt == NULL)
return NULL;
}
else {
po = NULL;
prompt = "";
}
s = PyOS_Readline(prompt);
Py_XDECREF(po);
if (s == NULL) {
PyErr_SetNone(PyExc_KeyboardInterrupt);
return NULL;
}
if (*s == '\0') {
PyErr_SetNone(PyExc_EOFError);
result = NULL;
}
else { /* strip trailing '\n' */
result = PyString_FromStringAndSize(s, strlen(s)-1);
}
free(s);
return result;
}
1991-06-08 01:10:43 +09:00
if (v != NULL) {
f = PySys_GetObject("stdout");
if (f == NULL) {
PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
return NULL;
}
if (Py_FlushLine() != 0 ||
PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
1991-06-08 01:10:43 +09:00
return NULL;
}
f = PySys_GetObject("stdin");
if (f == NULL) {
PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
return NULL;
}
return PyFile_GetLine(f, -1);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char raw_input_doc[] =
"raw_input([prompt]) -> string\n\
\n\
Read a string from standard input. The trailing newline is stripped.\n\
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
is printed without a trailing newline before reading.";
static PyObject *
builtin_reduce(self, args)
PyObject *self;
PyObject *args;
{
PyObject *seq, *func, *result = NULL;
PySequenceMethods *sqf;
register int i;
if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
return NULL;
if (result != NULL)
Py_INCREF(result);
sqf = seq->ob_type->tp_as_sequence;
if (sqf == NULL || sqf->sq_item == NULL) {
PyErr_SetString(PyExc_TypeError,
"2nd argument to reduce() must be a sequence object");
return NULL;
}
if ((args = PyTuple_New(2)) == NULL)
goto Fail;
for (i = 0; ; ++i) {
PyObject *op2;
if (args->ob_refcnt > 1) {
Py_DECREF(args);
if ((args = PyTuple_New(2)) == NULL)
goto Fail;
}
if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
if (PyErr_ExceptionMatches(PyExc_IndexError)) {
PyErr_Clear();
break;
}
goto Fail;
}
if (result == NULL)
result = op2;
else {
PyTuple_SetItem(args, 0, result);
PyTuple_SetItem(args, 1, op2);
if ((result = PyEval_CallObject(func, args)) == NULL)
goto Fail;
}
}
Py_DECREF(args);
if (result == NULL)
PyErr_SetString(PyExc_TypeError,
"reduce of empty sequence with no initial value");
return result;
Fail:
Py_XDECREF(args);
Py_XDECREF(result);
return NULL;
}
1998-06-27 05:23:49 +08:00
static char reduce_doc[] =
"reduce(function, sequence[, initial]) -> value\n\
\n\
Apply a function of two arguments cumulatively to the items of a sequence,\n\
from left to right, so as to reduce the sequence to a single value.\n\
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
of the sequence in the calculation, and serves as a default when the\n\
sequence is empty.";
static PyObject *
builtin_reload(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:reload", &v))
return NULL;
return PyImport_ReloadModule(v);
1990-12-20 23:06:42 +08:00
}
1998-06-27 05:23:49 +08:00
static char reload_doc[] =
"reload(module) -> module\n\
\n\
Reload the module. The module must have been successfully imported before.";
static PyObject *
builtin_repr(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:repr", &v))
return NULL;
return PyObject_Repr(v);
}
1998-06-27 05:23:49 +08:00
static char repr_doc[] =
"repr(object) -> string\n\
\n\
Return the canonical string representation of the object.\n\
For most object types, eval(repr(object)) == object.";
static PyObject *
builtin_round(self, args)
PyObject *self;
PyObject *args;
{
double x;
double f;
int ndigits = 0;
int i;
if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
return NULL;
f = 1.0;
i = abs(ndigits);
while (--i >= 0)
f = f*10.0;
if (ndigits < 0)
x /= f;
else
x *= f;
if (x >= 0.0)
x = floor(x + 0.5);
else
x = ceil(x - 0.5);
if (ndigits < 0)
x *= f;
else
x /= f;
return PyFloat_FromDouble(x);
}
1998-06-27 05:23:49 +08:00
static char round_doc[] =
"round(number[, ndigits]) -> floating point number\n\
\n\
Round a number to a given precision in decimal digits (default 0 digits).\n\
This always returns a floating point number. Precision may be negative.";
static PyObject *
builtin_str(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:str", &v))
return NULL;
return PyObject_Str(v);
}
1998-06-27 05:23:49 +08:00
static char str_doc[] =
"str(object) -> string\n\
\n\
Return a nice string representation of the object.\n\
If the argument is a string, the return value is the same object.";
static PyObject *
builtin_tuple(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:tuple", &v))
return NULL;
return PySequence_Tuple(v);
}
1998-06-27 05:23:49 +08:00
static char tuple_doc[] =
"tuple(sequence) -> list\n\
\n\
Return a tuple whose items are the same as those of the argument sequence.\n\
If the argument is a tuple, the return value is the same object.";
static PyObject *
builtin_type(self, args)
PyObject *self;
PyObject *args;
1990-12-20 23:06:42 +08:00
{
PyObject *v;
if (!PyArg_ParseTuple(args, "O:type", &v))
1990-12-20 23:06:42 +08:00
return NULL;
v = (PyObject *)v->ob_type;
Py_INCREF(v);
1990-12-20 23:06:42 +08:00
return v;
}
1998-06-27 05:23:49 +08:00
static char type_doc[] =
"type(object) -> type object\n\
\n\
Return the type of the object.";
static PyObject *
builtin_vars(self, args)
PyObject *self;
PyObject *args;
{
PyObject *v = NULL;
PyObject *d;
if (!PyArg_ParseTuple(args, "|O:vars", &v))
return NULL;
if (v == NULL) {
d = PyEval_GetLocals();
1995-07-27 00:26:31 +08:00
if (d == NULL) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_SystemError,
"no locals!?");
1995-07-27 00:26:31 +08:00
}
else
Py_INCREF(d);
}
else {
d = PyObject_GetAttrString(v, "__dict__");
if (d == NULL) {
PyErr_SetString(PyExc_TypeError,
"vars() argument must have __dict__ attribute");
return NULL;
}
}
return d;
}
1998-06-27 05:23:49 +08:00
static char vars_doc[] =
"vars([object]) -> dictionary\n\
\n\
Without arguments, equivalent to locals().\n\
With an argument, equivalent to object.__dict__.";
static int
abstract_issubclass(derived, cls, err, first)
PyObject *derived;
PyObject *cls;
char *err;
int first;
{
static PyObject *__bases__ = NULL;
PyObject *bases;
int i, n;
int r = 0;
if (__bases__ == NULL) {
__bases__ = PyString_FromString("__bases__");
if (__bases__ == NULL)
return -1;
}
if (first) {
bases = PyObject_GetAttr(cls, __bases__);
if (bases == NULL || !PyTuple_Check(bases)) {
Py_XDECREF(bases);
PyErr_SetString(PyExc_TypeError, err);
return -1;
}
Py_DECREF(bases);
}
if (derived == cls)
return 1;
bases = PyObject_GetAttr(derived, __bases__);
if (bases == NULL || !PyTuple_Check(bases)) {
Py_XDECREF(bases);
PyErr_SetString(PyExc_TypeError, err);
return -1;
}
n = PyTuple_GET_SIZE(bases);
for (i = 0; i < n; i++) {
r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
cls, err, 0);
if (r != 0)
break;
}
Py_DECREF(bases);
return r;
}
1998-06-27 05:23:49 +08:00
static PyObject *
builtin_isinstance(self, args)
PyObject *self;
PyObject *args;
{
PyObject *inst;
PyObject *cls;
PyObject *icls;
static PyObject *__class__ = NULL;
int retval = 0;
if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
return NULL;
if (PyClass_Check(cls)) {
if (PyInstance_Check(inst)) {
PyObject *inclass =
(PyObject*)((PyInstanceObject*)inst)->in_class;
retval = PyClass_IsSubclass(inclass, cls);
}
}
else if (PyType_Check(cls)) {
retval = ((PyObject *)(inst->ob_type) == cls);
}
else if (!PyInstance_Check(inst)) {
if (__class__ == NULL) {
__class__ = PyString_FromString("__class__");
if (__class__ == NULL)
return NULL;
}
icls = PyObject_GetAttr(inst, __class__);
if (icls != NULL) {
retval = abstract_issubclass(
icls, cls,
"second argument must be a class",
1);
Py_DECREF(icls);
if (retval < 0)
return NULL;
}
else {
PyErr_SetString(PyExc_TypeError,
"second argument must be a class");
return NULL;
}
}
else {
PyErr_SetString(PyExc_TypeError,
"second argument must be a class");
return NULL;
}
return PyInt_FromLong(retval);
}
1998-06-27 05:23:49 +08:00
static char isinstance_doc[] =
"isinstance(object, class-or-type) -> Boolean\n\
\n\
Return whether an object is an instance of a class or of a subclass thereof.\n\
With a type as second argument, return whether that is the object's type.";
static PyObject *
builtin_issubclass(self, args)
PyObject *self;
PyObject *args;
{
PyObject *derived;
PyObject *cls;
int retval;
if (!PyArg_ParseTuple(args, "OO", &derived, &cls))
return NULL;
if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
retval = abstract_issubclass(
derived, cls, "arguments must be classes", 1);
if (retval < 0)
return NULL;
}
else {
/* shortcut */
if (!(retval = (derived == cls)))
retval = PyClass_IsSubclass(derived, cls);
}
return PyInt_FromLong(retval);
}
1998-06-27 05:23:49 +08:00
static char issubclass_doc[] =
"issubclass(C, B) -> Boolean\n\
\n\
Return whether class C is a subclass (i.e., a derived class) of class B.";
static PyMethodDef builtin_methods[] = {
1998-06-27 05:23:49 +08:00
{"__import__", builtin___import__, 1, import_doc},
{"abs", builtin_abs, 1, abs_doc},
{"apply", builtin_apply, 1, apply_doc},
{"buffer", builtin_buffer, 1, buffer_doc},
1998-06-27 05:23:49 +08:00
{"callable", builtin_callable, 1, callable_doc},
{"chr", builtin_chr, 1, chr_doc},
{"cmp", builtin_cmp, 1, cmp_doc},
{"coerce", builtin_coerce, 1, coerce_doc},
{"compile", builtin_compile, 1, compile_doc},
1996-01-12 09:09:56 +08:00
#ifndef WITHOUT_COMPLEX
1998-06-27 05:23:49 +08:00
{"complex", builtin_complex, 1, complex_doc},
1996-01-12 09:09:56 +08:00
#endif
1998-06-27 05:23:49 +08:00
{"delattr", builtin_delattr, 1, delattr_doc},
{"dir", builtin_dir, 1, dir_doc},
{"divmod", builtin_divmod, 1, divmod_doc},
{"eval", builtin_eval, 1, eval_doc},
{"execfile", builtin_execfile, 1, execfile_doc},
{"filter", builtin_filter, 1, filter_doc},
{"float", builtin_float, 1, float_doc},
{"getattr", builtin_getattr, 1, getattr_doc},
{"globals", builtin_globals, 1, globals_doc},
{"hasattr", builtin_hasattr, 1, hasattr_doc},
{"hash", builtin_hash, 1, hash_doc},
{"hex", builtin_hex, 1, hex_doc},
{"id", builtin_id, 1, id_doc},
{"input", builtin_input, 1, input_doc},
{"intern", builtin_intern, 1, intern_doc},
{"int", builtin_int, 1, int_doc},
{"isinstance", builtin_isinstance, 1, isinstance_doc},
{"issubclass", builtin_issubclass, 1, issubclass_doc},
{"len", builtin_len, 1, len_doc},
{"list", builtin_list, 1, list_doc},
{"locals", builtin_locals, 1, locals_doc},
{"long", builtin_long, 1, long_doc},
{"map", builtin_map, 1, map_doc},
{"max", builtin_max, 1, max_doc},
{"min", builtin_min, 1, min_doc},
{"oct", builtin_oct, 1, oct_doc},
{"open", builtin_open, 1, open_doc},
{"ord", builtin_ord, 1, ord_doc},
{"pow", builtin_pow, 1, pow_doc},
{"range", builtin_range, 1, range_doc},
{"raw_input", builtin_raw_input, 1, raw_input_doc},
{"reduce", builtin_reduce, 1, reduce_doc},
{"reload", builtin_reload, 1, reload_doc},
{"repr", builtin_repr, 1, repr_doc},
{"round", builtin_round, 1, round_doc},
{"setattr", builtin_setattr, 1, setattr_doc},
{"slice", builtin_slice, 1, slice_doc},
{"str", builtin_str, 1, str_doc},
{"tuple", builtin_tuple, 1, tuple_doc},
{"type", builtin_type, 1, type_doc},
{"vars", builtin_vars, 1, vars_doc},
{"xrange", builtin_xrange, 1, xrange_doc},
{NULL, NULL},
1990-12-20 23:06:42 +08:00
};
/* Predefined exceptions */
PyObject *PyExc_Exception;
PyObject *PyExc_StandardError;
PyObject *PyExc_ArithmeticError;
PyObject *PyExc_LookupError;
PyObject *PyExc_AssertionError;
PyObject *PyExc_AttributeError;
PyObject *PyExc_EOFError;
PyObject *PyExc_FloatingPointError;
PyObject *PyExc_EnvironmentError;
PyObject *PyExc_IOError;
PyObject *PyExc_OSError;
PyObject *PyExc_ImportError;
PyObject *PyExc_IndexError;
PyObject *PyExc_KeyError;
PyObject *PyExc_KeyboardInterrupt;
PyObject *PyExc_MemoryError;
PyObject *PyExc_NameError;
PyObject *PyExc_OverflowError;
PyObject *PyExc_RuntimeError;
PyObject *PyExc_NotImplementedError;
PyObject *PyExc_SyntaxError;
PyObject *PyExc_SystemError;
PyObject *PyExc_SystemExit;
PyObject *PyExc_TypeError;
PyObject *PyExc_ValueError;
PyObject *PyExc_ZeroDivisionError;
PyObject *PyExc_MemoryErrorInst;
static struct
{
char* name;
PyObject** exc;
int leaf_exc;
}
bltin_exc[] = {
{"Exception", &PyExc_Exception, 0},
{"StandardError", &PyExc_StandardError, 0},
{"ArithmeticError", &PyExc_ArithmeticError, 0},
{"LookupError", &PyExc_LookupError, 0},
{"AssertionError", &PyExc_AssertionError, 1},
{"AttributeError", &PyExc_AttributeError, 1},
{"EOFError", &PyExc_EOFError, 1},
{"FloatingPointError", &PyExc_FloatingPointError, 1},
{"EnvironmentError", &PyExc_EnvironmentError, 0},
{"IOError", &PyExc_IOError, 1},
{"OSError", &PyExc_OSError, 1},
{"ImportError", &PyExc_ImportError, 1},
{"IndexError", &PyExc_IndexError, 1},
{"KeyError", &PyExc_KeyError, 1},
{"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1},
{"MemoryError", &PyExc_MemoryError, 1},
{"NameError", &PyExc_NameError, 1},
{"OverflowError", &PyExc_OverflowError, 1},
{"RuntimeError", &PyExc_RuntimeError, 1},
{"NotImplementedError",&PyExc_NotImplementedError,1},
{"SyntaxError", &PyExc_SyntaxError, 1},
{"SystemError", &PyExc_SystemError, 1},
{"SystemExit", &PyExc_SystemExit, 1},
{"TypeError", &PyExc_TypeError, 1},
{"ValueError", &PyExc_ValueError, 1},
{"ZeroDivisionError", &PyExc_ZeroDivisionError, 1},
{NULL, NULL}
};
/* import exceptions module to extract class exceptions. on success,
* return 1. on failure return 0 which signals _PyBuiltin_Init_2 to fall
* back to using old-style string based exceptions.
*/
static int
init_class_exc(dict)
PyObject *dict;
{
int i;
PyObject *m = PyImport_ImportModule("exceptions");
PyObject *args = NULL;
PyObject *d = NULL;
/* make sure we got the module and its dictionary */
if (m == NULL ||
(d = PyModule_GetDict(m)) == NULL)
{
PySys_WriteStderr("'import exceptions' failed; ");
if (Py_VerboseFlag) {
PySys_WriteStderr("traceback:\n");
PyErr_Print();
}
else {
PySys_WriteStderr("use -v for traceback\n");
}
goto finally;
}
for (i = 0; bltin_exc[i].name; i++) {
/* dig the exception out of the module */
PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name);
if (!exc) {
PySys_WriteStderr(
"Built-in exception class not found: %s. Library mismatch?\n",
bltin_exc[i].name);
goto finally;
}
/* free the old-style exception string object */
Py_XDECREF(*bltin_exc[i].exc);
/* squirrel away a pointer to the exception */
Py_INCREF(exc);
*bltin_exc[i].exc = exc;
/* and insert the name in the __builtin__ module */
if (PyDict_SetItemString(dict, bltin_exc[i].name, exc)) {
PySys_WriteStderr(
"Cannot insert exception into __builtin__: %s\n",
bltin_exc[i].name);
goto finally;
}
}
/* we need one pre-allocated instance */
args = Py_BuildValue("()");
if (!args ||
!(PyExc_MemoryErrorInst =
PyEval_CallObject(PyExc_MemoryError, args)))
{
PySys_WriteStderr("Cannot pre-allocate MemoryError instance\n");
goto finally;
}
Py_DECREF(args);
/* we're done with the exceptions module */
Py_DECREF(m);
if (PyErr_Occurred()) {
PySys_WriteStderr("Cannot initialize standard class exceptions; ");
if (Py_VerboseFlag) {
PySys_WriteStderr("traceback:\n");
PyErr_Print();
}
else
PySys_WriteStderr("use -v for traceback\n");
goto finally;
}
return 1;
finally:
Py_XDECREF(m);
Py_XDECREF(args);
PyErr_Clear();
return 0;
}
static void
fini_instances()
{
Py_XDECREF(PyExc_MemoryErrorInst);
PyExc_MemoryErrorInst = NULL;
}
static PyObject *
newstdexception(dict, name)
PyObject *dict;
1991-12-16 23:42:38 +08:00
char *name;
1990-12-20 23:06:42 +08:00
{
PyObject *v = PyString_FromString(name);
if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
Py_FatalError("Cannot create string-based exceptions");
1990-12-20 23:06:42 +08:00
return v;
}
static void
initerrors(dict)
PyObject *dict;
1990-12-20 23:06:42 +08:00
{
int i, j;
int exccnt = 0;
for (i = 0; bltin_exc[i].name; i++, exccnt++) {
Py_XDECREF(*bltin_exc[i].exc);
if (bltin_exc[i].leaf_exc)
*bltin_exc[i].exc =
newstdexception(dict, bltin_exc[i].name);
}
/* This is kind of bogus because we special case the some of the
* new exceptions to be nearly forward compatible. But this means
* we hard code knowledge about exceptions.py into C here. I don't
* have a better solution, though.
*/
PyExc_LookupError = PyTuple_New(2);
Py_INCREF(PyExc_IndexError);
PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError);
Py_INCREF(PyExc_KeyError);
PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError);
PyDict_SetItemString(dict, "LookupError", PyExc_LookupError);
PyExc_ArithmeticError = PyTuple_New(3);
Py_INCREF(PyExc_OverflowError);
PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
Py_INCREF(PyExc_ZeroDivisionError);
PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
Py_INCREF(PyExc_FloatingPointError);
PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
PyExc_EnvironmentError = PyTuple_New(2);
Py_INCREF(PyExc_IOError);
PyTuple_SET_ITEM(PyExc_EnvironmentError, 0, PyExc_IOError);
Py_INCREF(PyExc_OSError);
PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
/* missing from the StandardError tuple: Exception, StandardError,
* and SystemExit
*/
PyExc_StandardError = PyTuple_New(exccnt-3);
for (i = 2, j = 0; bltin_exc[i].name; i++) {
PyObject *exc = *bltin_exc[i].exc;
/* SystemExit is not an error, but it is an exception */
if (exc != PyExc_SystemExit) {
Py_INCREF(exc);
PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
}
}
PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
/* Exception is a 2-tuple */
PyExc_Exception = PyTuple_New(2);
Py_INCREF(PyExc_SystemExit);
PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
Py_INCREF(PyExc_StandardError);
PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
PyDict_SetItemString(dict, "Exception", PyExc_Exception);
if (PyErr_Occurred())
Py_FatalError("Could not initialize built-in string exceptions");
}
static void
finierrors()
{
int i;
for (i = 0; bltin_exc[i].name; i++) {
PyObject *exc = *bltin_exc[i].exc;
Py_XDECREF(exc);
*bltin_exc[i].exc = NULL;
}
}
1998-06-27 05:23:49 +08:00
static char builtin_doc[] =
"Built-in functions, exceptions, and other objects.\n\
\n\
Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
PyObject *
_PyBuiltin_Init_1()
{
PyObject *mod, *dict;
1998-06-27 05:23:49 +08:00
mod = Py_InitModule4("__builtin__", builtin_methods,
builtin_doc, (PyObject *)NULL,
PYTHON_API_VERSION);
if (mod == NULL)
return NULL;
dict = PyModule_GetDict(mod);
initerrors(dict);
if (PyDict_SetItemString(dict, "None", Py_None) < 0)
return NULL;
if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
return NULL;
if (PyDict_SetItemString(dict, "__debug__",
PyInt_FromLong(Py_OptimizeFlag == 0)) < 0)
return NULL;
return mod;
1990-12-20 23:06:42 +08:00
}
void
_PyBuiltin_Init_2(dict)
PyObject *dict;
{
/* if Python was started with -X, initialize the class exceptions */
if (Py_UseClassExceptionsFlag) {
if (!init_class_exc(dict)) {
/* class based exceptions could not be
* initialized. Fall back to using string based
* exceptions.
*/
PySys_WriteStderr(
"Warning! Falling back to string-based exceptions\n");
initerrors(dict);
}
}
}
void
_PyBuiltin_Fini_1()
{
fini_instances();
}
void
_PyBuiltin_Fini_2()
1990-12-20 23:06:42 +08:00
{
finierrors();
1990-12-20 23:06:42 +08:00
}
/* Helper for filter(): filter a tuple through a function */
static PyObject *
filtertuple(func, tuple)
PyObject *func;
PyObject *tuple;
{
PyObject *result;
register int i, j;
int len = PyTuple_Size(tuple);
1995-08-04 12:07:45 +08:00
if (len == 0) {
Py_INCREF(tuple);
1995-08-04 12:07:45 +08:00
return tuple;
}
if ((result = PyTuple_New(len)) == NULL)
return NULL;
for (i = j = 0; i < len; ++i) {
PyObject *item, *good;
int ok;
if ((item = PyTuple_GetItem(tuple, i)) == NULL)
goto Fail_1;
if (func == Py_None) {
Py_INCREF(item);
good = item;
}
else {
PyObject *arg = Py_BuildValue("(O)", item);
if (arg == NULL)
goto Fail_1;
good = PyEval_CallObject(func, arg);
Py_DECREF(arg);
if (good == NULL)
goto Fail_1;
}
ok = PyObject_IsTrue(good);
Py_DECREF(good);
if (ok) {
Py_INCREF(item);
if (PyTuple_SetItem(result, j++, item) < 0)
goto Fail_1;
}
}
if (_PyTuple_Resize(&result, j, 0) < 0)
return NULL;
return result;
Fail_1:
Py_DECREF(result);
return NULL;
}
/* Helper for filter(): filter a string through a function */
static PyObject *
filterstring(func, strobj)
PyObject *func;
PyObject *strobj;
{
PyObject *result;
register int i, j;
int len = PyString_Size(strobj);
if (func == Py_None) {
/* No character is ever false -- share input string */
Py_INCREF(strobj);
return strobj;
}
if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
return NULL;
for (i = j = 0; i < len; ++i) {
PyObject *item, *arg, *good;
int ok;
item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
if (item == NULL)
goto Fail_1;
arg = Py_BuildValue("(O)", item);
Py_DECREF(item);
if (arg == NULL)
goto Fail_1;
good = PyEval_CallObject(func, arg);
Py_DECREF(arg);
if (good == NULL)
goto Fail_1;
ok = PyObject_IsTrue(good);
Py_DECREF(good);
if (ok)
PyString_AS_STRING((PyStringObject *)result)[j++] =
PyString_AS_STRING((PyStringObject *)item)[0];
}
if (j < len && _PyString_Resize(&result, j) < 0)
return NULL;
return result;
Fail_1:
Py_DECREF(result);
return NULL;
}