📄 bltinmodule.c
字号:
"eval() arg 1 must be a string or code object");
return NULL;
}
if (PyString_AsStringAndSize(cmd, &str, NULL))
return NULL;
while (*str == ' ' || *str == '\t')
str++;
cf.cf_flags = 0;
(void)PyEval_MergeCompilerFlags(&cf);
return PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
}
const static char eval_doc[] =
#ifdef SYMBIAN
"";
#else
"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.";
#endif
static PyObject *
builtin_execfile(PyObject *self, PyObject *args)
{
char *filename;
PyObject *globals = Py_None, *locals = Py_None;
PyObject *res;
FILE* fp = NULL;
PyCompilerFlags cf;
int exists;
#ifndef RISCOS
struct stat s;
#endif
if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
&filename,
&PyDict_Type, &globals,
&PyDict_Type, &locals))
return NULL;
if (globals == Py_None) {
globals = PyEval_GetGlobals();
if (locals == Py_None)
locals = PyEval_GetLocals();
}
else if (locals == Py_None)
locals = globals;
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
if (PyDict_SetItemString(globals, "__builtins__",
PyEval_GetBuiltins()) != 0)
return NULL;
}
exists = 0;
/* Test for existence or directory. */
#ifndef RISCOS
if (!stat(filename, &s)) {
if (S_ISDIR(s.st_mode))
#if defined(PYOS_OS2) && defined(PYCC_VACPP)
errno = EOS2ERR;
#else
errno = EISDIR;
#endif
else
exists = 1;
}
#else
if (object_exists(filename)) {
if (isdir(filename))
errno = EISDIR;
else
exists = 1;
}
#endif /* RISCOS */
if (exists) {
Py_BEGIN_ALLOW_THREADS
fp = fopen(filename, "r");
Py_END_ALLOW_THREADS
if (fp == NULL) {
exists = 0;
}
}
if (!exists) {
PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
return NULL;
}
cf.cf_flags = 0;
if (PyEval_MergeCompilerFlags(&cf))
res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
locals, 1, &cf);
else
res = PyRun_FileEx(fp, filename, Py_file_input, globals,
locals, 1);
return res;
}
const static char execfile_doc[] =
#ifdef SYMBIAN
"";
#else
"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.";
#endif
static PyObject *
builtin_getattr(PyObject *self, PyObject *args)
{
PyObject *v, *result, *dflt = NULL;
PyObject *name;
if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
return NULL;
#ifdef Py_USING_UNICODE
if (PyUnicode_Check(name)) {
name = _PyUnicode_AsDefaultEncodedString(name, NULL);
if (name == NULL)
return NULL;
}
#endif
if (!PyString_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"attribute name must be string");
return NULL;
}
result = PyObject_GetAttr(v, name);
if (result == NULL && dflt != NULL &&
PyErr_ExceptionMatches(PyExc_AttributeError))
{
PyErr_Clear();
Py_INCREF(dflt);
result = dflt;
}
return result;
}
const static char getattr_doc[] =
#ifdef SYMBIAN
"";
#else
"getattr(object, name[, default]) -> value\n\
\n\
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
When a default argument is given, it is returned when the attribute doesn't\n\
exist; without it, an exception is raised in that case.";
#endif
static PyObject *
builtin_globals(PyObject *self)
{
PyObject *d;
d = PyEval_GetGlobals();
Py_INCREF(d);
return d;
}
const static char globals_doc[] =
#ifdef SYMBIAN
"";
#else
"globals() -> dictionary\n\
\n\
Return the dictionary containing the current scope's global variables.";
#endif
static PyObject *
builtin_hasattr(PyObject *self, PyObject *args)
{
PyObject *v;
PyObject *name;
if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
return NULL;
#ifdef Py_USING_UNICODE
if (PyUnicode_Check(name)) {
name = _PyUnicode_AsDefaultEncodedString(name, NULL);
if (name == NULL)
return NULL;
}
#endif
if (!PyString_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"attribute name must be string");
return NULL;
}
v = PyObject_GetAttr(v, name);
if (v == NULL) {
PyErr_Clear();
Py_INCREF(Py_False);
return Py_False;
}
Py_DECREF(v);
Py_INCREF(Py_True);
return Py_True;
}
const static char hasattr_doc[] =
#ifdef SYMBIAN
"";
#else
"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.)";
#endif
static PyObject *
builtin_id(PyObject *self, PyObject *v)
{
return PyLong_FromVoidPtr(v);
}
const static char id_doc[] =
#ifdef SYMBIAN
"";
#else
"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.)";
#endif
static PyObject *
builtin_map(PyObject *self, PyObject *args)
{
typedef struct {
PyObject *it; /* the iterator object */
int saw_StopIteration; /* bool: did the iterator end? */
} sequence;
PyObject *func, *result;
sequence *seqs = NULL, *sqp;
int n, len;
register int i, j;
n = PyTuple_Size(args);
if (n < 2) {
PyErr_SetString(PyExc_TypeError,
"map() requires at least two args");
return NULL;
}
func = PyTuple_GetItem(args, 0);
n--;
if (func == Py_None && n == 1) {
/* map(None, S) is the same as list(S). */
return PySequence_List(PyTuple_GetItem(args, 1));
}
/* Get space for sequence descriptors. Must NULL out the iterator
* pointers so that jumping to Fail_2 later doesn't see trash.
*/
if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
PyErr_NoMemory();
return NULL;
}
for (i = 0; i < n; ++i) {
seqs[i].it = (PyObject*)NULL;
seqs[i].saw_StopIteration = 0;
}
/* Do a first pass to obtain iterators for the arguments, and set len
* to the largest of their lengths.
*/
len = 0;
for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
PyObject *curseq;
int curlen;
/* Get iterator. */
curseq = PyTuple_GetItem(args, i+1);
sqp->it = PyObject_GetIter(curseq);
if (sqp->it == NULL) {
static const char errmsg[] =
"argument %d to map() must support iteration";
char errbuf[sizeof(errmsg) + 25];
PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
PyErr_SetString(PyExc_TypeError, errbuf);
goto Fail_2;
}
/* Update len. */
curlen = -1; /* unknown */
if (PySequence_Check(curseq) &&
curseq->ob_type->tp_as_sequence->sq_length) {
curlen = PySequence_Size(curseq);
if (curlen < 0)
PyErr_Clear();
}
if (curlen < 0)
curlen = 8; /* arbitrary */
if (curlen > len)
len = curlen;
}
/* Get space for the result list. */
if ((result = (PyObject *) PyList_New(len)) == NULL)
goto Fail_2;
/* Iterate over the sequences until all have stopped. */
for (i = 0; ; ++i) {
PyObject *alist, *item=NULL, *value;
int numactive = 0;
if (func == Py_None && n == 1)
alist = NULL;
else if ((alist = PyTuple_New(n)) == NULL)
goto Fail_1;
for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
if (sqp->saw_StopIteration) {
Py_INCREF(Py_None);
item = Py_None;
}
else {
item = PyIter_Next(sqp->it);
if (item)
++numactive;
else {
if (PyErr_Occurred()) {
Py_XDECREF(alist);
goto Fail_1;
}
Py_INCREF(Py_None);
item = Py_None;
sqp->saw_StopIteration = 1;
}
}
if (alist)
PyTuple_SET_ITEM(alist, j, item);
else
break;
}
if (!alist)
alist = item;
if (numactive == 0) {
Py_DECREF(alist);
break;
}
if (func == Py_None)
value = alist;
else {
value = PyEval_CallObject(func, alist);
Py_DECREF(alist);
if (value == NULL)
goto Fail_1;
}
if (i >= len) {
int status = PyList_Append(result, value);
Py_DECREF(value);
if (status < 0)
goto Fail_1;
}
else if (PyList_SetItem(result, i, value) < 0)
goto Fail_1;
}
if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
goto Fail_1;
goto Succeed;
Fail_1:
Py_DECREF(result);
Fail_2:
result = NULL;
Succeed:
assert(seqs);
for (i = 0; i < n; ++i)
Py_XDECREF(seqs[i].it);
PyMem_DEL(seqs);
return result;
}
const static char map_doc[] =
#ifdef SYMBIAN
"";
#else
"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).";
#endif
static PyObject *
builtin_setattr(PyObject *self, PyObject *args)
{
PyObject *v;
PyObject *name;
PyObject *value;
if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
return NULL;
if (PyObject_SetAttr(v, name, value) != 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
const static char setattr_doc[] =
#ifdef SYMBIAN
"";
#else
"setattr(object, name, value)\n\
\n\
Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
``x.y = v''.";
#endif
static PyObject *
builtin_delattr(PyObject *self, PyObject *args)
{
PyObject *v;
PyObject *name;
if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
return NULL;
if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
const static char delattr_doc[] =
#ifdef SYMBIAN
"";
#else
"delattr(object, name)\n\
\n\
Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
``del x.y''.";
#endif
static PyObject *
builtin_hash(PyObject *self, PyObject *v)
{
long x;
x = PyObject_Hash(v);
if (x == -1)
return NULL;
return PyInt_FromLong(x);
}
const static char hash_doc[] =
#ifdef SYMBIAN
"";
#else
"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.";
#endif
static PyObject *
builtin_hex(PyObject *self, PyObject *v)
{
PyNumberMethods *nb;
// XXX:CW32
if ((nb = (PyNumberMethods *)v->ob_type->tp_as_number) == NULL ||
nb->nb_hex == NULL) {
PyErr_SetString(PyExc_TypeError,
"hex() argument can't be converted to hex");
return NULL;
}
return (*nb->nb_hex)(v);
}
const static char hex_doc[] =
#ifdef SYMBIAN
"";
#else
"hex(number) -> string\n\
\n\
Return the hexadecimal representation of an integer or long integer.";
#endif
static PyObject *builtin_raw_input(PyObject *, PyObject *);
static PyObject *
builtin_input(PyObject *self, PyObject *args)
{
PyObject *line;
char *str;
PyObject *res;
PyObject *globals, *locals;
line = builtin_raw_input(self, args);
if (line == NULL)
return line;
if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
return NULL;
while (*str == ' ' || *str == '\t')
str++;
globals = PyEval_GetGlobals();
locals = PyEval_GetLocals();
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
if (PyDict_SetItemString(globals, "__builtins__",
PyEval_GetBuiltins()) != 0)
return NULL;
}
res = PyRun_String(str, Py_eval_input, globals, locals);
Py_DECREF(line);
return res;
}
const static char input_doc[] =
#ifdef SYMBIAN
"";
#else
"input([prompt]) -> value\n\
\n\
Equivalent to eval(raw_input(prompt)).";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -