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
|
|
|
|
|
1996-10-25 11:44:06 -03: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 08:39:46 -04:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 08:39:46 -04:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03: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 08:39:46 -04:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* Built-in functions */
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
#include "Python.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
#include "node.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"
|
|
|
|
|
1997-04-11 17:37:35 -03:00
|
|
|
#include <ctype.h>
|
|
|
|
|
1996-12-10 11:37:36 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
/* Forward */
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *filterstring Py_PROTO((PyObject *, PyObject *));
|
|
|
|
static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *));
|
|
|
|
static PyObject *int_from_string Py_PROTO((PyObject *));
|
|
|
|
static PyObject *long_from_string Py_PROTO((PyObject *));
|
|
|
|
static PyObject *float_from_string Py_PROTO((PyObject *));
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin___import__(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-02 15:04:15 -04:00
|
|
|
{
|
|
|
|
char *name;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *globals = NULL;
|
|
|
|
PyObject *locals = NULL;
|
|
|
|
PyObject *fromlist = NULL;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s|OOO:__import__",
|
1995-02-14 05:42:43 -04:00
|
|
|
&name, &globals, &locals, &fromlist))
|
1995-01-02 15:04:15 -04:00
|
|
|
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 04:33:22 -03:00
|
|
|
return PyImport_ImportModuleEx(name, globals, locals, fromlist);
|
1995-01-02 15:04:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_abs(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyNumberMethods *nm;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:abs", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
|
|
|
if ((nm = v->ob_type->tp_as_number) == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"abs() requires numeric argument");
|
1991-05-05 17:00:36 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_apply(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-12-16 09:03:00 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *func, *alist = NULL, *kwdict = NULL;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
|
1991-12-16 09:03:00 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (alist != NULL && !PyTuple_Check(alist)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"apply() 2nd argument must be tuple");
|
1994-08-29 09:52:16 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
if (kwdict != NULL && !PyDict_Check(kwdict)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1995-07-18 11:51:37 -03:00
|
|
|
"apply() 3rd argument must be dictionary");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyEval_CallObjectWithKeywords(func, alist, kwdict);
|
1991-12-16 09:03:00 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1994-08-29 09:52:16 -03:00
|
|
|
builtin_callable(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1994-08-29 09:52:16 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:callable", &v))
|
1994-08-29 09:52:16 -03:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyInt_FromLong((long)PyCallable_Check(v));
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-11-03 11:01:26 -04:00
|
|
|
builtin_filter(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *func, *seq, *result;
|
|
|
|
PySequenceMethods *sqf;
|
1993-10-27 11:56:44 -03:00
|
|
|
int len;
|
|
|
|
register int i, j;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyString_Check(seq)) {
|
|
|
|
PyObject *r = filterstring(func, seq);
|
1993-10-26 14:58:25 -03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyTuple_Check(seq)) {
|
|
|
|
PyObject *r = filtertuple(func, seq);
|
1993-10-26 14:58:25 -03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1993-10-27 11:56:44 -03:00
|
|
|
if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_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;
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyList_Check(seq) && seq->ob_refcnt == 1) {
|
|
|
|
Py_INCREF(seq);
|
1993-10-26 14:58:25 -03:00
|
|
|
result = seq;
|
|
|
|
}
|
1993-10-27 11:56:44 -03:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((result = PyList_New(len)) == NULL)
|
1993-10-26 14:58:25 -03:00
|
|
|
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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *item, *good;
|
1993-10-27 11:56:44 -03:00
|
|
|
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;
|
1997-08-22 18:14:38 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_IndexError)) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
1994-08-29 09:52:16 -03:00
|
|
|
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
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (func == Py_None) {
|
1993-10-27 11:56:44 -03:00
|
|
|
good = item;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(good);
|
1993-10-27 11:56:44 -03:00
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *arg = Py_BuildValue("(O)", item);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (arg == NULL)
|
|
|
|
goto Fail_1;
|
1997-04-29 17:08:16 -03:00
|
|
|
good = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(arg);
|
1995-01-10 13:40:55 -04:00
|
|
|
if (good == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_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
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
ok = PyObject_IsTrue(good);
|
|
|
|
Py_DECREF(good);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (ok) {
|
1994-08-29 09:52:16 -03:00
|
|
|
if (j < len) {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyList_SetItem(result, j++, item) < 0)
|
1994-08-29 09:52:16 -03:00
|
|
|
goto Fail_1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
j++;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyList_Append(result, item) < 0)
|
1994-08-29 09:52:16 -03:00
|
|
|
goto Fail_1;
|
|
|
|
}
|
1995-01-10 13:40:55 -04:00
|
|
|
} else {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(item);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (j < len && PyList_SetSlice(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:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
Fail_2:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_chr(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
long x;
|
|
|
|
char s[1];
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "l:chr", &x))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
if (x < 0 || x >= 256) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"chr() arg not in range(256)");
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-11 17:37:35 -03:00
|
|
|
s[0] = (char)x;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyString_FromStringAndSize(s, 1);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1992-10-18 15:53:57 -03:00
|
|
|
builtin_cmp(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-10-18 15:53:57 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *a, *b;
|
1997-05-22 21:06:51 -03:00
|
|
|
long c;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
|
1992-10-18 15:53:57 -03:00
|
|
|
return NULL;
|
1997-05-22 21:06:51 -03:00
|
|
|
c = PyObject_Compare(a, b);
|
|
|
|
if (c && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
return PyInt_FromLong(c);
|
1992-10-18 15:53:57 -03:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-07 08:39:01 -04:00
|
|
|
builtin_coerce(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-07 08:39:01 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v, *w;
|
|
|
|
PyObject *res;
|
1995-01-07 08:39:01 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
|
1995-01-07 08:39:01 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyNumber_Coerce(&v, &w) < 0)
|
1995-01-10 11:26:20 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
res = Py_BuildValue("(OO)", v, w);
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
1995-01-10 11:26:20 -04:00
|
|
|
return res;
|
1995-01-07 08:39:01 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-03-30 13:46:03 -04:00
|
|
|
builtin_compile(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-03-30 13:46:03 -04:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
char *filename;
|
|
|
|
char *startstr;
|
|
|
|
int start;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
|
1993-03-30 13:46:03 -04:00
|
|
|
return NULL;
|
|
|
|
if (strcmp(startstr, "exec") == 0)
|
1997-05-07 14:46:13 -03:00
|
|
|
start = Py_file_input;
|
1993-03-30 13:46:03 -04:00
|
|
|
else if (strcmp(startstr, "eval") == 0)
|
1997-05-07 14:46:13 -03:00
|
|
|
start = Py_eval_input;
|
1995-07-07 19:43:42 -03:00
|
|
|
else if (strcmp(startstr, "single") == 0)
|
1997-05-07 14:46:13 -03:00
|
|
|
start = Py_single_input;
|
1993-03-30 13:46:03 -04:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_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;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
return Py_CompileString(str, filename, start);
|
1993-03-30 13:46:03 -04:00
|
|
|
}
|
|
|
|
|
1996-01-11 21:09:56 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1996-01-11 21:09:56 -04:00
|
|
|
builtin_complex(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1996-01-11 21:09:56 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *r, *i, *tmp;
|
|
|
|
PyNumberMethods *nbr, *nbi = NULL;
|
1996-07-20 23:27:43 -03:00
|
|
|
Py_complex cr, ci;
|
1997-03-31 13:15:43 -04:00
|
|
|
int own_r = 0;
|
1996-01-11 21:09:56 -04:00
|
|
|
|
|
|
|
i = NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
|
1996-01-11 21:09:56 -04:00
|
|
|
return NULL;
|
|
|
|
if ((nbr = r->ob_type->tp_as_number) == NULL ||
|
1997-03-31 13:15:43 -04:00
|
|
|
nbr->nb_float == NULL ||
|
|
|
|
(i != NULL &&
|
|
|
|
((nbi = i->ob_type->tp_as_number) == NULL ||
|
|
|
|
nbi->nb_float == NULL))) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1996-01-11 21:09:56 -04:00
|
|
|
"complex() argument can't be converted to complex");
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-05 19:18:18 -04:00
|
|
|
/* XXX Hack to support classes with __complex__ method */
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyInstance_Check(r)) {
|
|
|
|
static PyObject *complexstr;
|
|
|
|
PyObject *f;
|
1996-12-05 19:18:18 -04:00
|
|
|
if (complexstr == NULL) {
|
1997-01-18 04:04:16 -04:00
|
|
|
complexstr = PyString_InternFromString("__complex__");
|
1996-12-05 19:18:18 -04:00
|
|
|
if (complexstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
f = PyObject_GetAttr(r, complexstr);
|
1996-12-05 19:18:18 -04:00
|
|
|
if (f == NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
1996-12-05 19:18:18 -04:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *args = Py_BuildValue("()");
|
1996-12-05 19:18:18 -04:00
|
|
|
if (args == NULL)
|
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
r = PyEval_CallObject(f, args);
|
|
|
|
Py_DECREF(args);
|
1996-12-05 19:18:18 -04:00
|
|
|
if (r == NULL)
|
|
|
|
return NULL;
|
1997-03-31 13:15:43 -04:00
|
|
|
own_r = 1;
|
1996-12-05 19:18:18 -04:00
|
|
|
}
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyComplex_Check(r)) {
|
|
|
|
cr = ((PyComplexObject*)r)->cval;
|
1997-03-31 13:15:43 -04:00
|
|
|
if (own_r)
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(r);
|
1997-03-31 13:15:43 -04:00
|
|
|
}
|
1996-01-11 21:09:56 -04:00
|
|
|
else {
|
1996-08-08 15:49:41 -03:00
|
|
|
tmp = (*nbr->nb_float)(r);
|
1997-03-31 13:15:43 -04:00
|
|
|
if (own_r)
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(r);
|
1996-08-08 15:49:41 -03:00
|
|
|
if (tmp == NULL)
|
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
cr.real = PyFloat_AsDouble(tmp);
|
|
|
|
Py_DECREF(tmp);
|
1996-01-11 21:09:56 -04:00
|
|
|
cr.imag = 0.;
|
|
|
|
}
|
|
|
|
if (i == NULL) {
|
|
|
|
ci.real = 0.;
|
|
|
|
ci.imag = 0.;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
else if (PyComplex_Check(i))
|
|
|
|
ci = ((PyComplexObject*)i)->cval;
|
1996-01-11 21:09:56 -04:00
|
|
|
else {
|
1997-03-31 13:15:43 -04:00
|
|
|
tmp = (*nbi->nb_float)(i);
|
1996-08-08 15:49:41 -03:00
|
|
|
if (tmp == NULL)
|
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
ci.real = PyFloat_AsDouble(tmp);
|
|
|
|
Py_DECREF(tmp);
|
1996-01-11 21:09:56 -04:00
|
|
|
ci.imag = 0.;
|
|
|
|
}
|
|
|
|
cr.real -= ci.imag;
|
|
|
|
cr.imag += ci.real;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyComplex_FromCComplex(cr);
|
1996-01-11 21:09:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_dir(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-05-06 13:36:57 -03:00
|
|
|
static char *attrlist[] = {"__members__", "__methods__", NULL};
|
|
|
|
PyObject *v = NULL, *l = NULL, *m = NULL;
|
|
|
|
PyObject *d, *x;
|
|
|
|
int i;
|
|
|
|
char **s;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|O:dir", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL) {
|
1997-05-06 13:36:57 -03:00
|
|
|
x = PyEval_GetLocals();
|
|
|
|
if (x == NULL)
|
|
|
|
goto error;
|
|
|
|
l = PyMapping_Keys(x);
|
|
|
|
if (l == NULL)
|
|
|
|
goto error;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
d = PyObject_GetAttrString(v, "__dict__");
|
1997-05-06 13:36:57 -03:00
|
|
|
if (d == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
else {
|
|
|
|
l = PyMapping_Keys(d);
|
|
|
|
if (l == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_DECREF(d);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1997-05-06 13:36:57 -03:00
|
|
|
if (l == NULL) {
|
|
|
|
l = PyList_New(0);
|
|
|
|
if (l == NULL)
|
|
|
|
goto error;
|
1991-10-20 17:11:03 -03:00
|
|
|
}
|
1997-05-06 13:36:57 -03:00
|
|
|
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);
|
1996-05-23 19:49:07 -03:00
|
|
|
}
|
1991-10-24 11:54:44 -03:00
|
|
|
}
|
1997-05-06 13:36:57 -03:00
|
|
|
if (PyList_Sort(l) != 0)
|
|
|
|
goto error;
|
|
|
|
return l;
|
|
|
|
error:
|
|
|
|
Py_XDECREF(l);
|
|
|
|
return NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-07 08:39:01 -04:00
|
|
|
do_divmod(v, w)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v, *w;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *res;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyInstance_Check(v) || PyInstance_Check(w))
|
|
|
|
return PyInstance_DoBinOp(v, w, "__divmod__", "__rdivmod__",
|
1995-01-07 08:39:01 -04:00
|
|
|
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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1995-01-02 15:04:15 -04:00
|
|
|
"divmod() requires numeric or class instance arguments");
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyNumber_Coerce(&v, &w) != 0)
|
1991-07-01 15:42:41 -03:00
|
|
|
return NULL;
|
1995-01-07 08:39:01 -04:00
|
|
|
res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
1995-01-07 08:39:01 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-07 08:39:01 -04:00
|
|
|
builtin_divmod(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-07 08:39:01 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v, *w;
|
1995-01-07 08:39:01 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
|
1995-01-07 08:39:01 -04:00
|
|
|
return NULL;
|
|
|
|
return do_divmod(v, w);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_eval(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *cmd;
|
|
|
|
PyObject *globals = Py_None, *locals = Py_None;
|
1995-01-02 15:04:15 -04:00
|
|
|
char *str;
|
1993-11-30 09:40:46 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O|O!O!:eval",
|
1995-01-02 15:04:15 -04:00
|
|
|
&cmd,
|
1997-04-29 17:08:16 -03:00
|
|
|
&PyDict_Type, &globals,
|
|
|
|
&PyDict_Type, &locals))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (globals == Py_None) {
|
|
|
|
globals = PyEval_GetGlobals();
|
|
|
|
if (locals == Py_None)
|
|
|
|
locals = PyEval_GetLocals();
|
1995-01-09 13:53:26 -04:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
else if (locals == Py_None)
|
1995-01-09 13:53:26 -04:00
|
|
|
locals = globals;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
|
|
|
|
if (PyDict_SetItemString(globals, "__builtins__",
|
|
|
|
PyEval_GetBuiltins()) != 0)
|
1995-01-09 13:53:26 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyCode_Check(cmd))
|
|
|
|
return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
|
|
|
|
if (!PyString_Check(cmd)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1995-01-02 15:04:15 -04:00
|
|
|
"eval() argument 1 must be string or code object");
|
1992-08-14 12:14:30 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
str = PyString_AsString(cmd);
|
|
|
|
if ((int)strlen(str) != PyString_Size(cmd)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
1995-01-02 15:04:15 -04:00
|
|
|
"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++;
|
1997-05-07 14:46:13 -03:00
|
|
|
return PyRun_String(str, Py_eval_input, globals, locals);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_execfile(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-02-25 14:55:05 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
char *filename;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *globals = Py_None, *locals = Py_None;
|
|
|
|
PyObject *res;
|
1992-02-25 14:55:05 -04:00
|
|
|
FILE* fp;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
|
1995-01-02 15:04:15 -04:00
|
|
|
&filename,
|
1997-04-29 17:08:16 -03:00
|
|
|
&PyDict_Type, &globals,
|
|
|
|
&PyDict_Type, &locals))
|
1992-08-14 12:14:30 -03:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (globals == Py_None) {
|
|
|
|
globals = PyEval_GetGlobals();
|
|
|
|
if (locals == Py_None)
|
|
|
|
locals = PyEval_GetLocals();
|
1995-01-09 13:53:26 -04:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
else if (locals == Py_None)
|
1995-01-09 13:53:26 -04:00
|
|
|
locals = globals;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
|
|
|
|
if (PyDict_SetItemString(globals, "__builtins__",
|
|
|
|
PyEval_GetBuiltins()) != 0)
|
1995-01-09 13:53:26 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1995-01-02 15:04:15 -04:00
|
|
|
fp = fopen(filename, "r");
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-02-25 14:55:05 -04:00
|
|
|
if (fp == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-02-25 14:55:05 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-07 14:46:13 -03:00
|
|
|
res = PyRun_File(fp, filename, Py_file_input, globals, locals);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-02-25 14:55:05 -04:00
|
|
|
fclose(fp);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1995-01-02 15:04:15 -04:00
|
|
|
return res;
|
1992-02-25 14:55:05 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_float(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyNumberMethods *nb;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:float", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyString_Check(v))
|
1997-03-31 13:15:43 -04:00
|
|
|
return float_from_string(v);
|
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_float == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-12 08:09:23 -03:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_getattr(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-01-27 12:53:09 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyObject *name;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OS:getattr", &v, &name))
|
1992-01-27 12:53:09 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyObject_GetAttr(v, name);
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-07-07 19:43:42 -03:00
|
|
|
builtin_globals(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-07-07 19:43:42 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *d;
|
1995-07-07 19:43:42 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, ""))
|
1995-07-07 19:43:42 -03:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
d = PyEval_GetGlobals();
|
|
|
|
Py_INCREF(d);
|
1995-07-07 19:43:42 -03:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-03-29 06:43:31 -04:00
|
|
|
builtin_hasattr(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyObject *name;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name))
|
1993-03-29 06:43:31 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyObject_GetAttr(v, name);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (v == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
return PyInt_FromLong(0L);
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
|
|
|
return PyInt_FromLong(1L);
|
1992-01-27 12:53:09 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-03-30 13:46:03 -04:00
|
|
|
builtin_id(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-03-30 13:46:03 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:id", &v))
|
1993-03-30 13:46:03 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyInt_FromLong((long)v);
|
1993-03-30 13:46:03 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-10-26 14:58:25 -03:00
|
|
|
builtin_map(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
|
|
|
typedef struct {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *seq;
|
|
|
|
PySequenceMethods *sqf;
|
1993-10-26 14:58:25 -03:00
|
|
|
int len;
|
|
|
|
} sequence;
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *func, *result;
|
1993-10-26 14:58:25 -03:00
|
|
|
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;
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
n = PyTuple_Size(args);
|
1995-01-02 15:04:15 -04:00
|
|
|
if (n < 2) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"map() requires at least two args");
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
func = PyTuple_GetItem(args, 0);
|
1995-01-02 15:04:15 -04:00
|
|
|
n--;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
|
|
|
|
PyErr_NoMemory();
|
1993-10-27 11:56:44 -03:00
|
|
|
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;
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
|
1993-10-26 14:58:25 -03:00
|
|
|
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";
|
1997-04-30 16:00:27 -03:00
|
|
|
char errbuf[sizeof(errmsg) + 25];
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
sprintf(errbuf, errmsg, i+2);
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, errbuf);
|
1993-10-26 14:58:25 -03:00
|
|
|
goto Fail_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
|
|
|
|
goto Fail_2;
|
|
|
|
|
|
|
|
if (curlen > len)
|
|
|
|
len = curlen;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((result = (PyObject *) PyList_New(len)) == NULL)
|
1993-10-26 14:58:25 -03:00
|
|
|
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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *alist, *item=NULL, *value;
|
1994-08-29 09:52:16 -03:00
|
|
|
int any = 0;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (func == Py_None && n == 1)
|
1995-07-10 10:52:21 -03:00
|
|
|
alist = NULL;
|
1994-08-29 09:52:16 -03:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((alist = PyTuple_New(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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
item = Py_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;
|
1997-08-22 18:14:38 -03:00
|
|
|
if (PyErr_ExceptionMatches(
|
|
|
|
PyExc_IndexError))
|
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
item = Py_None;
|
1994-08-29 09:52:16 -03:00
|
|
|
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;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyTuple_SetItem(alist, j, item) < 0) {
|
|
|
|
Py_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:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_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
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (func == Py_None)
|
1995-07-10 10:52:21 -03:00
|
|
|
value = alist;
|
1993-10-26 14:58:25 -03:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
value = PyEval_CallObject(func, alist);
|
|
|
|
Py_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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyList_Append(result, value) < 0)
|
1994-08-29 09:52:16 -03:00
|
|
|
goto Fail_1;
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyList_SetItem(result, i, value) < 0)
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_1;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyMem_DEL(seqs);
|
1993-10-26 14:58:25 -03:00
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail_1:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
Fail_2:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (seqs) PyMem_DEL(seqs);
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_setattr(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-01-27 12:53:09 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyObject *name;
|
|
|
|
PyObject *value;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value))
|
1992-01-27 12:53:09 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyObject_SetAttr(v, name, value) != 0)
|
1992-01-27 12:53:09 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-01-27 12:53:09 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1994-08-29 09:53:40 -03:00
|
|
|
builtin_delattr(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1994-08-29 09:53:40 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyObject *name;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name))
|
1994-08-29 09:53:40 -03:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
|
1994-08-29 09:53:40 -03:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-08-29 09:53:40 -03:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-03-29 06:43:31 -04:00
|
|
|
builtin_hash(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1993-03-29 06:43:31 -04:00
|
|
|
long x;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:hash", &v))
|
1993-03-29 06:43:31 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
x = PyObject_Hash(v);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (x == -1)
|
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyInt_FromLong(x);
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_hex(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-10-24 11:54:44 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyNumberMethods *nb;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:hex", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-12 08:09:23 -03:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *));
|
1992-09-25 18:59:05 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_input(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *line;
|
1995-01-02 15:04:15 -04:00
|
|
|
char *str;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *res;
|
|
|
|
PyObject *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;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
|
|
|
while (*str == ' ' || *str == '\t')
|
|
|
|
str++;
|
1997-04-29 17:08:16 -03:00
|
|
|
globals = PyEval_GetGlobals();
|
|
|
|
locals = PyEval_GetLocals();
|
|
|
|
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
|
|
|
|
if (PyDict_SetItemString(globals, "__builtins__",
|
|
|
|
PyEval_GetBuiltins()) != 0)
|
1995-01-09 13:53:26 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-07 14:46:13 -03:00
|
|
|
res = PyRun_String(str, Py_eval_input, globals, locals);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(line);
|
1995-01-02 15:04:15 -04:00
|
|
|
return res;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-02-14 11:48:05 -04: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;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_int(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyNumberMethods *nb;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:int", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyString_Check(v))
|
1997-03-31 13:15:43 -04:00
|
|
|
return int_from_string(v);
|
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_int == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-12 08:09:23 -03:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_len(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1990-12-20 11:06:42 -04:00
|
|
|
long len;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyTypeObject *tp;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(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 {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "len() of unsized object");
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
if (len < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyInt_FromLong(len);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1996-04-08 23:41:06 -03:00
|
|
|
builtin_list(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1996-04-08 23:41:06 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PySequenceMethods *sqf;
|
1996-04-08 23:41:06 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:list", &v))
|
1996-04-08 23:41:06 -03:00
|
|
|
return NULL;
|
|
|
|
if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
|
|
|
|
int n = (*sqf->sq_length)(v);
|
|
|
|
int i;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *l;
|
1996-04-08 23:41:06 -03:00
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
l = PyList_New(n);
|
1996-04-08 23:41:06 -03:00
|
|
|
if (l == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *item = (*sqf->sq_item)(v, i);
|
1996-04-08 23:41:06 -03:00
|
|
|
if (item == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(l);
|
1996-04-08 23:41:06 -03:00
|
|
|
l = NULL;
|
|
|
|
break;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
PyList_SetItem(l, i, item);
|
1996-04-08 23:41:06 -03:00
|
|
|
}
|
|
|
|
/* XXX Should support indefinite-length sequences */
|
|
|
|
return l;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "list() argument must be a sequence");
|
1996-04-08 23:41:06 -03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-07-07 19:43:42 -03:00
|
|
|
builtin_locals(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-07-07 19:43:42 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *d;
|
1995-07-07 19:43:42 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, ""))
|
1995-07-07 19:43:42 -03:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
d = PyEval_GetLocals();
|
|
|
|
Py_INCREF(d);
|
1995-07-07 19:43:42 -03:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_long(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-05-05 17:00:36 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyNumberMethods *nb;
|
1992-09-12 08:09:23 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:long", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyString_Check(v))
|
1997-03-31 13:15:43 -04:00
|
|
|
return long_from_string(v);
|
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_long == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-12 08:09:23 -03:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
min_max(args, sign)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
int sign;
|
|
|
|
{
|
1994-08-29 09:52:16 -03:00
|
|
|
int i;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v, *w, *x;
|
|
|
|
PySequenceMethods *sq;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyTuple_Size(args) > 1)
|
1995-01-02 15:04:15 -04:00
|
|
|
v = args;
|
1997-04-29 17:08:16 -03:00
|
|
|
else if (!PyArg_ParseTuple(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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"min() or max() of non-sequence");
|
1990-12-20 11:06:42 -04:00
|
|
|
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) {
|
1997-08-22 18:14:38 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_IndexError)) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
1994-08-29 09:52:16 -03:00
|
|
|
break;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(w);
|
1994-08-29 09:52:16 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (w == NULL)
|
1990-12-20 11:06:42 -04:00
|
|
|
w = x;
|
1994-08-29 09:52:16 -03:00
|
|
|
else {
|
1997-05-22 21:06:51 -03:00
|
|
|
int c = PyObject_Compare(x, w);
|
|
|
|
if (c && PyErr_Occurred()) {
|
|
|
|
Py_DECREF(x);
|
|
|
|
Py_XDECREF(w);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (c * sign > 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(w);
|
1994-08-29 09:52:16 -03:00
|
|
|
w = x;
|
|
|
|
}
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(x);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
}
|
1994-08-29 09:52:16 -03:00
|
|
|
if (w == NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"min() or max() of empty sequence");
|
1990-12-20 11:06:42 -04:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
builtin_min(self, v)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *v;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
return min_max(v, -1);
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
builtin_max(self, v)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *v;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
return min_max(v, 1);
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_oct(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-10-24 11:54:44 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyNumberMethods *nb;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:oct", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-12 08:09:23 -03:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_open(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *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;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *f;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
f = PyFile_FromString(name, mode);
|
1994-08-29 09:52:16 -03:00
|
|
|
if (f != NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyFile_SetBufSize(f, bufsize);
|
1994-08-29 09:52:16 -03:00
|
|
|
return f;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1992-08-14 12:14:30 -03:00
|
|
|
builtin_ord(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1995-01-02 15:04:15 -04:00
|
|
|
char c;
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "c:ord", &c))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyInt_FromLong((long)(c & 0xff));
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-07 08:39:01 -04:00
|
|
|
do_pow(v, w)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v, *w;
|
1995-01-07 08:39:01 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *res;
|
|
|
|
if (PyInstance_Check(v) || PyInstance_Check(w))
|
|
|
|
return PyInstance_DoBinOp(v, w, "__pow__", "__rpow__", do_pow);
|
1995-01-07 08:39:01 -04:00
|
|
|
if (v->ob_type->tp_as_number == NULL ||
|
|
|
|
w->ob_type->tp_as_number == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"pow() requires numeric arguments");
|
1995-01-07 08:39:01 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-01-11 21:09:56 -04:00
|
|
|
if (
|
|
|
|
#ifndef WITHOUT_COMPLEX
|
1997-04-29 17:08:16 -03:00
|
|
|
!PyComplex_Check(v) &&
|
1996-01-11 21:09:56 -04:00
|
|
|
#endif
|
1997-04-29 17:08:16 -03:00
|
|
|
PyFloat_Check(w) && PyFloat_AsDouble(v) < 0.0) {
|
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"negative number to float power");
|
1995-02-10 12:57:16 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyNumber_Coerce(&v, &w) != 0)
|
1995-01-07 08:39:01 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
res = (*v->ob_type->tp_as_number->nb_power)(v, w, Py_None);
|
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
1995-01-07 08:39:01 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1991-07-01 15:42:41 -03:00
|
|
|
builtin_pow(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-05-05 17:00:36 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v, *w, *z = Py_None, *res;
|
|
|
|
PyObject *v1, *z1, *w2, *z2;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
|
1994-09-29 06:45:57 -03:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (z == Py_None)
|
1995-01-07 08:39:01 -04:00
|
|
|
return do_pow(v, w);
|
|
|
|
/* XXX The ternary version doesn't do class instance coercions */
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyInstance_Check(v))
|
1995-01-07 08:39:01 -04:00
|
|
|
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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"pow() requires numeric arguments");
|
1991-05-05 17:00:36 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyNumber_Coerce(&v, &w) != 0)
|
1991-07-01 15:42:41 -03:00
|
|
|
return NULL;
|
1995-01-07 08:39:01 -04:00
|
|
|
res = NULL;
|
|
|
|
v1 = v;
|
|
|
|
z1 = z;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyNumber_Coerce(&v1, &z1) != 0)
|
1995-01-07 08:39:01 -04:00
|
|
|
goto error2;
|
|
|
|
w2 = w;
|
|
|
|
z2 = z1;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyNumber_Coerce(&w2, &z2) != 0)
|
1995-01-07 08:39:01 -04:00
|
|
|
goto error1;
|
|
|
|
res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(w2);
|
|
|
|
Py_DECREF(z2);
|
1995-01-07 08:39:01 -04:00
|
|
|
error1:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v1);
|
|
|
|
Py_DECREF(z1);
|
1995-01-07 08:39:01 -04:00
|
|
|
error2:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
|
|
|
Py_DECREF(w);
|
1995-01-07 08:39:01 -04:00
|
|
|
return res;
|
1991-05-05 17:00:36 -03:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_range(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *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;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyTuple_Size(args) <= 1) {
|
|
|
|
if (!PyArg_ParseTuple(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 {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_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;
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyList_New(n);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *w = PyInt_FromLong(ilow);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (w == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
PyList_SetItem(v, i, w);
|
1990-12-20 11:06:42 -04:00
|
|
|
ilow += istep;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_xrange(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *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
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyTuple_Size(args) <= 1) {
|
|
|
|
if (!PyArg_ParseTuple(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 {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
|
1993-10-26 14:58:25 -03:00
|
|
|
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;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyRange_New(ilow, n, istep, 1);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
extern char *PyOS_Readline Py_PROTO((char *));
|
1995-07-07 19:43:42 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_raw_input(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v = NULL;
|
|
|
|
PyObject *f;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
|
1992-09-25 18:59:05 -03:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
|
|
|
|
PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
|
1995-07-26 13:26:31 -03:00
|
|
|
isatty(fileno(stdin)) && isatty(fileno(stdout))) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *po;
|
1995-07-07 19:43:42 -03:00
|
|
|
char *prompt;
|
|
|
|
char *s;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *result;
|
1995-07-07 19:43:42 -03:00
|
|
|
if (v != NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
po = PyObject_Str(v);
|
1995-07-07 19:43:42 -03:00
|
|
|
if (po == NULL)
|
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
prompt = PyString_AsString(po);
|
1995-07-07 19:43:42 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
po = NULL;
|
|
|
|
prompt = "";
|
|
|
|
}
|
1997-09-26 18:47:43 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1997-04-29 17:08:16 -03:00
|
|
|
s = PyOS_Readline(prompt);
|
1997-09-26 18:47:43 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(po);
|
1995-07-07 19:43:42 -03:00
|
|
|
if (s == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetNone(PyExc_KeyboardInterrupt);
|
1995-07-07 19:43:42 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (*s == '\0') {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
1995-07-07 19:43:42 -03:00
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
else { /* strip trailing '\n' */
|
1997-04-29 17:08:16 -03:00
|
|
|
result = PyString_FromStringAndSize(s, strlen(s)-1);
|
1995-07-07 19:43:42 -03:00
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
return result;
|
|
|
|
}
|
1991-06-07 13:10:43 -03:00
|
|
|
if (v != NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
f = PySys_GetObject("stdout");
|
1995-01-02 15:04:15 -04:00
|
|
|
if (f == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-22 21:06:51 -03:00
|
|
|
if (Py_FlushLine() != 0 ||
|
|
|
|
PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
|
1991-06-07 13:10:43 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
f = PySys_GetObject("stdin");
|
1995-01-02 15:04:15 -04:00
|
|
|
if (f == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyFile_GetLine(f, -1);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-10-26 14:58:25 -03:00
|
|
|
builtin_reduce(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *seq, *func, *result = NULL;
|
|
|
|
PySequenceMethods *sqf;
|
1993-10-26 14:58:25 -03:00
|
|
|
register int i;
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
|
|
|
if (result != NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1993-10-26 14:58:25 -03:00
|
|
|
"2nd argument to reduce() must be a sequence object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((args = PyTuple_New(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) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *op2;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
if (args->ob_refcnt > 1) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(args);
|
|
|
|
if ((args = PyTuple_New(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) {
|
1997-08-22 18:14:38 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_IndexError)) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_Clear();
|
1994-08-29 09:52:16 -03:00
|
|
|
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 {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyTuple_SetItem(args, 0, result);
|
|
|
|
PyTuple_SetItem(args, 1, op2);
|
|
|
|
if ((result = PyEval_CallObject(func, args)) == NULL)
|
1994-08-29 09:52:16 -03:00
|
|
|
goto Fail;
|
|
|
|
}
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(args);
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1994-08-29 09:52:16 -03:00
|
|
|
if (result == NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1994-08-29 09:52:16 -03:00
|
|
|
"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:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XDECREF(args);
|
|
|
|
Py_XDECREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_reload(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:reload", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyImport_ReloadModule(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_repr(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-11-26 04:54:07 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:repr", &v))
|
1992-11-26 04:54:07 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyObject_Repr(v);
|
1992-11-26 04:54:07 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-02-12 12:29:05 -04:00
|
|
|
builtin_round(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1993-02-12 12:29:05 -04:00
|
|
|
{
|
|
|
|
double x;
|
|
|
|
double f;
|
|
|
|
int ndigits = 0;
|
|
|
|
int i;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(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)
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyFloat_FromDouble(floor(x*f + 0.5) / f);
|
1993-02-12 12:29:05 -04:00
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyFloat_FromDouble(ceil(x*f - 0.5) / f);
|
1993-02-12 12:29:05 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_str(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-11-26 04:54:07 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:str", &v))
|
1992-11-26 04:54:07 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyObject_Str(v);
|
1992-11-26 04:54:07 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_tuple(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1994-08-29 09:53:11 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
|
|
|
PySequenceMethods *sqf;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:tuple", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyTuple_Check(v)) {
|
|
|
|
Py_INCREF(v);
|
1994-08-29 09:53:11 -03:00
|
|
|
return v;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyList_Check(v))
|
|
|
|
return PyList_AsTuple(v);
|
|
|
|
if (PyString_Check(v)) {
|
|
|
|
int n = PyString_Size(v);
|
|
|
|
PyObject *t = PyTuple_New(n);
|
1994-08-29 09:53:11 -03:00
|
|
|
if (t != NULL) {
|
|
|
|
int i;
|
1997-04-29 17:08:16 -03:00
|
|
|
char *p = PyString_AsString(v);
|
1994-08-29 09:53:11 -03:00
|
|
|
for (i = 0; i < n; i++) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *item =
|
|
|
|
PyString_FromStringAndSize(p+i, 1);
|
1994-08-29 09:53:11 -03:00
|
|
|
if (item == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(t);
|
1994-08-29 09:53:11 -03:00
|
|
|
t = NULL;
|
|
|
|
break;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
PyTuple_SetItem(t, i, item);
|
1994-08-29 09:53:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
/* Generic sequence object */
|
|
|
|
if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
|
|
|
|
int n = (*sqf->sq_length)(v);
|
|
|
|
int i;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *t;
|
1994-08-29 09:53:11 -03:00
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
t = PyTuple_New(n);
|
1994-08-29 09:53:11 -03:00
|
|
|
if (t == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *item = (*sqf->sq_item)(v, i);
|
1994-08-29 09:53:11 -03:00
|
|
|
if (item == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(t);
|
1994-08-29 09:53:11 -03:00
|
|
|
t = NULL;
|
|
|
|
break;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
PyTuple_SetItem(t, i, item);
|
1994-08-29 09:53:11 -03:00
|
|
|
}
|
|
|
|
/* XXX Should support indefinite-length sequences */
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
/* None of the above */
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"tuple() argument must be a sequence");
|
1994-08-29 09:53:11 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_type(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O:type", &v))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
v = (PyObject *)v->ob_type;
|
|
|
|
Py_INCREF(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1995-01-02 15:04:15 -04:00
|
|
|
builtin_vars(self, args)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1994-08-29 09:52:16 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v = NULL;
|
|
|
|
PyObject *d;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|O:vars", &v))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1994-08-29 09:52:16 -03:00
|
|
|
if (v == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
d = PyEval_GetLocals();
|
1995-07-26 13:26:31 -03:00
|
|
|
if (d == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"no locals!?");
|
1995-07-26 13:26:31 -03:00
|
|
|
}
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(d);
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
d = PyObject_GetAttrString(v, "__dict__");
|
1994-08-29 09:52:16 -03:00
|
|
|
if (d == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1994-08-29 09:52:16 -03:00
|
|
|
"vars() argument must have __dict__ attribute");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1997-08-22 18:14:38 -03:00
|
|
|
static PyObject *
|
|
|
|
builtin_isinstance(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyObject *inst;
|
|
|
|
PyObject *cls;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "OO", &inst, &cls))
|
|
|
|
return NULL;
|
1997-12-02 15:11:45 -04:00
|
|
|
if (PyType_Check(cls)) {
|
1997-12-10 01:51:47 -04:00
|
|
|
retval = ((PyObject *)(inst->ob_type) == cls);
|
1997-08-22 18:14:38 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-12-02 15:11:45 -04:00
|
|
|
if (!PyClass_Check(cls)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"second argument must be a class");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PyInstance_Check(inst))
|
|
|
|
retval = 0;
|
|
|
|
else {
|
|
|
|
PyObject *inclass =
|
|
|
|
(PyObject*)((PyInstanceObject*)inst)->in_class;
|
|
|
|
retval = PyClass_IsSubclass(inclass, cls);
|
|
|
|
}
|
1997-08-22 18:14:38 -03:00
|
|
|
}
|
|
|
|
return PyInt_FromLong(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "arguments must be classes");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* shortcut */
|
|
|
|
if (!(retval = (derived == cls)))
|
|
|
|
retval = PyClass_IsSubclass(derived, cls);
|
|
|
|
|
|
|
|
return PyInt_FromLong(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyMethodDef 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},
|
1997-02-14 11:48:05 -04:00
|
|
|
{"intern", builtin_intern, 1},
|
1995-01-02 15:04:15 -04:00
|
|
|
{"int", builtin_int, 1},
|
1997-08-22 18:14:38 -03:00
|
|
|
{"isinstance", builtin_isinstance, 1},
|
|
|
|
{"issubclass", builtin_issubclass, 1},
|
1995-01-02 15:04:15 -04:00
|
|
|
{"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
|
|
|
};
|
|
|
|
|
|
|
|
/* Predefined exceptions */
|
|
|
|
|
1997-09-16 15:43:15 -03:00
|
|
|
PyObject *PyExc_Exception;
|
1997-08-29 19:13:51 -03:00
|
|
|
PyObject *PyExc_StandardError;
|
1997-09-16 18:51:14 -03:00
|
|
|
PyObject *PyExc_ArithmeticError;
|
1997-08-29 19:13:51 -03:00
|
|
|
PyObject *PyExc_LookupError;
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *PyExc_AssertionError;
|
|
|
|
PyObject *PyExc_AttributeError;
|
|
|
|
PyObject *PyExc_EOFError;
|
1997-05-09 00:03:23 -03:00
|
|
|
PyObject *PyExc_FloatingPointError;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *PyExc_IOError;
|
|
|
|
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_SyntaxError;
|
|
|
|
PyObject *PyExc_SystemError;
|
|
|
|
PyObject *PyExc_SystemExit;
|
|
|
|
PyObject *PyExc_TypeError;
|
|
|
|
PyObject *PyExc_ValueError;
|
|
|
|
PyObject *PyExc_ZeroDivisionError;
|
|
|
|
|
1997-08-29 19:13:51 -03:00
|
|
|
PyObject *PyExc_MemoryErrorInst;
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
PyObject** exc;
|
|
|
|
int leaf_exc;
|
|
|
|
}
|
|
|
|
bltin_exc[] = {
|
1997-09-16 15:43:15 -03:00
|
|
|
{"Exception", &PyExc_Exception, 0},
|
1997-08-29 19:13:51 -03:00
|
|
|
{"StandardError", &PyExc_StandardError, 0},
|
1997-09-16 18:51:14 -03:00
|
|
|
{"ArithmeticError", &PyExc_ArithmeticError, 0},
|
1997-08-29 19:13:51 -03:00
|
|
|
{"LookupError", &PyExc_LookupError, 0},
|
|
|
|
{"AssertionError", &PyExc_AssertionError, 1},
|
|
|
|
{"AttributeError", &PyExc_AttributeError, 1},
|
|
|
|
{"EOFError", &PyExc_EOFError, 1},
|
|
|
|
{"FloatingPointError", &PyExc_FloatingPointError, 1},
|
|
|
|
{"IOError", &PyExc_IOError, 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},
|
|
|
|
{"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 */
|
|
|
|
static void
|
|
|
|
init_class_exc(dict)
|
|
|
|
PyObject *dict;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PyObject *m = PyImport_ImportModule("exceptions");
|
|
|
|
PyObject *d;
|
|
|
|
PyObject *args;
|
|
|
|
|
|
|
|
if (m == NULL ||
|
|
|
|
(d = PyModule_GetDict(m)) == NULL)
|
|
|
|
{
|
|
|
|
PyObject *f = PySys_GetObject("stderr");
|
|
|
|
if (Py_VerboseFlag) {
|
|
|
|
PyFile_WriteString(
|
|
|
|
"'import exceptions' failed; traceback:\n", f);
|
|
|
|
PyErr_Print();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyFile_WriteString(
|
|
|
|
"'import exceptions' failed; use -v for traceback\n", f);
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
PyFile_WriteString("defaulting to old style exceptions\n", f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
Py_FatalError("built-in exception cannot be initialized");
|
|
|
|
|
|
|
|
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 */
|
|
|
|
PyDict_SetItemString(dict, bltin_exc[i].name, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we need one pre-allocated instance */
|
|
|
|
args = Py_BuildValue("()");
|
|
|
|
if (args) {
|
|
|
|
PyExc_MemoryErrorInst =
|
|
|
|
PyEval_CallObject(PyExc_MemoryError, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we're done with the exceptions module */
|
|
|
|
Py_DECREF(m);
|
|
|
|
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
Py_FatalError("can't instantiate standard exceptions");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
fini_instances()
|
|
|
|
{
|
|
|
|
Py_XDECREF(PyExc_MemoryErrorInst);
|
|
|
|
PyExc_MemoryErrorInst = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1997-08-02 00:10:38 -03:00
|
|
|
newstdexception(dict, name)
|
|
|
|
PyObject *dict;
|
1991-12-16 11:42:38 -04:00
|
|
|
char *name;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v = PyString_FromString(name);
|
1997-08-02 00:10:38 -03:00
|
|
|
if (v == NULL || PyDict_SetItemString(dict, name, v) != 0)
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_FatalError("no mem for new standard exception");
|
1990-12-20 11:06:42 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1997-08-02 00:10:38 -03:00
|
|
|
initerrors(dict)
|
|
|
|
PyObject *dict;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-08-29 19:13:51 -03:00
|
|
|
int i;
|
|
|
|
int exccnt = 0;
|
|
|
|
for (i = 0; bltin_exc[i].name; i++, exccnt++) {
|
|
|
|
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 three 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);
|
|
|
|
|
1997-09-16 18:51:14 -03:00
|
|
|
PyExc_ArithmeticError = PyTuple_New(3);
|
1997-08-29 19:13:51 -03:00
|
|
|
Py_INCREF(PyExc_OverflowError);
|
1997-09-16 18:51:14 -03:00
|
|
|
PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError);
|
1997-08-29 19:13:51 -03:00
|
|
|
Py_INCREF(PyExc_ZeroDivisionError);
|
1997-09-16 18:51:14 -03:00
|
|
|
PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError);
|
1997-08-29 19:13:51 -03:00
|
|
|
Py_INCREF(PyExc_FloatingPointError);
|
1997-09-16 18:51:14 -03:00
|
|
|
PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError);
|
|
|
|
PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError);
|
1997-08-29 19:13:51 -03:00
|
|
|
|
1997-09-18 00:44:38 -03:00
|
|
|
PyExc_StandardError = PyTuple_New(exccnt-2);
|
|
|
|
for (i = 2; bltin_exc[i].name; i++) {
|
1997-08-29 19:13:51 -03:00
|
|
|
PyObject *exc = *bltin_exc[i].exc;
|
|
|
|
Py_INCREF(exc);
|
1997-09-18 00:44:38 -03:00
|
|
|
PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
|
1997-08-29 19:13:51 -03:00
|
|
|
}
|
|
|
|
PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
|
1997-09-16 15:43:15 -03:00
|
|
|
|
|
|
|
/* Exception is treated differently; for now, it's == StandardError */
|
|
|
|
PyExc_Exception = PyExc_StandardError;
|
|
|
|
Py_INCREF(PyExc_Exception);
|
|
|
|
PyDict_SetItemString(dict, "Exception", PyExc_Exception);
|
1997-08-29 19:13:51 -03:00
|
|
|
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
Py_FatalError("Could not initialize built-in string exceptions");
|
1997-08-02 00:10:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
finierrors()
|
|
|
|
{
|
1997-08-29 19:13:51 -03:00
|
|
|
int i;
|
|
|
|
for (i = 0; bltin_exc[i].name; i++) {
|
|
|
|
PyObject *exc = *bltin_exc[i].exc;
|
|
|
|
Py_XDECREF(exc);
|
|
|
|
*bltin_exc[i].exc = NULL;
|
|
|
|
}
|
1997-08-02 00:10:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
1997-08-29 19:13:51 -03:00
|
|
|
_PyBuiltin_Init_1()
|
1997-08-02 00:10:38 -03:00
|
|
|
{
|
|
|
|
PyObject *mod, *dict;
|
|
|
|
mod = Py_InitModule("__builtin__", builtin_methods);
|
|
|
|
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;
|
1997-08-29 19:13:51 -03:00
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
return mod;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-08-29 19:13:51 -03:00
|
|
|
_PyBuiltin_Init_2(dict)
|
|
|
|
PyObject *dict;
|
|
|
|
{
|
|
|
|
/* if Python was started with -X, initialize the class exceptions */
|
|
|
|
if (Py_UseClassExceptionsFlag)
|
|
|
|
init_class_exc(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyBuiltin_Fini_1()
|
|
|
|
{
|
|
|
|
fini_instances();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyBuiltin_Fini_2()
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-08-02 00:10:38 -03:00
|
|
|
finierrors();
|
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
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-10-26 14:58:25 -03:00
|
|
|
filtertuple(func, tuple)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *tuple;
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *result;
|
1993-10-26 14:58:25 -03:00
|
|
|
register int i, j;
|
1997-04-29 17:08:16 -03:00
|
|
|
int len = PyTuple_Size(tuple);
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1995-08-04 01:07:45 -03:00
|
|
|
if (len == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(tuple);
|
1995-08-04 01:07:45 -03:00
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((result = PyTuple_New(len)) == NULL)
|
1993-11-01 12:21:44 -04:00
|
|
|
return NULL;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
for (i = j = 0; i < len; ++i) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *item, *good;
|
1993-10-27 11:56:44 -03:00
|
|
|
int ok;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((item = PyTuple_GetItem(tuple, i)) == NULL)
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_1;
|
1997-04-29 17:08:16 -03:00
|
|
|
if (func == Py_None) {
|
|
|
|
Py_INCREF(item);
|
1993-10-27 11:56:44 -03:00
|
|
|
good = item;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *arg = Py_BuildValue("(O)", item);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (arg == NULL)
|
|
|
|
goto Fail_1;
|
1997-04-29 17:08:16 -03:00
|
|
|
good = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(arg);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (good == NULL)
|
|
|
|
goto Fail_1;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
ok = PyObject_IsTrue(good);
|
|
|
|
Py_DECREF(good);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (ok) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(item);
|
|
|
|
if (PyTuple_SetItem(result, j++, item) < 0)
|
1993-10-27 11:56:44 -03:00
|
|
|
goto Fail_1;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (_PyTuple_Resize(&result, j, 0) < 0)
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail_1:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_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
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
1993-10-26 14:58:25 -03:00
|
|
|
filterstring(func, strobj)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *strobj;
|
1993-10-26 14:58:25 -03:00
|
|
|
{
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *result;
|
1993-10-26 14:58:25 -03:00
|
|
|
register int i, j;
|
1997-04-29 17:08:16 -03:00
|
|
|
int len = PyString_Size(strobj);
|
1993-10-26 14:58:25 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (func == Py_None) {
|
1993-11-01 12:21:44 -04:00
|
|
|
/* No character is ever false -- share input string */
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(strobj);
|
1994-08-29 09:52:16 -03:00
|
|
|
return strobj;
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
|
1993-11-01 12:21:44 -04:00
|
|
|
return NULL;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
|
|
|
for (i = j = 0; i < len; ++i) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *item, *arg, *good;
|
1993-10-27 11:56:44 -03:00
|
|
|
int ok;
|
|
|
|
|
|
|
|
item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
|
|
|
|
if (item == NULL)
|
|
|
|
goto Fail_1;
|
1997-04-29 17:08:16 -03:00
|
|
|
arg = Py_BuildValue("(O)", item);
|
|
|
|
Py_DECREF(item);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (arg == NULL)
|
|
|
|
goto Fail_1;
|
1997-04-29 17:08:16 -03:00
|
|
|
good = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(arg);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (good == NULL)
|
|
|
|
goto Fail_1;
|
1997-04-29 17:08:16 -03:00
|
|
|
ok = PyObject_IsTrue(good);
|
|
|
|
Py_DECREF(good);
|
1993-10-27 11:56:44 -03:00
|
|
|
if (ok)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyString_AS_STRING((PyStringObject *)result)[j++] =
|
|
|
|
PyString_AS_STRING((PyStringObject *)item)[0];
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
if (j < len && _PyString_Resize(&result, j) < 0)
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Fail_1:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(result);
|
1993-10-26 14:58:25 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-31 13:15:43 -04:00
|
|
|
|
|
|
|
/* Copied with modifications from stropmodule.c: atoi,atof.atol */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
int_from_string(v)
|
|
|
|
PyObject *v;
|
|
|
|
{
|
|
|
|
extern long PyOS_strtol Py_PROTO((const char *, char **, int));
|
|
|
|
char *s, *end;
|
|
|
|
long x;
|
|
|
|
char buffer[256]; /* For errors */
|
|
|
|
|
1998-03-13 17:30:14 -04:00
|
|
|
s = PyString_AS_STRING(v);
|
1997-03-31 13:15:43 -04:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
|
|
|
if (s[0] == '\0') {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "empty string for int()");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
x = PyOS_strtol(s, &end, 10);
|
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
|
|
|
if (*end != '\0') {
|
|
|
|
sprintf(buffer, "invalid literal for int(): %.200s", s);
|
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-03-13 17:30:14 -04:00
|
|
|
else if (end-s != PyString_GET_SIZE(v)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"null byte in argument for int()");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-31 13:15:43 -04:00
|
|
|
else if (errno != 0) {
|
|
|
|
sprintf(buffer, "int() literal too large: %.200s", s);
|
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyInt_FromLong(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
long_from_string(v)
|
|
|
|
PyObject *v;
|
|
|
|
{
|
|
|
|
char *s, *end;
|
|
|
|
PyObject *x;
|
|
|
|
char buffer[256]; /* For errors */
|
|
|
|
|
1998-03-13 17:30:14 -04:00
|
|
|
s = PyString_AS_STRING(v);
|
1997-03-31 13:15:43 -04:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
|
|
|
if (s[0] == '\0') {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "empty string for long()");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
x = PyLong_FromString(s, &end, 10);
|
|
|
|
if (x == NULL)
|
|
|
|
return NULL;
|
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
|
|
|
if (*end != '\0') {
|
|
|
|
sprintf(buffer, "invalid literal for long(): %.200s", s);
|
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
Py_DECREF(x);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-03-13 17:30:14 -04:00
|
|
|
else if (end-s != PyString_GET_SIZE(v)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"null byte in argument for float()");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-31 13:15:43 -04:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
float_from_string(v)
|
|
|
|
PyObject *v;
|
|
|
|
{
|
|
|
|
extern double strtod Py_PROTO((const char *, char **));
|
|
|
|
char *s, *end;
|
|
|
|
double x;
|
|
|
|
char buffer[256]; /* For errors */
|
|
|
|
|
1998-03-13 17:30:14 -04:00
|
|
|
s = PyString_AS_STRING(v);
|
1997-03-31 13:15:43 -04:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
|
|
|
if (s[0] == '\0') {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "empty string for float()");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
PyFPE_START_PROTECT("float_from_string", return 0)
|
|
|
|
x = strtod(s, &end);
|
|
|
|
PyFPE_END_PROTECT(x)
|
|
|
|
while (*end && isspace(Py_CHARMASK(*end)))
|
|
|
|
end++;
|
|
|
|
if (*end != '\0') {
|
|
|
|
sprintf(buffer, "invalid literal for float(): %.200s", s);
|
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-03-13 17:30:14 -04:00
|
|
|
else if (end-s != PyString_GET_SIZE(v)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"null byte in argument for float()");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-31 13:15:43 -04:00
|
|
|
else if (errno != 0) {
|
|
|
|
sprintf(buffer, "float() literal too large: %.200s", s);
|
|
|
|
PyErr_SetString(PyExc_ValueError, buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyFloat_FromDouble(x);
|
|
|
|
}
|