Add built-in Print() function. This is PEP 3105 except for the name;

I'll rename it to print() later.

Now I can start working on the refactoring tool for print -> Print().

Also, sep and end should be required to be strings (or Unicode?).
Someone please volunteer.
This commit is contained in:
Guido van Rossum 2006-11-30 22:13:52 +00:00
parent 629712827b
commit 343435146a

View file

@ -1413,6 +1413,60 @@ equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
static PyObject *
builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"sep", "end", "file", 0};
PyObject *dummy_args = PyTuple_New(0);
PyObject *sep = NULL, *end = NULL, *file = NULL;
int i, err;
if (dummy_args == NULL)
return NULL;
if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:Print",
kwlist, &sep, &end, &file))
return NULL;
if (file == NULL || file == Py_None)
file = PySys_GetObject("stdout");
/* XXX Verify that sep and end are None, NULL or strings. */
for (i = 0; i < PyTuple_Size(args); i++) {
if (i > 0) {
if (sep == NULL || sep == Py_None)
err = PyFile_WriteString(" ", file);
else
err = PyFile_WriteObject(sep, file,
Py_PRINT_RAW);
if (err)
return NULL;
}
err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
Py_PRINT_RAW);
if (err)
return NULL;
}
if (end == NULL || end == Py_None)
err = PyFile_WriteString("\n", file);
else
err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
if (err)
return NULL;
Py_RETURN_NONE;
}
PyDoc_STRVAR(print_doc,
"Print(value, ..., file=None, sep=' ', end='\\n')\n\
\n\
Prints the values to a stream, or to sys.stdout by default.\n\
Optional keyword arguments:\n\
file: a file-like object (stream); defaults to the current sys.stdout.\n\
sep: string inserted between values, default a space.\n\
end: string appended after the last value, default a newline.");
/* Return number of items in range (lo, hi, step), when arguments are
* PyInt or PyLong objects. step > 0 required. Return a value < 0 if
* & only if the true value is too large to fit in a signed long.
@ -2014,6 +2068,7 @@ static PyMethodDef builtin_methods[] = {
{"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
{"ord", builtin_ord, METH_O, ord_doc},
{"pow", builtin_pow, METH_VARARGS, pow_doc},
{"Print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
{"range", builtin_range, METH_VARARGS, range_doc},
{"reload", builtin_reload, METH_O, reload_doc},
{"repr", builtin_repr, METH_O, repr_doc},