2 /* Built-in functions */
16 /* The default encoding used by the platform file system APIs
17 Can remain NULL for all platforms that don't have such a concept
19 #if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
20 const char *Py_FileSystemDefaultEncoding
= "mbcs";
22 const char *Py_FileSystemDefaultEncoding
= NULL
; /* use default */
26 static PyObject
*filterstring(PyObject
*, PyObject
*);
27 static PyObject
*filtertuple (PyObject
*, PyObject
*);
30 builtin___import__(PyObject
*self
, PyObject
*args
)
33 PyObject
*globals
= NULL
;
34 PyObject
*locals
= NULL
;
35 PyObject
*fromlist
= NULL
;
37 if (!PyArg_ParseTuple(args
, "s|OOO:__import__",
38 &name
, &globals
, &locals
, &fromlist
))
40 return PyImport_ImportModuleEx(name
, globals
, locals
, fromlist
);
43 static char import_doc
[] =
44 "__import__(name, globals, locals, fromlist) -> module\n\
46 Import a module. The globals are only used to determine the context;\n\
47 they are not modified. The locals are currently unused. The fromlist\n\
48 should be a list of names to emulate ``from name import ...'', or an\n\
49 empty list to emulate ``import name''.\n\
50 When importing a module from a package, note that __import__('A.B', ...)\n\
51 returns package A when fromlist is empty, but its submodule B when\n\
52 fromlist is not empty.";
56 builtin_abs(PyObject
*self
, PyObject
*args
)
60 if (!PyArg_ParseTuple(args
, "O:abs", &v
))
62 return PyNumber_Absolute(v
);
65 static char abs_doc
[] =
66 "abs(number) -> number\n\
68 Return the absolute value of the argument.";
72 builtin_apply(PyObject
*self
, PyObject
*args
)
74 PyObject
*func
, *alist
= NULL
, *kwdict
= NULL
;
75 PyObject
*t
= NULL
, *retval
= NULL
;
77 if (!PyArg_ParseTuple(args
, "O|OO:apply", &func
, &alist
, &kwdict
))
80 if (!PyTuple_Check(alist
)) {
81 if (!PySequence_Check(alist
)) {
82 PyErr_Format(PyExc_TypeError
,
83 "apply() arg 2 expect sequence, found %s",
84 alist
->ob_type
->tp_name
);
87 t
= PySequence_Tuple(alist
);
93 if (kwdict
!= NULL
&& !PyDict_Check(kwdict
)) {
94 PyErr_Format(PyExc_TypeError
,
95 "apply() arg 3 expected dictionary, found %s",
96 kwdict
->ob_type
->tp_name
);
99 retval
= PyEval_CallObjectWithKeywords(func
, alist
, kwdict
);
105 static char apply_doc
[] =
106 "apply(object[, args[, kwargs]]) -> value\n\
108 Call a callable object with positional arguments taken from the tuple args,\n\
109 and keyword arguments taken from the optional dictionary kwargs.\n\
110 Note that classes are callable, as are instances with a __call__() method.";
114 builtin_buffer(PyObject
*self
, PyObject
*args
)
118 int size
= Py_END_OF_BUFFER
;
120 if ( !PyArg_ParseTuple(args
, "O|ii:buffer", &ob
, &offset
, &size
) )
122 return PyBuffer_FromObject(ob
, offset
, size
);
125 static char buffer_doc
[] =
126 "buffer(object [, offset[, size]]) -> object\n\
128 Create a new buffer object which references the given object.\n\
129 The buffer will reference a slice of the target object from the\n\
130 start of the object (or at the specified offset). The slice will\n\
131 extend to the end of the target object (or with the specified size).";
135 builtin_callable(PyObject
*self
, PyObject
*args
)
139 if (!PyArg_ParseTuple(args
, "O:callable", &v
))
141 return PyInt_FromLong((long)PyCallable_Check(v
));
144 static char callable_doc
[] =
145 "callable(object) -> Boolean\n\
147 Return whether the object is callable (i.e., some kind of function).\n\
148 Note that classes are callable, as are instances with a __call__() method.";
152 builtin_filter(PyObject
*self
, PyObject
*args
)
154 PyObject
*func
, *seq
, *result
, *it
;
155 int len
; /* guess for result list size */
158 if (!PyArg_ParseTuple(args
, "OO:filter", &func
, &seq
))
161 /* Strings and tuples return a result of the same type. */
162 if (PyString_Check(seq
))
163 return filterstring(func
, seq
);
164 if (PyTuple_Check(seq
))
165 return filtertuple(func
, seq
);
168 it
= PyObject_GetIter(seq
);
172 /* Guess a result list size. */
173 len
= -1; /* unknown */
174 if (PySequence_Check(seq
) &&
175 seq
->ob_type
->tp_as_sequence
->sq_length
) {
176 len
= PySequence_Size(seq
);
181 len
= 8; /* arbitrary */
183 /* Get a result list. */
184 if (PyList_Check(seq
) && seq
->ob_refcnt
== 1) {
185 /* Eww - can modify the list in-place. */
190 result
= PyList_New(len
);
195 /* Build the result list. */
198 PyObject
*item
, *good
;
201 item
= PyIter_Next(it
);
203 if (PyErr_Occurred())
208 if (func
== Py_None
) {
213 PyObject
*arg
= Py_BuildValue("(O)", item
);
218 good
= PyEval_CallObject(func
, arg
);
225 ok
= PyObject_IsTrue(good
);
229 PyList_SET_ITEM(result
, j
, item
);
231 int status
= PyList_Append(result
, item
);
243 /* Cut back result list if len is too big. */
244 if (j
< len
&& PyList_SetSlice(result
, j
, len
, NULL
) < 0)
257 static char filter_doc
[] =
258 "filter(function, sequence) -> list\n\
260 Return a list containing those items of sequence for which function(item)\n\
261 is true. If function is None, return a list of items that are true.";
265 builtin_chr(PyObject
*self
, PyObject
*args
)
270 if (!PyArg_ParseTuple(args
, "l:chr", &x
))
272 if (x
< 0 || x
>= 256) {
273 PyErr_SetString(PyExc_ValueError
,
274 "chr() arg not in range(256)");
278 return PyString_FromStringAndSize(s
, 1);
281 static char chr_doc
[] =
282 "chr(i) -> character\n\
284 Return a string of one character with ordinal i; 0 <= i < 256.";
288 builtin_unichr(PyObject
*self
, PyObject
*args
)
293 if (!PyArg_ParseTuple(args
, "l:unichr", &x
))
296 #ifdef Py_UNICODE_WIDE
297 if (x
< 0 || x
> 0x10ffff) {
298 PyErr_SetString(PyExc_ValueError
,
299 "unichr() arg not in range(0x110000) "
300 "(wide Python build)");
304 if (x
< 0 || x
> 0xffff) {
305 PyErr_SetString(PyExc_ValueError
,
306 "unichr() arg not in range(0x10000) "
307 "(narrow Python build)");
313 /* UCS-2 character */
314 s
[0] = (Py_UNICODE
) x
;
315 return PyUnicode_FromUnicode(s
, 1);
318 #ifndef Py_UNICODE_WIDE
319 /* UCS-4 character. store as two surrogate characters */
321 s
[0] = 0xD800 + (Py_UNICODE
) (x
>> 10);
322 s
[1] = 0xDC00 + (Py_UNICODE
) (x
& 0x03FF);
323 return PyUnicode_FromUnicode(s
, 2);
325 s
[0] = (Py_UNICODE
)x
;
326 return PyUnicode_FromUnicode(s
, 1);
331 static char unichr_doc
[] =
332 "unichr(i) -> Unicode character\n\
334 Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.";
338 builtin_cmp(PyObject
*self
, PyObject
*args
)
343 if (!PyArg_ParseTuple(args
, "OO:cmp", &a
, &b
))
345 if (PyObject_Cmp(a
, b
, &c
) < 0)
347 return PyInt_FromLong((long)c
);
350 static char cmp_doc
[] =
351 "cmp(x, y) -> integer\n\
353 Return negative if x<y, zero if x==y, positive if x>y.";
357 builtin_coerce(PyObject
*self
, PyObject
*args
)
362 if (!PyArg_ParseTuple(args
, "OO:coerce", &v
, &w
))
364 if (PyNumber_Coerce(&v
, &w
) < 0)
366 res
= Py_BuildValue("(OO)", v
, w
);
372 static char coerce_doc
[] =
373 "coerce(x, y) -> None or (x1, y1)\n\
375 When x and y can be coerced to values of the same type, return a tuple\n\
376 containing the coerced values. When they can't be coerced, return None.";
380 builtin_compile(PyObject
*self
, PyObject
*args
)
388 if (!PyArg_ParseTuple(args
, "sss:compile", &str
, &filename
, &startstr
))
390 if (strcmp(startstr
, "exec") == 0)
391 start
= Py_file_input
;
392 else if (strcmp(startstr
, "eval") == 0)
393 start
= Py_eval_input
;
394 else if (strcmp(startstr
, "single") == 0)
395 start
= Py_single_input
;
397 PyErr_SetString(PyExc_ValueError
,
398 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
402 if (PyEval_MergeCompilerFlags(&cf
))
403 return Py_CompileStringFlags(str
, filename
, start
, &cf
);
405 return Py_CompileString(str
, filename
, start
);
408 static char compile_doc
[] =
409 "compile(source, filename, mode) -> code object\n\
411 Compile the source string (a Python module, statement or expression)\n\
412 into a code object that can be executed by the exec statement or eval().\n\
413 The filename will be used for run-time error messages.\n\
414 The mode must be 'exec' to compile a module, 'single' to compile a\n\
415 single (interactive) statement, or 'eval' to compile an expression.";
419 builtin_dir(PyObject
*self
, PyObject
*args
)
421 static char *attrlist
[] = {"__members__", "__methods__", NULL
};
422 PyObject
*v
= NULL
, *l
= NULL
, *m
= NULL
;
427 if (!PyArg_ParseTuple(args
, "|O:dir", &v
))
430 x
= PyEval_GetLocals();
433 l
= PyMapping_Keys(x
);
438 d
= PyObject_GetAttrString(v
, "__dict__");
442 l
= PyMapping_Keys(d
);
452 for (s
= attrlist
; *s
!= NULL
; s
++) {
453 m
= PyObject_GetAttrString(v
, *s
);
459 x
= PySequence_GetItem(m
, i
);
464 if (PyList_Append(l
, x
) != 0) {
474 if (PyList_Sort(l
) != 0)
482 static char dir_doc
[] =
483 "dir([object]) -> list of strings\n\
485 Return an alphabetized list of names comprising (some of) the attributes\n\
486 of the given object. Without an argument, the names in the current scope\n\
487 are listed. With an instance argument, only the instance attributes are\n\
488 returned. With a class argument, attributes of the base class are not\n\
489 returned. For other types or arguments, this may list members or methods.";
493 builtin_divmod(PyObject
*self
, PyObject
*args
)
497 if (!PyArg_ParseTuple(args
, "OO:divmod", &v
, &w
))
499 return PyNumber_Divmod(v
, w
);
502 static char divmod_doc
[] =
503 "divmod(x, y) -> (div, mod)\n\
505 Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
509 builtin_eval(PyObject
*self
, PyObject
*args
)
512 PyObject
*globals
= Py_None
, *locals
= Py_None
;
515 if (!PyArg_ParseTuple(args
, "O|O!O!:eval",
517 &PyDict_Type
, &globals
,
518 &PyDict_Type
, &locals
))
520 if (globals
== Py_None
) {
521 globals
= PyEval_GetGlobals();
522 if (locals
== Py_None
)
523 locals
= PyEval_GetLocals();
525 else if (locals
== Py_None
)
527 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
) {
528 if (PyDict_SetItemString(globals
, "__builtins__",
529 PyEval_GetBuiltins()) != 0)
532 if (PyCode_Check(cmd
)) {
533 if (PyTuple_GET_SIZE(((PyCodeObject
*)cmd
)->co_freevars
) > 0) {
534 PyErr_SetString(PyExc_TypeError
,
535 "code object passed to eval() may not contain free variables");
538 return PyEval_EvalCode((PyCodeObject
*) cmd
, globals
, locals
);
540 if (!PyString_Check(cmd
) &&
541 !PyUnicode_Check(cmd
)) {
542 PyErr_SetString(PyExc_TypeError
,
543 "eval() arg 1 must be a string or code object");
546 if (PyString_AsStringAndSize(cmd
, &str
, NULL
))
548 while (*str
== ' ' || *str
== '\t')
550 return PyRun_String(str
, Py_eval_input
, globals
, locals
);
553 static char eval_doc
[] =
554 "eval(source[, globals[, locals]]) -> value\n\
556 Evaluate the source in the context of globals and locals.\n\
557 The source may be a string representing a Python expression\n\
558 or a code object as returned by compile().\n\
559 The globals and locals are dictionaries, defaulting to the current\n\
560 globals and locals. If only globals is given, locals defaults to it.";
564 builtin_execfile(PyObject
*self
, PyObject
*args
)
567 PyObject
*globals
= Py_None
, *locals
= Py_None
;
574 if (!PyArg_ParseTuple(args
, "s|O!O!:execfile",
576 &PyDict_Type
, &globals
,
577 &PyDict_Type
, &locals
))
579 if (globals
== Py_None
) {
580 globals
= PyEval_GetGlobals();
581 if (locals
== Py_None
)
582 locals
= PyEval_GetLocals();
584 else if (locals
== Py_None
)
586 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
) {
587 if (PyDict_SetItemString(globals
, "__builtins__",
588 PyEval_GetBuiltins()) != 0)
593 /* Test for existence or directory. */
594 if (!stat(filename
, &s
)) {
595 if (S_ISDIR(s
.st_mode
))
602 Py_BEGIN_ALLOW_THREADS
603 fp
= fopen(filename
, "r");
612 PyErr_SetFromErrno(PyExc_IOError
);
616 if (PyEval_MergeCompilerFlags(&cf
))
617 res
= PyRun_FileExFlags(fp
, filename
, Py_file_input
, globals
,
620 res
= PyRun_FileEx(fp
, filename
, Py_file_input
, globals
,
625 static char execfile_doc
[] =
626 "execfile(filename[, globals[, locals]])\n\
628 Read and execute a Python script from a file.\n\
629 The globals and locals are dictionaries, defaulting to the current\n\
630 globals and locals. If only globals is given, locals defaults to it.";
634 builtin_getattr(PyObject
*self
, PyObject
*args
)
636 PyObject
*v
, *result
, *dflt
= NULL
;
639 if (!PyArg_ParseTuple(args
, "OO|O:getattr", &v
, &name
, &dflt
))
641 if (PyUnicode_Check(name
)) {
642 name
= _PyUnicode_AsDefaultEncodedString(name
, NULL
);
647 if (!PyString_Check(name
)) {
648 PyErr_SetString(PyExc_TypeError
,
649 "attribute name must be string");
652 result
= PyObject_GetAttr(v
, name
);
653 if (result
== NULL
&& dflt
!= NULL
) {
661 static char getattr_doc
[] =
662 "getattr(object, name[, default]) -> value\n\
664 Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
665 When a default argument is given, it is returned when the attribute doesn't\n\
666 exist; without it, an exception is raised in that case.";
670 builtin_globals(PyObject
*self
, PyObject
*args
)
674 if (!PyArg_ParseTuple(args
, ":globals"))
676 d
= PyEval_GetGlobals();
681 static char globals_doc
[] =
682 "globals() -> dictionary\n\
684 Return the dictionary containing the current scope's global variables.";
688 builtin_hasattr(PyObject
*self
, PyObject
*args
)
693 if (!PyArg_ParseTuple(args
, "OO:hasattr", &v
, &name
))
695 if (PyUnicode_Check(name
)) {
696 name
= _PyUnicode_AsDefaultEncodedString(name
, NULL
);
701 if (!PyString_Check(name
)) {
702 PyErr_SetString(PyExc_TypeError
,
703 "attribute name must be string");
706 v
= PyObject_GetAttr(v
, name
);
717 static char hasattr_doc
[] =
718 "hasattr(object, name) -> Boolean\n\
720 Return whether the object has an attribute with the given name.\n\
721 (This is done by calling getattr(object, name) and catching exceptions.)";
725 builtin_id(PyObject
*self
, PyObject
*args
)
729 if (!PyArg_ParseTuple(args
, "O:id", &v
))
731 return PyLong_FromVoidPtr(v
);
734 static char id_doc
[] =
735 "id(object) -> integer\n\
737 Return the identity of an object. This is guaranteed to be unique among\n\
738 simultaneously existing objects. (Hint: it's the object's memory address.)";
742 builtin_map(PyObject
*self
, PyObject
*args
)
745 PyObject
*it
; /* the iterator object */
746 int saw_StopIteration
; /* bool: did the iterator end? */
749 PyObject
*func
, *result
;
750 sequence
*seqs
= NULL
, *sqp
;
754 n
= PyTuple_Size(args
);
756 PyErr_SetString(PyExc_TypeError
,
757 "map() requires at least two args");
761 func
= PyTuple_GetItem(args
, 0);
764 if (func
== Py_None
&& n
== 1) {
765 /* map(None, S) is the same as list(S). */
766 return PySequence_List(PyTuple_GetItem(args
, 1));
769 /* Get space for sequence descriptors. Must NULL out the iterator
770 * pointers so that jumping to Fail_2 later doesn't see trash.
772 if ((seqs
= PyMem_NEW(sequence
, n
)) == NULL
) {
776 for (i
= 0; i
< n
; ++i
) {
777 seqs
[i
].it
= (PyObject
*)NULL
;
778 seqs
[i
].saw_StopIteration
= 0;
781 /* Do a first pass to obtain iterators for the arguments, and set len
782 * to the largest of their lengths.
785 for (i
= 0, sqp
= seqs
; i
< n
; ++i
, ++sqp
) {
790 curseq
= PyTuple_GetItem(args
, i
+1);
791 sqp
->it
= PyObject_GetIter(curseq
);
792 if (sqp
->it
== NULL
) {
793 static char errmsg
[] =
794 "argument %d to map() must support iteration";
795 char errbuf
[sizeof(errmsg
) + 25];
796 sprintf(errbuf
, errmsg
, i
+2);
797 PyErr_SetString(PyExc_TypeError
, errbuf
);
802 curlen
= -1; /* unknown */
803 if (PySequence_Check(curseq
) &&
804 curseq
->ob_type
->tp_as_sequence
->sq_length
) {
805 curlen
= PySequence_Size(curseq
);
810 curlen
= 8; /* arbitrary */
815 /* Get space for the result list. */
816 if ((result
= (PyObject
*) PyList_New(len
)) == NULL
)
819 /* Iterate over the sequences until all have stopped. */
821 PyObject
*alist
, *item
=NULL
, *value
;
824 if (func
== Py_None
&& n
== 1)
826 else if ((alist
= PyTuple_New(n
)) == NULL
)
829 for (j
= 0, sqp
= seqs
; j
< n
; ++j
, ++sqp
) {
830 if (sqp
->saw_StopIteration
) {
835 item
= PyIter_Next(sqp
->it
);
839 if (PyErr_Occurred()) {
845 sqp
->saw_StopIteration
= 1;
849 PyTuple_SET_ITEM(alist
, j
, item
);
857 if (numactive
== 0) {
865 value
= PyEval_CallObject(func
, alist
);
871 int status
= PyList_Append(result
, value
);
876 else if (PyList_SetItem(result
, i
, value
) < 0)
880 if (i
< len
&& PyList_SetSlice(result
, i
, len
, NULL
) < 0)
891 for (i
= 0; i
< n
; ++i
)
892 Py_XDECREF(seqs
[i
].it
);
897 static char map_doc
[] =
898 "map(function, sequence[, sequence, ...]) -> list\n\
900 Return a list of the results of applying the function to the items of\n\
901 the argument sequence(s). If more than one sequence is given, the\n\
902 function is called with an argument list consisting of the corresponding\n\
903 item of each sequence, substituting None for missing values when not all\n\
904 sequences have the same length. If the function is None, return a list of\n\
905 the items of the sequence (or a list of tuples if more than one sequence).";
909 builtin_setattr(PyObject
*self
, PyObject
*args
)
915 if (!PyArg_ParseTuple(args
, "OOO:setattr", &v
, &name
, &value
))
917 if (PyObject_SetAttr(v
, name
, value
) != 0)
923 static char setattr_doc
[] =
924 "setattr(object, name, value)\n\
926 Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
931 builtin_delattr(PyObject
*self
, PyObject
*args
)
936 if (!PyArg_ParseTuple(args
, "OO:delattr", &v
, &name
))
938 if (PyObject_SetAttr(v
, name
, (PyObject
*)NULL
) != 0)
944 static char delattr_doc
[] =
945 "delattr(object, name)\n\
947 Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
952 builtin_hash(PyObject
*self
, PyObject
*args
)
957 if (!PyArg_ParseTuple(args
, "O:hash", &v
))
959 x
= PyObject_Hash(v
);
962 return PyInt_FromLong(x
);
965 static char hash_doc
[] =
966 "hash(object) -> integer\n\
968 Return a hash value for the object. Two objects with the same value have\n\
969 the same hash value. The reverse is not necessarily true, but likely.";
973 builtin_hex(PyObject
*self
, PyObject
*args
)
978 if (!PyArg_ParseTuple(args
, "O:hex", &v
))
981 if ((nb
= v
->ob_type
->tp_as_number
) == NULL
||
982 nb
->nb_hex
== NULL
) {
983 PyErr_SetString(PyExc_TypeError
,
984 "hex() argument can't be converted to hex");
987 return (*nb
->nb_hex
)(v
);
990 static char hex_doc
[] =
991 "hex(number) -> string\n\
993 Return the hexadecimal representation of an integer or long integer.";
996 static PyObject
*builtin_raw_input(PyObject
*, PyObject
*);
999 builtin_input(PyObject
*self
, PyObject
*args
)
1004 PyObject
*globals
, *locals
;
1006 line
= builtin_raw_input(self
, args
);
1009 if (!PyArg_Parse(line
, "s;embedded '\\0' in input line", &str
))
1011 while (*str
== ' ' || *str
== '\t')
1013 globals
= PyEval_GetGlobals();
1014 locals
= PyEval_GetLocals();
1015 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
) {
1016 if (PyDict_SetItemString(globals
, "__builtins__",
1017 PyEval_GetBuiltins()) != 0)
1020 res
= PyRun_String(str
, Py_eval_input
, globals
, locals
);
1025 static char input_doc
[] =
1026 "input([prompt]) -> value\n\
1028 Equivalent to eval(raw_input(prompt)).";
1032 builtin_intern(PyObject
*self
, PyObject
*args
)
1035 if (!PyArg_ParseTuple(args
, "S:intern", &s
))
1038 PyString_InternInPlace(&s
);
1042 static char intern_doc
[] =
1043 "intern(string) -> string\n\
1045 ``Intern'' the given string. This enters the string in the (global)\n\
1046 table of interned strings whose purpose is to speed up dictionary lookups.\n\
1047 Return the string itself or the previously interned string object with the\n\
1052 builtin_iter(PyObject
*self
, PyObject
*args
)
1054 PyObject
*v
, *w
= NULL
;
1056 if (!PyArg_ParseTuple(args
, "O|O:iter", &v
, &w
))
1059 return PyObject_GetIter(v
);
1060 if (!PyCallable_Check(v
)) {
1061 PyErr_SetString(PyExc_TypeError
,
1062 "iter(v, w): v must be callable");
1065 return PyCallIter_New(v
, w
);
1068 static char iter_doc
[] =
1069 "iter(collection) -> iterator\n\
1070 iter(callable, sentinel) -> iterator\n\
1072 Get an iterator from an object. In the first form, the argument must\n\
1073 supply its own iterator, or be a sequence.\n\
1074 In the second form, the callable is called until it returns the sentinel.";
1078 builtin_len(PyObject
*self
, PyObject
*args
)
1083 if (!PyArg_ParseTuple(args
, "O:len", &v
))
1085 res
= PyObject_Size(v
);
1086 if (res
< 0 && PyErr_Occurred())
1088 return PyInt_FromLong(res
);
1091 static char len_doc
[] =
1092 "len(object) -> integer\n\
1094 Return the number of items of a sequence or mapping.";
1098 builtin_slice(PyObject
*self
, PyObject
*args
)
1100 PyObject
*start
, *stop
, *step
;
1102 start
= stop
= step
= NULL
;
1104 if (!PyArg_ParseTuple(args
, "O|OO:slice", &start
, &stop
, &step
))
1107 /* This swapping of stop and start is to maintain similarity with
1113 return PySlice_New(start
, stop
, step
);
1116 static char slice_doc
[] =
1117 "slice([start,] stop[, step]) -> slice object\n\
1119 Create a slice object. This is used for slicing by the Numeric extensions.";
1123 builtin_locals(PyObject
*self
, PyObject
*args
)
1127 if (!PyArg_ParseTuple(args
, ":locals"))
1129 d
= PyEval_GetLocals();
1134 static char locals_doc
[] =
1135 "locals() -> dictionary\n\
1137 Return the dictionary containing the current scope's local variables.";
1141 min_max(PyObject
*args
, int op
)
1143 PyObject
*v
, *w
, *x
, *it
;
1145 if (PyTuple_Size(args
) > 1)
1147 else if (!PyArg_ParseTuple(args
, "O:min/max", &v
))
1150 it
= PyObject_GetIter(v
);
1154 w
= NULL
; /* the result */
1156 x
= PyIter_Next(it
);
1158 if (PyErr_Occurred()) {
1169 int cmp
= PyObject_RichCompareBool(x
, w
, op
);
1185 PyErr_SetString(PyExc_ValueError
,
1186 "min() or max() arg is an empty sequence");
1192 builtin_min(PyObject
*self
, PyObject
*v
)
1194 return min_max(v
, Py_LT
);
1197 static char min_doc
[] =
1198 "min(sequence) -> value\n\
1199 min(a, b, c, ...) -> value\n\
1201 With a single sequence argument, return its smallest item.\n\
1202 With two or more arguments, return the smallest argument.";
1206 builtin_max(PyObject
*self
, PyObject
*v
)
1208 return min_max(v
, Py_GT
);
1211 static char max_doc
[] =
1212 "max(sequence) -> value\n\
1213 max(a, b, c, ...) -> value\n\
1215 With a single sequence argument, return its largest item.\n\
1216 With two or more arguments, return the largest argument.";
1220 builtin_oct(PyObject
*self
, PyObject
*args
)
1223 PyNumberMethods
*nb
;
1225 if (!PyArg_ParseTuple(args
, "O:oct", &v
))
1227 if (v
== NULL
|| (nb
= v
->ob_type
->tp_as_number
) == NULL
||
1228 nb
->nb_oct
== NULL
) {
1229 PyErr_SetString(PyExc_TypeError
,
1230 "oct() argument can't be converted to oct");
1233 return (*nb
->nb_oct
)(v
);
1236 static char oct_doc
[] =
1237 "oct(number) -> string\n\
1239 Return the octal representation of an integer or long integer.";
1243 builtin_open(PyObject
*self
, PyObject
*args
)
1250 if (!PyArg_ParseTuple(args
, "et|si:open", Py_FileSystemDefaultEncoding
,
1251 &name
, &mode
, &bufsize
))
1253 f
= PyFile_FromString(name
, mode
);
1254 PyMem_Free(name
); /* free the encoded string */
1256 PyFile_SetBufSize(f
, bufsize
);
1260 static char open_doc
[] =
1261 "open(filename[, mode[, buffering]]) -> file object\n\
1263 Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
1264 writing or appending. The file will be created if it doesn't exist\n\
1265 when opened for writing or appending; it will be truncated when\n\
1266 opened for writing. Add a 'b' to the mode for binary files.\n\
1267 Add a '+' to the mode to allow simultaneous reading and writing.\n\
1268 If the buffering argument is given, 0 means unbuffered, 1 means line\n\
1269 buffered, and larger numbers specify the buffer size.";
1273 builtin_ord(PyObject
*self
, PyObject
*args
)
1279 if (!PyArg_ParseTuple(args
, "O:ord", &obj
))
1282 if (PyString_Check(obj
)) {
1283 size
= PyString_GET_SIZE(obj
);
1285 ord
= (long)((unsigned char)*PyString_AS_STRING(obj
));
1286 return PyInt_FromLong(ord
);
1288 } else if (PyUnicode_Check(obj
)) {
1289 size
= PyUnicode_GET_SIZE(obj
);
1291 ord
= (long)*PyUnicode_AS_UNICODE(obj
);
1292 return PyInt_FromLong(ord
);
1295 PyErr_Format(PyExc_TypeError
,
1296 "ord() expected string of length 1, but " \
1297 "%.200s found", obj
->ob_type
->tp_name
);
1301 PyErr_Format(PyExc_TypeError
,
1302 "ord() expected a character, "
1303 "but string of length %d found",
1308 static char ord_doc
[] =
1309 "ord(c) -> integer\n\
1311 Return the integer ordinal of a one-character string.";
1315 builtin_pow(PyObject
*self
, PyObject
*args
)
1317 PyObject
*v
, *w
, *z
= Py_None
;
1319 if (!PyArg_ParseTuple(args
, "OO|O:pow", &v
, &w
, &z
))
1321 return PyNumber_Power(v
, w
, z
);
1324 static char pow_doc
[] =
1325 "pow(x, y[, z]) -> number\n\
1327 With two arguments, equivalent to x**y. With three arguments,\n\
1328 equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
1331 /* Return number of items in range/xrange (lo, hi, step). step > 0
1332 * required. Return a value < 0 if & only if the true value is too
1333 * large to fit in a signed long.
1336 get_len_of_range(long lo
, long hi
, long step
)
1338 /* -------------------------------------------------------------
1339 If lo >= hi, the range is empty.
1340 Else if n values are in the range, the last one is
1341 lo + (n-1)*step, which must be <= hi-1. Rearranging,
1342 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
1343 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
1344 the RHS is non-negative and so truncation is the same as the
1345 floor. Letting M be the largest positive long, the worst case
1346 for the RHS numerator is hi=M, lo=-M-1, and then
1347 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
1348 precision to compute the RHS exactly.
1349 ---------------------------------------------------------------*/
1352 unsigned long uhi
= (unsigned long)hi
;
1353 unsigned long ulo
= (unsigned long)lo
;
1354 unsigned long diff
= uhi
- ulo
- 1;
1355 n
= (long)(diff
/ (unsigned long)step
+ 1);
1361 builtin_range(PyObject
*self
, PyObject
*args
)
1363 long ilow
= 0, ihigh
= 0, istep
= 1;
1369 if (PyTuple_Size(args
) <= 1) {
1370 if (!PyArg_ParseTuple(args
,
1371 "l;range() requires 1-3 int arguments",
1376 if (!PyArg_ParseTuple(args
,
1377 "ll|l;range() requires 1-3 int arguments",
1378 &ilow
, &ihigh
, &istep
))
1382 PyErr_SetString(PyExc_ValueError
, "range() arg 3 must not be zero");
1386 bign
= get_len_of_range(ilow
, ihigh
, istep
);
1388 bign
= get_len_of_range(ihigh
, ilow
, -istep
);
1390 if (bign
< 0 || (long)n
!= bign
) {
1391 PyErr_SetString(PyExc_OverflowError
,
1392 "range() result has too many items");
1398 for (i
= 0; i
< n
; i
++) {
1399 PyObject
*w
= PyInt_FromLong(ilow
);
1404 PyList_SET_ITEM(v
, i
, w
);
1410 static char range_doc
[] =
1411 "range([start,] stop[, step]) -> list of integers\n\
1413 Return a list containing an arithmetic progression of integers.\n\
1414 range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1415 When step is given, it specifies the increment (or decrement).\n\
1416 For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1417 These are exactly the valid indices for a list of 4 elements.";
1421 builtin_xrange(PyObject
*self
, PyObject
*args
)
1423 long ilow
= 0, ihigh
= 0, istep
= 1;
1426 if (PyTuple_Size(args
) <= 1) {
1427 if (!PyArg_ParseTuple(args
,
1428 "l;xrange() requires 1-3 int arguments",
1433 if (!PyArg_ParseTuple(args
,
1434 "ll|l;xrange() requires 1-3 int arguments",
1435 &ilow
, &ihigh
, &istep
))
1439 PyErr_SetString(PyExc_ValueError
, "xrange() arg 3 must not be zero");
1443 n
= get_len_of_range(ilow
, ihigh
, istep
);
1445 n
= get_len_of_range(ihigh
, ilow
, -istep
);
1447 PyErr_SetString(PyExc_OverflowError
,
1448 "xrange() result has too many items");
1451 return PyRange_New(ilow
, n
, istep
, 1);
1454 static char xrange_doc
[] =
1455 "xrange([start,] stop[, step]) -> xrange object\n\
1457 Like range(), but instead of returning a list, returns an object that\n\
1458 generates the numbers in the range on demand. This is slightly slower\n\
1459 than range() but more memory efficient.";
1463 builtin_raw_input(PyObject
*self
, PyObject
*args
)
1468 if (!PyArg_ParseTuple(args
, "|O:[raw_]input", &v
))
1470 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin
&&
1471 PyFile_AsFile(PySys_GetObject("stdout")) == stdout
&&
1472 isatty(fileno(stdin
)) && isatty(fileno(stdout
))) {
1478 po
= PyObject_Str(v
);
1481 prompt
= PyString_AsString(po
);
1489 s
= PyOS_Readline(prompt
);
1492 PyErr_SetNone(PyExc_KeyboardInterrupt
);
1496 PyErr_SetNone(PyExc_EOFError
);
1499 else { /* strip trailing '\n' */
1500 size_t len
= strlen(s
);
1501 if (len
> INT_MAX
) {
1502 PyErr_SetString(PyExc_OverflowError
, "input too long");
1506 result
= PyString_FromStringAndSize(s
, (int)(len
-1));
1513 f
= PySys_GetObject("stdout");
1515 PyErr_SetString(PyExc_RuntimeError
, "lost sys.stdout");
1518 if (Py_FlushLine() != 0 ||
1519 PyFile_WriteObject(v
, f
, Py_PRINT_RAW
) != 0)
1522 f
= PySys_GetObject("stdin");
1524 PyErr_SetString(PyExc_RuntimeError
, "lost sys.stdin");
1527 return PyFile_GetLine(f
, -1);
1530 static char raw_input_doc
[] =
1531 "raw_input([prompt]) -> string\n\
1533 Read a string from standard input. The trailing newline is stripped.\n\
1534 If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1535 On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1536 is printed without a trailing newline before reading.";
1540 builtin_reduce(PyObject
*self
, PyObject
*args
)
1542 PyObject
*seq
, *func
, *result
= NULL
, *it
;
1544 if (!PyArg_ParseTuple(args
, "OO|O:reduce", &func
, &seq
, &result
))
1549 it
= PyObject_GetIter(seq
);
1551 PyErr_SetString(PyExc_TypeError
,
1552 "reduce() arg 2 must support iteration");
1557 if ((args
= PyTuple_New(2)) == NULL
)
1563 if (args
->ob_refcnt
> 1) {
1565 if ((args
= PyTuple_New(2)) == NULL
)
1569 op2
= PyIter_Next(it
);
1571 if (PyErr_Occurred())
1579 PyTuple_SetItem(args
, 0, result
);
1580 PyTuple_SetItem(args
, 1, op2
);
1581 if ((result
= PyEval_CallObject(func
, args
)) == NULL
)
1589 PyErr_SetString(PyExc_TypeError
,
1590 "reduce() of empty sequence with no initial value");
1602 static char reduce_doc
[] =
1603 "reduce(function, sequence[, initial]) -> value\n\
1605 Apply a function of two arguments cumulatively to the items of a sequence,\n\
1606 from left to right, so as to reduce the sequence to a single value.\n\
1607 For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
1608 ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
1609 of the sequence in the calculation, and serves as a default when the\n\
1610 sequence is empty.";
1614 builtin_reload(PyObject
*self
, PyObject
*args
)
1618 if (!PyArg_ParseTuple(args
, "O:reload", &v
))
1620 return PyImport_ReloadModule(v
);
1623 static char reload_doc
[] =
1624 "reload(module) -> module\n\
1626 Reload the module. The module must have been successfully imported before.";
1630 builtin_repr(PyObject
*self
, PyObject
*args
)
1634 if (!PyArg_ParseTuple(args
, "O:repr", &v
))
1636 return PyObject_Repr(v
);
1639 static char repr_doc
[] =
1640 "repr(object) -> string\n\
1642 Return the canonical string representation of the object.\n\
1643 For most object types, eval(repr(object)) == object.";
1647 builtin_round(PyObject
*self
, PyObject
*args
)
1654 if (!PyArg_ParseTuple(args
, "d|i:round", &x
, &ndigits
))
1672 return PyFloat_FromDouble(x
);
1675 static char round_doc
[] =
1676 "round(number[, ndigits]) -> floating point number\n\
1678 Round a number to a given precision in decimal digits (default 0 digits).\n\
1679 This always returns a floating point number. Precision may be negative.";
1683 builtin_vars(PyObject
*self
, PyObject
*args
)
1688 if (!PyArg_ParseTuple(args
, "|O:vars", &v
))
1691 d
= PyEval_GetLocals();
1693 if (!PyErr_Occurred())
1694 PyErr_SetString(PyExc_SystemError
,
1701 d
= PyObject_GetAttrString(v
, "__dict__");
1703 PyErr_SetString(PyExc_TypeError
,
1704 "vars() argument must have __dict__ attribute");
1711 static char vars_doc
[] =
1712 "vars([object]) -> dictionary\n\
1714 Without arguments, equivalent to locals().\n\
1715 With an argument, equivalent to object.__dict__.";
1718 builtin_isinstance(PyObject
*self
, PyObject
*args
)
1724 if (!PyArg_ParseTuple(args
, "OO:isinstance", &inst
, &cls
))
1727 retval
= PyObject_IsInstance(inst
, cls
);
1730 return PyInt_FromLong(retval
);
1733 static char isinstance_doc
[] =
1734 "isinstance(object, class-or-type) -> Boolean\n\
1736 Return whether an object is an instance of a class or of a subclass thereof.\n\
1737 With a type as second argument, return whether that is the object's type.";
1741 builtin_issubclass(PyObject
*self
, PyObject
*args
)
1747 if (!PyArg_ParseTuple(args
, "OO:issubclass", &derived
, &cls
))
1750 retval
= PyObject_IsSubclass(derived
, cls
);
1753 return PyInt_FromLong(retval
);
1756 static char issubclass_doc
[] =
1757 "issubclass(C, B) -> Boolean\n\
1759 Return whether class C is a subclass (i.e., a derived class) of class B.";
1763 builtin_zip(PyObject
*self
, PyObject
*args
)
1766 int itemsize
= PySequence_Length(args
);
1768 PyObject
*itlist
; /* tuple of iterators */
1771 PyErr_SetString(PyExc_TypeError
,
1772 "zip() requires at least one sequence");
1775 /* args must be a tuple */
1776 assert(PyTuple_Check(args
));
1778 /* allocate result list */
1779 if ((ret
= PyList_New(0)) == NULL
)
1782 /* obtain iterators */
1783 itlist
= PyTuple_New(itemsize
);
1786 for (i
= 0; i
< itemsize
; ++i
) {
1787 PyObject
*item
= PyTuple_GET_ITEM(args
, i
);
1788 PyObject
*it
= PyObject_GetIter(item
);
1790 if (PyErr_ExceptionMatches(PyExc_TypeError
))
1791 PyErr_Format(PyExc_TypeError
,
1792 "zip argument #%d must support iteration",
1794 goto Fail_ret_itlist
;
1796 PyTuple_SET_ITEM(itlist
, i
, it
);
1799 /* build result into ret list */
1802 PyObject
*next
= PyTuple_New(itemsize
);
1804 goto Fail_ret_itlist
;
1806 for (i
= 0; i
< itemsize
; i
++) {
1807 PyObject
*it
= PyTuple_GET_ITEM(itlist
, i
);
1808 PyObject
*item
= PyIter_Next(it
);
1810 if (PyErr_Occurred()) {
1818 PyTuple_SET_ITEM(next
, i
, item
);
1821 status
= PyList_Append(ret
, next
);
1824 goto Fail_ret_itlist
;
1835 static char zip_doc
[] =
1836 "zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
1838 Return a list of tuples, where each tuple contains the i-th element\n\
1839 from each of the argument sequences. The returned list is truncated\n\
1840 in length to the length of the shortest argument sequence.";
1843 static PyMethodDef builtin_methods
[] = {
1844 {"__import__", builtin___import__
, 1, import_doc
},
1845 {"abs", builtin_abs
, 1, abs_doc
},
1846 {"apply", builtin_apply
, 1, apply_doc
},
1847 {"buffer", builtin_buffer
, 1, buffer_doc
},
1848 {"callable", builtin_callable
, 1, callable_doc
},
1849 {"chr", builtin_chr
, 1, chr_doc
},
1850 {"cmp", builtin_cmp
, 1, cmp_doc
},
1851 {"coerce", builtin_coerce
, 1, coerce_doc
},
1852 {"compile", builtin_compile
, 1, compile_doc
},
1853 {"delattr", builtin_delattr
, 1, delattr_doc
},
1854 {"dir", builtin_dir
, 1, dir_doc
},
1855 {"divmod", builtin_divmod
, 1, divmod_doc
},
1856 {"eval", builtin_eval
, 1, eval_doc
},
1857 {"execfile", builtin_execfile
, 1, execfile_doc
},
1858 {"filter", builtin_filter
, 1, filter_doc
},
1859 {"getattr", builtin_getattr
, 1, getattr_doc
},
1860 {"globals", builtin_globals
, 1, globals_doc
},
1861 {"hasattr", builtin_hasattr
, 1, hasattr_doc
},
1862 {"hash", builtin_hash
, 1, hash_doc
},
1863 {"hex", builtin_hex
, 1, hex_doc
},
1864 {"id", builtin_id
, 1, id_doc
},
1865 {"input", builtin_input
, 1, input_doc
},
1866 {"intern", builtin_intern
, 1, intern_doc
},
1867 {"isinstance", builtin_isinstance
, 1, isinstance_doc
},
1868 {"issubclass", builtin_issubclass
, 1, issubclass_doc
},
1869 {"iter", builtin_iter
, 1, iter_doc
},
1870 {"len", builtin_len
, 1, len_doc
},
1871 {"locals", builtin_locals
, 1, locals_doc
},
1872 {"map", builtin_map
, 1, map_doc
},
1873 {"max", builtin_max
, 1, max_doc
},
1874 {"min", builtin_min
, 1, min_doc
},
1875 {"oct", builtin_oct
, 1, oct_doc
},
1876 {"open", builtin_open
, 1, open_doc
},
1877 {"ord", builtin_ord
, 1, ord_doc
},
1878 {"pow", builtin_pow
, 1, pow_doc
},
1879 {"range", builtin_range
, 1, range_doc
},
1880 {"raw_input", builtin_raw_input
, 1, raw_input_doc
},
1881 {"reduce", builtin_reduce
, 1, reduce_doc
},
1882 {"reload", builtin_reload
, 1, reload_doc
},
1883 {"repr", builtin_repr
, 1, repr_doc
},
1884 {"round", builtin_round
, 1, round_doc
},
1885 {"setattr", builtin_setattr
, 1, setattr_doc
},
1886 {"slice", builtin_slice
, 1, slice_doc
},
1887 {"unichr", builtin_unichr
, 1, unichr_doc
},
1888 {"vars", builtin_vars
, 1, vars_doc
},
1889 {"xrange", builtin_xrange
, 1, xrange_doc
},
1890 {"zip", builtin_zip
, 1, zip_doc
},
1894 static char builtin_doc
[] =
1895 "Built-in functions, exceptions, and other objects.\n\
1897 Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
1900 _PyBuiltin_Init(void)
1902 PyObject
*mod
, *dict
, *debug
;
1903 mod
= Py_InitModule4("__builtin__", builtin_methods
,
1904 builtin_doc
, (PyObject
*)NULL
,
1905 PYTHON_API_VERSION
);
1908 dict
= PyModule_GetDict(mod
);
1909 if (PyDict_SetItemString(dict
, "None", Py_None
) < 0)
1911 if (PyDict_SetItemString(dict
, "Ellipsis", Py_Ellipsis
) < 0)
1913 if (PyDict_SetItemString(dict
, "NotImplemented",
1914 Py_NotImplemented
) < 0)
1916 if (PyDict_SetItemString(dict
, "classmethod",
1917 (PyObject
*) &PyClassMethod_Type
) < 0)
1919 #ifndef WITHOUT_COMPLEX
1920 if (PyDict_SetItemString(dict
, "complex",
1921 (PyObject
*) &PyComplex_Type
) < 0)
1924 if (PyDict_SetItemString(dict
, "dictionary",
1925 (PyObject
*) &PyDict_Type
) < 0)
1927 if (PyDict_SetItemString(dict
, "float",
1928 (PyObject
*) &PyFloat_Type
) < 0)
1930 if (PyDict_SetItemString(dict
, "int", (PyObject
*) &PyInt_Type
) < 0)
1932 if (PyDict_SetItemString(dict
, "list", (PyObject
*) &PyList_Type
) < 0)
1934 if (PyDict_SetItemString(dict
, "long", (PyObject
*) &PyLong_Type
) < 0)
1936 if (PyDict_SetItemString(dict
, "object",
1937 (PyObject
*) &PyBaseObject_Type
) < 0)
1939 if (PyDict_SetItemString(dict
, "staticmethod",
1940 (PyObject
*) &PyStaticMethod_Type
) < 0)
1942 if (PyDict_SetItemString(dict
, "str", (PyObject
*) &PyString_Type
) < 0)
1944 if (PyDict_SetItemString(dict
, "tuple",
1945 (PyObject
*) &PyTuple_Type
) < 0)
1947 if (PyDict_SetItemString(dict
, "type", (PyObject
*) &PyType_Type
) < 0)
1949 if (PyDict_SetItemString(dict
, "unicode",
1950 (PyObject
*) &PyUnicode_Type
) < 0)
1952 debug
= PyInt_FromLong(Py_OptimizeFlag
== 0);
1953 if (PyDict_SetItemString(dict
, "__debug__", debug
) < 0) {
1962 /* Helper for filter(): filter a tuple through a function */
1965 filtertuple(PyObject
*func
, PyObject
*tuple
)
1969 int len
= PyTuple_Size(tuple
);
1976 if ((result
= PyTuple_New(len
)) == NULL
)
1979 for (i
= j
= 0; i
< len
; ++i
) {
1980 PyObject
*item
, *good
;
1983 if ((item
= PyTuple_GetItem(tuple
, i
)) == NULL
)
1985 if (func
== Py_None
) {
1990 PyObject
*arg
= Py_BuildValue("(O)", item
);
1993 good
= PyEval_CallObject(func
, arg
);
1998 ok
= PyObject_IsTrue(good
);
2002 if (PyTuple_SetItem(result
, j
++, item
) < 0)
2007 if (_PyTuple_Resize(&result
, j
) < 0)
2018 /* Helper for filter(): filter a string through a function */
2021 filterstring(PyObject
*func
, PyObject
*strobj
)
2025 int len
= PyString_Size(strobj
);
2027 if (func
== Py_None
) {
2028 /* No character is ever false -- share input string */
2032 if ((result
= PyString_FromStringAndSize(NULL
, len
)) == NULL
)
2035 for (i
= j
= 0; i
< len
; ++i
) {
2036 PyObject
*item
, *arg
, *good
;
2039 item
= (*strobj
->ob_type
->tp_as_sequence
->sq_item
)(strobj
, i
);
2042 arg
= Py_BuildValue("(O)", item
);
2047 good
= PyEval_CallObject(func
, arg
);
2053 ok
= PyObject_IsTrue(good
);
2056 PyString_AS_STRING((PyStringObject
*)result
)[j
++] =
2057 PyString_AS_STRING((PyStringObject
*)item
)[0];
2061 if (j
< len
&& _PyString_Resize(&result
, j
) < 0)