1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
2000-06-30 20:50:40 -03:00
|
|
|
Copyright (c) 2000, BeOpen.com.
|
|
|
|
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
|
|
|
|
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
See the file "Misc/COPYRIGHT" for information on usage and
|
|
|
|
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
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
|
2000-07-03 18:39:47 -03:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
1996-12-10 11:37:36 -04:00
|
|
|
|
1993-10-26 14:58:25 -03:00
|
|
|
/* Forward */
|
2000-07-09 00:09:57 -03:00
|
|
|
static PyObject *filterstring(PyObject *, PyObject *);
|
|
|
|
static PyObject *filtertuple (PyObject *, 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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char import_doc[] =
|
|
|
|
"__import__(name, globals, locals, fromlist) -> module\n\
|
|
|
|
\n\
|
|
|
|
Import a module. The globals are only used to determine the context;\n\
|
|
|
|
they are not modified. The locals are currently unused. The fromlist\n\
|
|
|
|
should be a list of names to emulate ``from name import ...'', or an\n\
|
|
|
|
empty list to emulate ``import name''.\n\
|
|
|
|
When importing a module from a package, note that __import__('A.B', ...)\n\
|
|
|
|
returns package A when fromlist is empty, but its submodule B when\n\
|
|
|
|
fromlist is not empty.";
|
|
|
|
|
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;
|
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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
return PyNumber_Absolute(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char abs_doc[] =
|
|
|
|
"abs(number) -> number\n\
|
|
|
|
\n\
|
|
|
|
Return the absolute value of the argument.";
|
|
|
|
|
|
|
|
|
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;
|
1998-10-01 12:33:12 -03:00
|
|
|
PyObject *t = NULL, *retval = 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;
|
1998-10-01 12:33:12 -03:00
|
|
|
if (alist != NULL) {
|
|
|
|
if (!PyTuple_Check(alist)) {
|
|
|
|
if (!PySequence_Check(alist)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"apply() 2nd argument must be a sequence");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
t = PySequence_Tuple(alist);
|
|
|
|
if (t == NULL)
|
|
|
|
return NULL;
|
|
|
|
alist = t;
|
|
|
|
}
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
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");
|
1998-10-01 12:33:12 -03:00
|
|
|
goto finally;
|
1995-07-18 11:51:37 -03:00
|
|
|
}
|
1998-10-01 12:33:12 -03:00
|
|
|
retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
|
|
|
|
finally:
|
|
|
|
Py_XDECREF(t);
|
|
|
|
return retval;
|
1991-12-16 09:03:00 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char apply_doc[] =
|
1999-12-23 10:16:55 -04:00
|
|
|
"apply(object, args[, kwargs]) -> value\n\
|
1998-06-26 18:23:49 -03:00
|
|
|
\n\
|
1999-12-23 10:16:55 -04:00
|
|
|
Call a callable object with positional arguments taken from the tuple args,\n\
|
|
|
|
and keyword arguments taken from the optional dictionary kwargs.\n\
|
|
|
|
Note that classes are callable, as are instances with a __call__() method.";
|
1998-06-26 18:23:49 -03:00
|
|
|
|
|
|
|
|
1999-03-19 15:07:19 -04:00
|
|
|
static PyObject *
|
|
|
|
builtin_buffer(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyObject *ob;
|
|
|
|
int offset = 0;
|
|
|
|
int size = Py_END_OF_BUFFER;
|
|
|
|
|
|
|
|
if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
|
|
|
|
return NULL;
|
|
|
|
return PyBuffer_FromObject(ob, offset, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char buffer_doc[] =
|
2000-03-10 19:00:52 -04:00
|
|
|
"buffer(object [, offset[, size]]) -> object\n\
|
1999-03-19 15:07:19 -04:00
|
|
|
\n\
|
|
|
|
Creates a new buffer object which references the given object.\n\
|
|
|
|
The buffer will reference a slice of the target object from the\n\
|
|
|
|
start of the object (or at the specified offset). The slice will\n\
|
|
|
|
extend to the end of the target object (or with the specified size).";
|
|
|
|
|
|
|
|
|
2000-03-10 19:00:52 -04:00
|
|
|
static PyObject *
|
|
|
|
builtin_unicode(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
2000-04-11 12:38:23 -03:00
|
|
|
PyObject *v;
|
2000-03-10 19:00:52 -04:00
|
|
|
char *encoding = NULL;
|
|
|
|
char *errors = NULL;
|
|
|
|
|
2000-04-11 12:38:23 -03:00
|
|
|
if ( !PyArg_ParseTuple(args, "O|ss:unicode", &v, &encoding, &errors) )
|
2000-03-10 19:00:52 -04:00
|
|
|
return NULL;
|
2000-07-07 10:48:25 -03:00
|
|
|
return PyUnicode_FromEncodedObject(v, encoding, errors);
|
2000-03-10 19:00:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char unicode_doc[] =
|
|
|
|
"unicode(string [, encoding[, errors]]) -> object\n\
|
|
|
|
\n\
|
2000-04-12 23:42:50 -03:00
|
|
|
Creates a new Unicode object from the given encoded string.\n\
|
2000-05-09 16:55:16 -03:00
|
|
|
encoding defaults to the current default string encoding and \n\
|
|
|
|
errors, defining the error handling, to 'strict'.";
|
2000-03-10 19:00:52 -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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char callable_doc[] =
|
|
|
|
"callable(object) -> Boolean\n\
|
|
|
|
\n\
|
|
|
|
Return whether the object is callable (i.e., some kind of function).\n\
|
|
|
|
Note that classes are callable, as are instances with a __call__() method.";
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
sqf = seq->ob_type->tp_as_sequence;
|
|
|
|
if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == 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) {
|
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 {
|
1999-01-28 14:49:12 -04:00
|
|
|
int status = PyList_Append(result, item);
|
1994-08-29 09:52:16 -03:00
|
|
|
j++;
|
1999-01-28 14:49:12 -04:00
|
|
|
Py_DECREF(item);
|
|
|
|
if (status < 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;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char filter_doc[] =
|
|
|
|
"filter(function, sequence) -> list\n\
|
|
|
|
\n\
|
|
|
|
Return a list containing those items of sequence for which function(item)\n\
|
|
|
|
is true. If function is None, return a list of items that are true.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char chr_doc[] =
|
|
|
|
"chr(i) -> character\n\
|
|
|
|
\n\
|
|
|
|
Return a string of one character with ordinal i; 0 <= i < 256.";
|
|
|
|
|
|
|
|
|
2000-03-10 19:00:52 -04:00
|
|
|
static PyObject *
|
|
|
|
builtin_unichr(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
long x;
|
|
|
|
Py_UNICODE s[1];
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "l:unichr", &x))
|
|
|
|
return NULL;
|
|
|
|
if (x < 0 || x >= 65536) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"unichr() arg not in range(65536)");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s[0] = (Py_UNICODE)x;
|
|
|
|
return PyUnicode_FromUnicode(s, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char unichr_doc[] =
|
2000-04-12 23:42:50 -03:00
|
|
|
"unichr(i) -> Unicode character\n\
|
2000-03-10 19:00:52 -04:00
|
|
|
\n\
|
2000-04-12 23:42:50 -03:00
|
|
|
Return a Unicode string of one character with ordinal i; 0 <= i < 65536.";
|
2000-03-10 19:00:52 -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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
int 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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
if (PyObject_Cmp(a, b, &c) < 0)
|
1997-05-22 21:06:51 -03:00
|
|
|
return NULL;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
return PyInt_FromLong((long)c);
|
1992-10-18 15:53:57 -03:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char cmp_doc[] =
|
|
|
|
"cmp(x, y) -> integer\n\
|
|
|
|
\n\
|
|
|
|
Return negative if x<y, zero if x==y, positive if x>y.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char coerce_doc[] =
|
|
|
|
"coerce(x, y) -> None or (x1, y1)\n\
|
|
|
|
\n\
|
|
|
|
When x and y can be coerced to values of the same type, return a tuple\n\
|
|
|
|
containing the coerced values. When they can't be coerced, return None.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char compile_doc[] =
|
|
|
|
"compile(source, filename, mode) -> code object\n\
|
|
|
|
\n\
|
|
|
|
Compile the source string (a Python module, statement or expression)\n\
|
|
|
|
into a code object that can be executed by the exec statement or eval().\n\
|
|
|
|
The filename will be used for run-time error messages.\n\
|
|
|
|
The mode must be 'exec' to compile a module, 'single' to compile a\n\
|
|
|
|
single (interactive) statement, or 'eval' to compile an expression.";
|
|
|
|
|
|
|
|
|
1996-01-11 21:09:56 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
|
|
|
|
1999-03-25 17:16:07 -04:00
|
|
|
static PyObject *
|
|
|
|
complex_from_string(v)
|
|
|
|
PyObject *v;
|
|
|
|
{
|
2000-07-09 00:09:57 -03:00
|
|
|
extern double strtod(const char *, char **);
|
2000-04-05 17:11:21 -03:00
|
|
|
const char *s, *start;
|
|
|
|
char *end;
|
1999-03-25 17:16:07 -04:00
|
|
|
double x=0.0, y=0.0, z;
|
|
|
|
int got_re=0, got_im=0, done=0;
|
|
|
|
int digit_or_dot;
|
|
|
|
int sw_error=0;
|
|
|
|
int sign;
|
|
|
|
char buffer[256]; /* For errors */
|
2000-04-05 17:11:21 -03:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if (PyString_Check(v)) {
|
|
|
|
s = PyString_AS_STRING(v);
|
|
|
|
len = PyString_GET_SIZE(v);
|
|
|
|
}
|
|
|
|
else if (PyUnicode_Check(v)) {
|
|
|
|
char s_buffer[256];
|
1999-03-25 17:16:07 -04:00
|
|
|
|
2000-04-05 17:11:21 -03:00
|
|
|
if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"complex() literal too large to convert");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
|
|
|
|
PyUnicode_GET_SIZE(v),
|
|
|
|
s_buffer,
|
|
|
|
NULL))
|
|
|
|
return NULL;
|
|
|
|
s = s_buffer;
|
|
|
|
len = strlen(s);
|
|
|
|
}
|
|
|
|
else if (PyObject_AsCharBuffer(v, &s, &len)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"complex() needs a string first argument");
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-03-25 17:16:07 -04:00
|
|
|
|
|
|
|
/* position on first nonblank */
|
2000-04-05 17:11:21 -03:00
|
|
|
start = s;
|
1999-03-25 17:16:07 -04:00
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
|
|
|
if (s[0] == '\0') {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"empty string for complex()");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
z = -1.0;
|
|
|
|
sign = 1;
|
|
|
|
do {
|
|
|
|
|
|
|
|
switch (*s) {
|
|
|
|
|
|
|
|
case '\0':
|
2000-04-05 17:11:21 -03:00
|
|
|
if (s-start != len) {
|
1999-03-25 17:16:07 -04:00
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_ValueError,
|
|
|
|
"null byte in argument for complex()");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(!done) sw_error=1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
sign = -1;
|
|
|
|
/* Fallthrough */
|
|
|
|
case '+':
|
|
|
|
if (done) sw_error=1;
|
|
|
|
s++;
|
|
|
|
if ( *s=='\0'||*s=='+'||*s=='-' ||
|
|
|
|
isspace(Py_CHARMASK(*s)) ) sw_error=1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'J':
|
|
|
|
case 'j':
|
|
|
|
if (got_im || done) {
|
|
|
|
sw_error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (z<0.0) {
|
|
|
|
y=sign;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
y=sign*z;
|
|
|
|
}
|
|
|
|
got_im=1;
|
|
|
|
s++;
|
|
|
|
if (*s!='+' && *s!='-' )
|
|
|
|
done=1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (isspace(Py_CHARMASK(*s))) {
|
|
|
|
while (*s && isspace(Py_CHARMASK(*s)))
|
|
|
|
s++;
|
|
|
|
if (s[0] != '\0')
|
|
|
|
sw_error=1;
|
|
|
|
else
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
digit_or_dot =
|
|
|
|
(*s=='.' || isdigit(Py_CHARMASK(*s)));
|
|
|
|
if (done||!digit_or_dot) {
|
|
|
|
sw_error=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
PyFPE_START_PROTECT("strtod", return 0)
|
|
|
|
z = strtod(s, &end) ;
|
|
|
|
PyFPE_END_PROTECT(z)
|
|
|
|
if (errno != 0) {
|
|
|
|
sprintf(buffer,
|
|
|
|
"float() out of range: %.150s", s);
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_ValueError,
|
|
|
|
buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s=end;
|
|
|
|
if (*s=='J' || *s=='j') {
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (got_re) {
|
|
|
|
sw_error=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* accept a real part */
|
|
|
|
x=sign*z;
|
|
|
|
got_re=1;
|
|
|
|
if (got_im) done=1;
|
|
|
|
z = -1.0;
|
|
|
|
sign = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
} /* end of switch */
|
|
|
|
|
|
|
|
} while (*s!='\0' && !sw_error);
|
|
|
|
|
|
|
|
if (sw_error) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"malformed string for complex()");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyComplex_FromDoubles(x,y);
|
|
|
|
}
|
|
|
|
|
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;
|
2000-04-05 17:11:21 -03:00
|
|
|
if (PyString_Check(r) || PyUnicode_Check(r))
|
1999-03-25 17:16:07 -04:00
|
|
|
return complex_from_string(r);
|
1996-01-11 21:09:56 -04:00
|
|
|
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);
|
1999-01-27 19:13:59 -04:00
|
|
|
Py_DECREF(f);
|
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;
|
1998-04-10 19:27:42 -03:00
|
|
|
if (own_r) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(r);
|
1998-04-10 19:27:42 -03:00
|
|
|
}
|
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);
|
1998-04-10 19:27:42 -03:00
|
|
|
if (own_r) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(r);
|
1998-04-10 19:27:42 -03:00
|
|
|
}
|
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);
|
1999-03-25 17:16:07 -04:00
|
|
|
cr.imag = 0.0;
|
1996-01-11 21:09:56 -04:00
|
|
|
}
|
|
|
|
if (i == NULL) {
|
1999-03-25 17:16:07 -04:00
|
|
|
ci.real = 0.0;
|
|
|
|
ci.imag = 0.0;
|
1996-01-11 21:09:56 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char complex_doc[] =
|
|
|
|
"complex(real[, imag]) -> complex number\n\
|
|
|
|
\n\
|
|
|
|
Create a complex number from a real part and an optional imaginary part.\n\
|
|
|
|
This is equivalent to (real + imag*1j) where imag defaults to 0.";
|
|
|
|
|
|
|
|
|
1996-01-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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char dir_doc[] =
|
|
|
|
"dir([object]) -> list of strings\n\
|
|
|
|
\n\
|
|
|
|
Return an alphabetized list of names comprising (some of) the attributes\n\
|
|
|
|
of the given object. Without an argument, the names in the current scope\n\
|
|
|
|
are listed. With an instance argument, only the instance attributes are\n\
|
|
|
|
returned. With a class argument, attributes of the base class are not\n\
|
|
|
|
returned. For other types or arguments, this may list members or methods.";
|
|
|
|
|
|
|
|
|
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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
return PyNumber_Divmod(v, w);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char divmod_doc[] =
|
|
|
|
"divmod(x, y) -> (div, mod)\n\
|
|
|
|
\n\
|
|
|
|
Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
|
|
|
|
|
|
|
|
|
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);
|
2000-06-28 18:12:25 -03:00
|
|
|
if (strlen(str) != (size_t)PyString_Size(cmd)) {
|
1997-04-29 17:08:16 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char eval_doc[] =
|
|
|
|
"eval(source[, globals[, locals]]) -> value\n\
|
|
|
|
\n\
|
|
|
|
Evaluate the source in the context of globals and locals.\n\
|
|
|
|
The source may be a string representing a Python expression\n\
|
|
|
|
or a code object as returned by compile().\n\
|
|
|
|
The globals and locals are dictionaries, defaulting to the current\n\
|
|
|
|
globals and locals. If only globals is given, locals defaults to it.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char execfile_doc[] =
|
|
|
|
"execfile(filename[, globals[, locals]])\n\
|
|
|
|
\n\
|
|
|
|
Read and execute a Python script from a file.\n\
|
|
|
|
The globals and locals are dictionaries, defaulting to the current\n\
|
|
|
|
globals and locals. If only globals is given, locals defaults to it.";
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
1998-06-29 10:38:57 -03:00
|
|
|
PyObject *v, *result, *dflt = NULL;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *name;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1998-06-29 10:38:57 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OS|O:getattr", &v, &name, &dflt))
|
1992-01-27 12:53:09 -04:00
|
|
|
return NULL;
|
1998-06-29 10:38:57 -03:00
|
|
|
result = PyObject_GetAttr(v, name);
|
|
|
|
if (result == NULL && dflt != NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_INCREF(dflt);
|
|
|
|
result = dflt;
|
|
|
|
}
|
|
|
|
return result;
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char getattr_doc[] =
|
1998-06-29 10:38:57 -03:00
|
|
|
"getattr(object, name[, default]) -> value\n\
|
1998-06-26 18:23:49 -03:00
|
|
|
\n\
|
1998-06-29 10:38:57 -03:00
|
|
|
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
|
|
|
|
When a default argument is given, it is returned when the attribute doesn't\n\
|
|
|
|
exist; without it, an exception is raised in that case.";
|
1998-06-26 18:23:49 -03: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
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, ":globals"))
|
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;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char globals_doc[] =
|
|
|
|
"globals() -> dictionary\n\
|
|
|
|
\n\
|
|
|
|
Return the dictionary containing the current scope's global variables.";
|
|
|
|
|
|
|
|
|
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();
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
Py_INCREF(Py_False);
|
|
|
|
return Py_False;
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_DECREF(v);
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
Py_INCREF(Py_True);
|
|
|
|
return Py_True;
|
1992-01-27 12:53:09 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char hasattr_doc[] =
|
|
|
|
"hasattr(object, name) -> Boolean\n\
|
|
|
|
\n\
|
|
|
|
Return whether the object has an attribute with the given name.\n\
|
|
|
|
(This is done by calling getattr(object, name) and catching exceptions.)";
|
|
|
|
|
|
|
|
|
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;
|
2000-06-28 18:12:25 -03:00
|
|
|
return PyLong_FromVoidPtr(v);
|
1993-03-30 13:46:03 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char id_doc[] =
|
|
|
|
"id(object) -> integer\n\
|
|
|
|
\n\
|
|
|
|
Return the identity of an object. This is guaranteed to be unique among\n\
|
|
|
|
simultaneously existing objects. (Hint: it's the object's memory address.)";
|
|
|
|
|
|
|
|
|
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
|
|
|
|
1998-07-10 14:37:30 -03:00
|
|
|
if (func == Py_None && n == 1) {
|
|
|
|
/* map(None, S) is the same as list(S). */
|
|
|
|
return PySequence_List(PyTuple_GetItem(args, 1));
|
|
|
|
}
|
|
|
|
|
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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
PySequenceMethods *sqf;
|
1993-10-26 14:58:25 -03:00
|
|
|
|
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;
|
|
|
|
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
|
|
|
|
if (sqf == NULL ||
|
|
|
|
sqf->sq_length == NULL ||
|
|
|
|
sqf->sq_item == NULL)
|
|
|
|
{
|
1993-10-26 14:58:25 -03:00
|
|
|
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;
|
|
|
|
|
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) {
|
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) {
|
1999-01-28 14:49:12 -04:00
|
|
|
int status = PyList_Append(result, value);
|
1999-01-28 00:21:35 -04:00
|
|
|
Py_DECREF(value);
|
1999-01-28 14:49:12 -04:00
|
|
|
if (status < 0)
|
|
|
|
goto Fail_1;
|
1994-08-29 09:52:16 -03:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-07-10 14:37:30 -03:00
|
|
|
if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
|
|
|
|
goto Fail_1;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char map_doc[] =
|
|
|
|
"map(function, sequence[, sequence, ...]) -> list\n\
|
|
|
|
\n\
|
|
|
|
Return a list of the results of applying the function to the items of\n\
|
|
|
|
the argument sequence(s). If more than one sequence is given, the\n\
|
|
|
|
function is called with an argument list consisting of the corresponding\n\
|
|
|
|
item of each sequence, substituting None for missing values when not all\n\
|
|
|
|
sequences have the same length. If the function is None, return a list of\n\
|
|
|
|
the items of the sequence (or a list of tuples if more than one sequence).";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char setattr_doc[] =
|
|
|
|
"setattr(object, name, value)\n\
|
|
|
|
\n\
|
|
|
|
Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
|
|
|
|
``x.y = v''.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char delattr_doc[] =
|
1998-11-23 18:13:04 -04:00
|
|
|
"delattr(object, name)\n\
|
1998-06-26 18:23:49 -03:00
|
|
|
\n\
|
|
|
|
Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
|
|
|
|
``del x.y''.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char hash_doc[] =
|
|
|
|
"hash(object) -> integer\n\
|
|
|
|
\n\
|
|
|
|
Return a hash value for the object. Two objects with the same value have\n\
|
|
|
|
the same hash value. The reverse is not necessarily true, but likely.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char hex_doc[] =
|
|
|
|
"hex(number) -> string\n\
|
|
|
|
\n\
|
|
|
|
Return the hexadecimal representation of an integer or long integer.";
|
|
|
|
|
|
|
|
|
2000-07-09 00:09:57 -03:00
|
|
|
static PyObject *builtin_raw_input(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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char input_doc[] =
|
|
|
|
"input([prompt]) -> value\n\
|
|
|
|
\n\
|
|
|
|
Equivalent to eval(raw_input(prompt)).";
|
|
|
|
|
|
|
|
|
1997-02-14 11:48:05 -04:00
|
|
|
static PyObject *
|
|
|
|
builtin_intern(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyObject *s;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "S:intern", &s))
|
1997-02-14 11:48:05 -04:00
|
|
|
return NULL;
|
|
|
|
Py_INCREF(s);
|
|
|
|
PyString_InternInPlace(&s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char intern_doc[] =
|
|
|
|
"intern(string) -> string\n\
|
|
|
|
\n\
|
|
|
|
``Intern'' the given string. This enters the string in the (global)\n\
|
|
|
|
table of interned strings whose purpose is to speed up dictionary lookups.\n\
|
|
|
|
Return the string itself or the previously interned string object with the\n\
|
|
|
|
same value.";
|
|
|
|
|
|
|
|
|
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;
|
1999-10-12 16:54:53 -03:00
|
|
|
int base = -909; /* unlikely! */
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1999-10-12 16:54:53 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
|
1995-01-02 15:04:15 -04:00
|
|
|
return NULL;
|
1999-10-12 16:54:53 -03:00
|
|
|
if (base == -909)
|
|
|
|
return PyNumber_Int(v);
|
2000-04-05 17:11:21 -03:00
|
|
|
else if (PyString_Check(v))
|
|
|
|
return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
|
|
|
|
else if (PyUnicode_Check(v))
|
|
|
|
return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
|
|
|
|
PyUnicode_GET_SIZE(v),
|
|
|
|
base);
|
|
|
|
else {
|
1999-10-12 16:54:53 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"can't convert non-string with explicit base");
|
|
|
|
return NULL;
|
|
|
|
}
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char int_doc[] =
|
1999-10-12 16:54:53 -03:00
|
|
|
"int(x[, base]) -> integer\n\
|
|
|
|
\n\
|
|
|
|
Convert a string or number to an integer, if possible. A floating point\n\
|
|
|
|
argument will be truncated towards zero (this does not include a string\n\
|
|
|
|
representation of a floating point number!) When converting a string, use\n\
|
|
|
|
the optional base. It is an error to supply a base when converting a\n\
|
|
|
|
non-string.";
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
builtin_long(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyObject *v;
|
|
|
|
int base = -909; /* unlikely! */
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
|
|
|
|
return NULL;
|
|
|
|
if (base == -909)
|
|
|
|
return PyNumber_Long(v);
|
2000-04-05 17:11:21 -03:00
|
|
|
else if (PyString_Check(v))
|
|
|
|
return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
|
|
|
|
else if (PyUnicode_Check(v))
|
|
|
|
return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
|
|
|
|
PyUnicode_GET_SIZE(v),
|
|
|
|
base);
|
|
|
|
else {
|
1999-10-12 16:54:53 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"can't convert non-string with explicit base");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char long_doc[] =
|
|
|
|
"long(x) -> long integer\n\
|
|
|
|
long(x, base) -> long integer\n\
|
|
|
|
\n\
|
|
|
|
Convert a string or number to a long integer, if possible. A floating\n\
|
|
|
|
point argument will be truncated towards zero (this does not include a\n\
|
|
|
|
string representation of a floating point number!) When converting a\n\
|
|
|
|
string, use the given base. It is an error to supply a base when\n\
|
|
|
|
converting a non-string.";
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
builtin_float(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O:float", &v))
|
|
|
|
return NULL;
|
|
|
|
if (PyString_Check(v))
|
|
|
|
return PyFloat_FromString(v, NULL);
|
|
|
|
return PyNumber_Float(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char float_doc[] =
|
|
|
|
"float(x) -> floating point number\n\
|
1998-06-26 18:23:49 -03:00
|
|
|
\n\
|
1999-10-12 16:54:53 -03:00
|
|
|
Convert a string or number to a floating point number, if possible.";
|
1998-06-26 18:23:49 -03: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;
|
1998-06-29 19:26:50 -03:00
|
|
|
long res;
|
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;
|
1998-06-29 19:26:50 -03:00
|
|
|
res = PyObject_Length(v);
|
|
|
|
if (res < 0 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
return PyInt_FromLong(res);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char len_doc[] =
|
|
|
|
"len(object) -> integer\n\
|
|
|
|
\n\
|
|
|
|
Return the number of items of a sequence or mapping.";
|
|
|
|
|
|
|
|
|
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;
|
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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
return PySequence_List(v);
|
1996-04-08 23:41:06 -03:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char list_doc[] =
|
|
|
|
"list(sequence) -> list\n\
|
|
|
|
\n\
|
|
|
|
Return a new list whose items are the same as those of the argument sequence.";
|
|
|
|
|
1996-07-30 13:49:37 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
builtin_slice(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
PyObject *start, *stop, *step;
|
1996-07-30 13:49:37 -03:00
|
|
|
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
start = stop = step = NULL;
|
1996-07-30 13:49:37 -03:00
|
|
|
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
|
|
|
|
return NULL;
|
1996-07-30 13:49:37 -03:00
|
|
|
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
/* This swapping of stop and start is to maintain similarity with
|
|
|
|
range(). */
|
|
|
|
if (stop == NULL) {
|
|
|
|
stop = start;
|
|
|
|
start = NULL;
|
|
|
|
}
|
|
|
|
return PySlice_New(start, stop, step);
|
1996-07-30 13:49:37 -03:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char slice_doc[] =
|
1999-07-19 12:21:16 -03:00
|
|
|
"slice([start,] stop[, step]) -> slice object\n\
|
1998-06-26 18:23:49 -03:00
|
|
|
\n\
|
|
|
|
Create a slice object. This is used for slicing by the Numeric extensions.";
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, ":locals"))
|
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;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char locals_doc[] =
|
|
|
|
"locals() -> dictionary\n\
|
|
|
|
\n\
|
|
|
|
Return the dictionary containing the current scope's local variables.";
|
|
|
|
|
|
|
|
|
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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
if (sq == NULL || sq->sq_item == 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);
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char min_doc[] =
|
|
|
|
"min(sequence) -> value\n\
|
|
|
|
min(a, b, c, ...) -> value\n\
|
|
|
|
\n\
|
|
|
|
With a single sequence argument, return its smallest item.\n\
|
|
|
|
With two or more arguments, return the smallest argument.";
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char max_doc[] =
|
|
|
|
"max(sequence) -> value\n\
|
|
|
|
max(a, b, c, ...) -> value\n\
|
|
|
|
\n\
|
|
|
|
With a single sequence argument, return its largest item.\n\
|
|
|
|
With two or more arguments, return the largest argument.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char oct_doc[] =
|
|
|
|
"oct(number) -> string\n\
|
|
|
|
\n\
|
|
|
|
Return the octal representation of an integer or long integer.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char open_doc[] =
|
|
|
|
"open(filename[, mode[, buffering]]) -> file object\n\
|
|
|
|
\n\
|
|
|
|
Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
|
|
|
|
writing or appending. The file will be created if it doesn't exist\n\
|
|
|
|
when opened for writing or appending; it will be truncated when\n\
|
|
|
|
opened for writing. Add a 'b' to the mode for binary files.\n\
|
|
|
|
Add a '+' to the mode to allow simultaneous reading and writing.\n\
|
|
|
|
If the buffering argument is given, 0 means unbuffered, 1 means line\n\
|
|
|
|
buffered, and larger numbers specify the buffer size.";
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2000-03-10 19:00:52 -04:00
|
|
|
PyObject *obj;
|
|
|
|
long ord;
|
2000-04-12 18:19:47 -03:00
|
|
|
int size;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
2000-03-10 19:00:52 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "O:ord", &obj))
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
2000-03-10 19:00:52 -04:00
|
|
|
|
2000-04-12 18:19:47 -03:00
|
|
|
if (PyString_Check(obj)) {
|
|
|
|
size = PyString_GET_SIZE(obj);
|
|
|
|
if (size == 1)
|
|
|
|
ord = (long)((unsigned char)*PyString_AS_STRING(obj));
|
|
|
|
} else if (PyUnicode_Check(obj)) {
|
|
|
|
size = PyUnicode_GET_SIZE(obj);
|
|
|
|
if (size == 1)
|
|
|
|
ord = (long)*PyUnicode_AS_UNICODE(obj);
|
|
|
|
} else {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2000-04-12 23:42:50 -03:00
|
|
|
"expected string or Unicode character, " \
|
2000-04-12 18:19:47 -03:00
|
|
|
"%.200s found", obj->ob_type->tp_name);
|
2000-03-10 19:00:52 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-04-12 18:19:47 -03:00
|
|
|
if (size == 1)
|
|
|
|
return PyInt_FromLong(ord);
|
2000-03-10 19:00:52 -04:00
|
|
|
|
2000-04-12 18:19:47 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"expected a character, length-%d string found",
|
|
|
|
size);
|
|
|
|
return NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char ord_doc[] =
|
|
|
|
"ord(c) -> integer\n\
|
|
|
|
\n\
|
2000-04-12 23:42:50 -03:00
|
|
|
Return the integer ordinal of a one character string.";
|
1998-06-26 18:23:49 -03:00
|
|
|
|
|
|
|
|
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
|
|
|
{
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
PyObject *v, *w, *z = Py_None;
|
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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
return PyNumber_Power(v, w, z);
|
1991-05-05 17:00:36 -03:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char pow_doc[] =
|
|
|
|
"pow(x, y[, z]) -> number\n\
|
|
|
|
\n\
|
|
|
|
With two arguments, equivalent to x**y. With three arguments,\n\
|
|
|
|
equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
|
|
|
|
|
|
|
|
|
1999-02-23 12:11:01 -04:00
|
|
|
/* Return number of items in range/xrange (lo, hi, step). step > 0
|
|
|
|
* required. Return a value < 0 if & only if the true value is too
|
|
|
|
* large to fit in a signed long.
|
|
|
|
*/
|
|
|
|
static long
|
|
|
|
get_len_of_range(lo, hi, step)
|
|
|
|
long lo;
|
|
|
|
long hi;
|
|
|
|
long step; /* must be > 0 */
|
|
|
|
{
|
|
|
|
/* -------------------------------------------------------------
|
|
|
|
If lo >= hi, the range is empty.
|
|
|
|
Else if n values are in the range, the last one is
|
|
|
|
lo + (n-1)*step, which must be <= hi-1. Rearranging,
|
|
|
|
n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
|
|
|
|
the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
|
|
|
|
the RHS is non-negative and so truncation is the same as the
|
|
|
|
floor. Letting M be the largest positive long, the worst case
|
|
|
|
for the RHS numerator is hi=M, lo=-M-1, and then
|
|
|
|
hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
|
|
|
|
precision to compute the RHS exactly.
|
|
|
|
---------------------------------------------------------------*/
|
|
|
|
long n = 0;
|
|
|
|
if (lo < hi) {
|
|
|
|
unsigned long uhi = (unsigned long)hi;
|
|
|
|
unsigned long ulo = (unsigned long)lo;
|
|
|
|
unsigned long diff = uhi - ulo - 1;
|
|
|
|
n = (long)(diff / (unsigned long)step + 1);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
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;
|
1999-02-23 12:11:01 -04:00
|
|
|
long bign;
|
1990-12-20 11:06:42 -04:00
|
|
|
int i, n;
|
1999-02-23 12:11:01 -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 (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;
|
|
|
|
}
|
1999-02-23 12:11:01 -04:00
|
|
|
if (istep > 0)
|
|
|
|
bign = get_len_of_range(ilow, ihigh, istep);
|
|
|
|
else
|
|
|
|
bign = get_len_of_range(ihigh, ilow, -istep);
|
|
|
|
n = (int)bign;
|
|
|
|
if (bign < 0 || (long)n != bign) {
|
1999-01-12 01:07:47 -04:00
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
1999-02-23 12:11:01 -04:00
|
|
|
"range() has too many items");
|
1999-01-12 01:07:47 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
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;
|
|
|
|
}
|
1998-04-24 15:22:02 -03:00
|
|
|
PyList_SET_ITEM(v, i, w);
|
1990-12-20 11:06:42 -04:00
|
|
|
ilow += istep;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char range_doc[] =
|
|
|
|
"range([start,] stop[, step]) -> list of integers\n\
|
|
|
|
\n\
|
|
|
|
Return a list containing an arithmetic progression of integers.\n\
|
|
|
|
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
|
|
|
|
When step is given, it specifies the increment (or decrement).\n\
|
|
|
|
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
|
|
|
|
These are exactly the valid indices for a list of 4 elements.";
|
|
|
|
|
|
|
|
|
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
|
|
|
if (istep > 0)
|
1999-02-23 12:11:01 -04:00
|
|
|
n = get_len_of_range(ilow, ihigh, istep);
|
1995-01-02 15:04:15 -04:00
|
|
|
else
|
1999-02-23 12:11:01 -04:00
|
|
|
n = get_len_of_range(ihigh, ilow, -istep);
|
|
|
|
if (n < 0) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"xrange() has more than sys.maxint items");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
return PyRange_New(ilow, n, istep, 1);
|
1993-10-26 14:58:25 -03:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char xrange_doc[] =
|
|
|
|
"xrange([start,] stop[, step]) -> xrange object\n\
|
|
|
|
\n\
|
|
|
|
Like range(), but instead of returning a list, returns an object that\n\
|
|
|
|
generates the numbers in the range on demand. This is slightly slower\n\
|
|
|
|
than range() but more memory efficient.";
|
|
|
|
|
|
|
|
|
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);
|
1998-06-26 15:25:38 -03:00
|
|
|
if (prompt == NULL)
|
|
|
|
return NULL;
|
1995-07-07 19:43:42 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
po = NULL;
|
|
|
|
prompt = "";
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
s = PyOS_Readline(prompt);
|
|
|
|
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' */
|
2000-06-28 18:12:25 -03:00
|
|
|
size_t len = strlen(s);
|
|
|
|
if (len > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError, "input too long");
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = PyString_FromStringAndSize(s, (int)(len-1));
|
|
|
|
}
|
1995-07-07 19:43:42 -03:00
|
|
|
}
|
2000-05-03 20:44:39 -03:00
|
|
|
PyMem_FREE(s);
|
1995-07-07 19:43:42 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char raw_input_doc[] =
|
|
|
|
"raw_input([prompt]) -> string\n\
|
|
|
|
\n\
|
|
|
|
Read a string from standard input. The trailing newline is stripped.\n\
|
|
|
|
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
|
|
|
|
On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
|
|
|
|
is printed without a trailing newline before reading.";
|
|
|
|
|
|
|
|
|
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
|
|
|
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
sqf = seq->ob_type->tp_as_sequence;
|
|
|
|
if (sqf == NULL || sqf->sq_item == 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;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char reduce_doc[] =
|
|
|
|
"reduce(function, sequence[, initial]) -> value\n\
|
|
|
|
\n\
|
|
|
|
Apply a function of two arguments cumulatively to the items of a sequence,\n\
|
|
|
|
from left to right, so as to reduce the sequence to a single value.\n\
|
|
|
|
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
|
|
|
|
((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
|
|
|
|
of the sequence in the calculation, and serves as a default when the\n\
|
|
|
|
sequence is empty.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char reload_doc[] =
|
|
|
|
"reload(module) -> module\n\
|
|
|
|
\n\
|
|
|
|
Reload the module. The module must have been successfully imported before.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char repr_doc[] =
|
|
|
|
"repr(object) -> string\n\
|
|
|
|
\n\
|
|
|
|
Return the canonical string representation of the object.\n\
|
|
|
|
For most object types, eval(repr(object)) == object.";
|
|
|
|
|
|
|
|
|
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;
|
1998-05-09 11:42:25 -03:00
|
|
|
i = abs(ndigits);
|
|
|
|
while (--i >= 0)
|
1993-02-12 12:29:05 -04:00
|
|
|
f = f*10.0;
|
1998-05-09 11:42:25 -03:00
|
|
|
if (ndigits < 0)
|
|
|
|
x /= f;
|
|
|
|
else
|
|
|
|
x *= f;
|
1993-02-12 12:29:05 -04:00
|
|
|
if (x >= 0.0)
|
1998-05-09 11:42:25 -03:00
|
|
|
x = floor(x + 0.5);
|
|
|
|
else
|
|
|
|
x = ceil(x - 0.5);
|
|
|
|
if (ndigits < 0)
|
|
|
|
x *= f;
|
1993-02-12 12:29:05 -04:00
|
|
|
else
|
1998-05-09 11:42:25 -03:00
|
|
|
x /= f;
|
|
|
|
return PyFloat_FromDouble(x);
|
1993-02-12 12:29:05 -04:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char round_doc[] =
|
|
|
|
"round(number[, ndigits]) -> floating point number\n\
|
|
|
|
\n\
|
|
|
|
Round a number to a given precision in decimal digits (default 0 digits).\n\
|
|
|
|
This always returns a floating point number. Precision may be negative.";
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char str_doc[] =
|
|
|
|
"str(object) -> string\n\
|
|
|
|
\n\
|
|
|
|
Return a nice string representation of the object.\n\
|
|
|
|
If the argument is a string, the return value is the same object.";
|
|
|
|
|
|
|
|
|
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;
|
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;
|
A bunch of functions are now properly implemented in abstract.c, and
the code here becomes much simpler. In particular: abs(), divmod(),
pow(), int(), long(), float(), len(), tuple(), list().
Also make sure that no use of a function pointer gotten from a
tp_as_sequence or tp_as_mapping structure is made without checking it
for NULL first.
A few other cosmetic things, such as properly reindenting slice().
1998-05-21 21:51:39 -03:00
|
|
|
return PySequence_Tuple(v);
|
1994-08-29 09:53:11 -03:00
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char tuple_doc[] =
|
|
|
|
"tuple(sequence) -> list\n\
|
|
|
|
\n\
|
|
|
|
Return a tuple whose items are the same as those of the argument sequence.\n\
|
|
|
|
If the argument is a tuple, the return value is the same object.";
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char type_doc[] =
|
|
|
|
"type(object) -> type object\n\
|
|
|
|
\n\
|
|
|
|
Return the type of the object.";
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char vars_doc[] =
|
|
|
|
"vars([object]) -> dictionary\n\
|
|
|
|
\n\
|
|
|
|
Without arguments, equivalent to locals().\n\
|
|
|
|
With an argument, equivalent to object.__dict__.";
|
|
|
|
|
1999-06-16 14:28:37 -03:00
|
|
|
static int
|
|
|
|
abstract_issubclass(derived, cls, err, first)
|
|
|
|
PyObject *derived;
|
|
|
|
PyObject *cls;
|
|
|
|
char *err;
|
|
|
|
int first;
|
|
|
|
{
|
|
|
|
static PyObject *__bases__ = NULL;
|
|
|
|
PyObject *bases;
|
1999-06-17 16:12:39 -03:00
|
|
|
int i, n;
|
1999-06-16 14:28:37 -03:00
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
if (__bases__ == NULL) {
|
|
|
|
__bases__ = PyString_FromString("__bases__");
|
|
|
|
if (__bases__ == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
bases = PyObject_GetAttr(cls, __bases__);
|
|
|
|
if (bases == NULL || !PyTuple_Check(bases)) {
|
|
|
|
Py_XDECREF(bases);
|
|
|
|
PyErr_SetString(PyExc_TypeError, err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(bases);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (derived == cls)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
bases = PyObject_GetAttr(derived, __bases__);
|
|
|
|
if (bases == NULL || !PyTuple_Check(bases)) {
|
|
|
|
Py_XDECREF(bases);
|
|
|
|
PyErr_SetString(PyExc_TypeError, err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = PyTuple_GET_SIZE(bases);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
|
|
|
|
cls, err, 0);
|
|
|
|
if (r != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(bases);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
1998-06-26 18:23:49 -03:00
|
|
|
|
1997-08-22 18:14:38 -03:00
|
|
|
static PyObject *
|
|
|
|
builtin_isinstance(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyObject *inst;
|
|
|
|
PyObject *cls;
|
1999-06-16 14:28:37 -03:00
|
|
|
PyObject *icls;
|
|
|
|
static PyObject *__class__ = NULL;
|
|
|
|
int retval = 0;
|
1997-08-22 18:14:38 -03:00
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
|
1997-08-22 18:14:38 -03:00
|
|
|
return NULL;
|
1999-06-16 14:28:37 -03:00
|
|
|
|
|
|
|
if (PyClass_Check(cls)) {
|
|
|
|
if (PyInstance_Check(inst)) {
|
|
|
|
PyObject *inclass =
|
|
|
|
(PyObject*)((PyInstanceObject*)inst)->in_class;
|
|
|
|
retval = PyClass_IsSubclass(inclass, cls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (PyType_Check(cls)) {
|
1997-12-10 01:51:47 -04:00
|
|
|
retval = ((PyObject *)(inst->ob_type) == cls);
|
1997-08-22 18:14:38 -03:00
|
|
|
}
|
1999-06-16 14:28:37 -03:00
|
|
|
else if (!PyInstance_Check(inst)) {
|
|
|
|
if (__class__ == NULL) {
|
|
|
|
__class__ = PyString_FromString("__class__");
|
|
|
|
if (__class__ == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
icls = PyObject_GetAttr(inst, __class__);
|
|
|
|
if (icls != NULL) {
|
|
|
|
retval = abstract_issubclass(
|
|
|
|
icls, cls,
|
|
|
|
"second argument must be a class",
|
|
|
|
1);
|
|
|
|
Py_DECREF(icls);
|
|
|
|
if (retval < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
1997-12-02 15:11:45 -04:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"second argument must be a class");
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-16 14:28:37 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"second argument must be a class");
|
|
|
|
return NULL;
|
1997-08-22 18:14:38 -03:00
|
|
|
}
|
|
|
|
return PyInt_FromLong(retval);
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char isinstance_doc[] =
|
|
|
|
"isinstance(object, class-or-type) -> Boolean\n\
|
|
|
|
\n\
|
|
|
|
Return whether an object is an instance of a class or of a subclass thereof.\n\
|
|
|
|
With a type as second argument, return whether that is the object's type.";
|
|
|
|
|
1997-08-22 18:14:38 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
builtin_issubclass(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyObject *derived;
|
|
|
|
PyObject *cls;
|
|
|
|
int retval;
|
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
|
1997-08-22 18:14:38 -03:00
|
|
|
return NULL;
|
1999-06-16 14:28:37 -03:00
|
|
|
|
1997-08-22 18:14:38 -03:00
|
|
|
if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
|
1999-06-16 14:28:37 -03:00
|
|
|
retval = abstract_issubclass(
|
|
|
|
derived, cls, "arguments must be classes", 1);
|
|
|
|
if (retval < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* shortcut */
|
|
|
|
if (!(retval = (derived == cls)))
|
|
|
|
retval = PyClass_IsSubclass(derived, cls);
|
1997-08-22 18:14:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return PyInt_FromLong(retval);
|
|
|
|
}
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char issubclass_doc[] =
|
|
|
|
"issubclass(C, B) -> Boolean\n\
|
|
|
|
\n\
|
|
|
|
Return whether class C is a subclass (i.e., a derived class) of class B.";
|
|
|
|
|
1997-08-22 18:14:38 -03:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyMethodDef builtin_methods[] = {
|
1998-06-26 18:23:49 -03:00
|
|
|
{"__import__", builtin___import__, 1, import_doc},
|
|
|
|
{"abs", builtin_abs, 1, abs_doc},
|
|
|
|
{"apply", builtin_apply, 1, apply_doc},
|
1999-03-19 15:07:19 -04:00
|
|
|
{"buffer", builtin_buffer, 1, buffer_doc},
|
1998-06-26 18:23:49 -03:00
|
|
|
{"callable", builtin_callable, 1, callable_doc},
|
|
|
|
{"chr", builtin_chr, 1, chr_doc},
|
|
|
|
{"cmp", builtin_cmp, 1, cmp_doc},
|
|
|
|
{"coerce", builtin_coerce, 1, coerce_doc},
|
|
|
|
{"compile", builtin_compile, 1, compile_doc},
|
1996-01-11 21:09:56 -04:00
|
|
|
#ifndef WITHOUT_COMPLEX
|
1998-06-26 18:23:49 -03:00
|
|
|
{"complex", builtin_complex, 1, complex_doc},
|
1996-01-11 21:09:56 -04:00
|
|
|
#endif
|
1998-06-26 18:23:49 -03:00
|
|
|
{"delattr", builtin_delattr, 1, delattr_doc},
|
|
|
|
{"dir", builtin_dir, 1, dir_doc},
|
|
|
|
{"divmod", builtin_divmod, 1, divmod_doc},
|
|
|
|
{"eval", builtin_eval, 1, eval_doc},
|
|
|
|
{"execfile", builtin_execfile, 1, execfile_doc},
|
|
|
|
{"filter", builtin_filter, 1, filter_doc},
|
|
|
|
{"float", builtin_float, 1, float_doc},
|
|
|
|
{"getattr", builtin_getattr, 1, getattr_doc},
|
|
|
|
{"globals", builtin_globals, 1, globals_doc},
|
|
|
|
{"hasattr", builtin_hasattr, 1, hasattr_doc},
|
|
|
|
{"hash", builtin_hash, 1, hash_doc},
|
|
|
|
{"hex", builtin_hex, 1, hex_doc},
|
|
|
|
{"id", builtin_id, 1, id_doc},
|
|
|
|
{"input", builtin_input, 1, input_doc},
|
|
|
|
{"intern", builtin_intern, 1, intern_doc},
|
|
|
|
{"int", builtin_int, 1, int_doc},
|
|
|
|
{"isinstance", builtin_isinstance, 1, isinstance_doc},
|
|
|
|
{"issubclass", builtin_issubclass, 1, issubclass_doc},
|
|
|
|
{"len", builtin_len, 1, len_doc},
|
|
|
|
{"list", builtin_list, 1, list_doc},
|
|
|
|
{"locals", builtin_locals, 1, locals_doc},
|
|
|
|
{"long", builtin_long, 1, long_doc},
|
|
|
|
{"map", builtin_map, 1, map_doc},
|
|
|
|
{"max", builtin_max, 1, max_doc},
|
|
|
|
{"min", builtin_min, 1, min_doc},
|
|
|
|
{"oct", builtin_oct, 1, oct_doc},
|
|
|
|
{"open", builtin_open, 1, open_doc},
|
|
|
|
{"ord", builtin_ord, 1, ord_doc},
|
|
|
|
{"pow", builtin_pow, 1, pow_doc},
|
|
|
|
{"range", builtin_range, 1, range_doc},
|
|
|
|
{"raw_input", builtin_raw_input, 1, raw_input_doc},
|
|
|
|
{"reduce", builtin_reduce, 1, reduce_doc},
|
|
|
|
{"reload", builtin_reload, 1, reload_doc},
|
|
|
|
{"repr", builtin_repr, 1, repr_doc},
|
|
|
|
{"round", builtin_round, 1, round_doc},
|
|
|
|
{"setattr", builtin_setattr, 1, setattr_doc},
|
|
|
|
{"slice", builtin_slice, 1, slice_doc},
|
|
|
|
{"str", builtin_str, 1, str_doc},
|
|
|
|
{"tuple", builtin_tuple, 1, tuple_doc},
|
|
|
|
{"type", builtin_type, 1, type_doc},
|
2000-03-10 19:00:52 -04:00
|
|
|
{"unicode", builtin_unicode, 1, unicode_doc},
|
|
|
|
{"unichr", builtin_unichr, 1, unichr_doc},
|
1998-06-26 18:23:49 -03:00
|
|
|
{"vars", builtin_vars, 1, vars_doc},
|
|
|
|
{"xrange", builtin_xrange, 1, xrange_doc},
|
1991-12-16 09:03:00 -04:00
|
|
|
{NULL, NULL},
|
1990-12-20 11:06:42 -04:00
|
|
|
};
|
|
|
|
|
1998-06-26 18:23:49 -03:00
|
|
|
static char builtin_doc[] =
|
|
|
|
"Built-in functions, exceptions, and other objects.\n\
|
|
|
|
\n\
|
|
|
|
Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
|
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
PyObject *
|
2000-05-25 20:15:05 -03:00
|
|
|
_PyBuiltin_Init()
|
1997-08-02 00:10:38 -03:00
|
|
|
{
|
2000-06-20 01:54:19 -03:00
|
|
|
PyObject *mod, *dict, *debug;
|
1998-06-26 18:23:49 -03:00
|
|
|
mod = Py_InitModule4("__builtin__", builtin_methods,
|
|
|
|
builtin_doc, (PyObject *)NULL,
|
|
|
|
PYTHON_API_VERSION);
|
1997-08-02 00:10:38 -03:00
|
|
|
if (mod == NULL)
|
|
|
|
return NULL;
|
|
|
|
dict = PyModule_GetDict(mod);
|
|
|
|
if (PyDict_SetItemString(dict, "None", Py_None) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
|
|
|
|
return NULL;
|
2000-06-20 01:54:19 -03:00
|
|
|
debug = PyInt_FromLong(Py_OptimizeFlag == 0);
|
|
|
|
if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
|
|
|
|
Py_XDECREF(debug);
|
1997-08-02 00:10:38 -03:00
|
|
|
return NULL;
|
2000-06-20 01:54:19 -03:00
|
|
|
}
|
|
|
|
Py_XDECREF(debug);
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|