8000 Add built-in Print() function. This is PEP 3105 except for the name; · python/cpython@3434351 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3434351

Browse files
committed
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.
1 parent 6297128 commit 3434351

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Python/bltinmodule.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,60 @@ equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
14131413

14141414

14151415

1416+
static PyObject *
1417+
builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1418+
{
1419+
static char *kwlist[] = {"sep", "end", "file", 0};
1420+
PyObject *dummy_args = PyTuple_New(0);
1421+
PyObject *sep = NULL, *end = NULL, *file = NULL;
1422+
int i, err;
1423+
1424+
if (dummy_args == NULL)
1425+
return NULL;
1426+
if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:Print",
1427+
kwlist, &sep, &end, &file))
1428+
return NULL;
1429+
if (file == NULL || file == Py_None)
1430+
file = PySys_GetObject("stdout");
1431+
1432+
/* XXX Verify that sep and end are None, NULL or strings. */
1433+
1434+
for (i = 0; i < PyTuple_Size(args); i++) {
1435+
if (i > 0) {
1436+
if (sep == NULL || sep == Py_None)
1437+
err = PyFile_WriteString(" ", file);
1438+
else
1439+
err = PyFile_WriteObject(sep, file,
1440+
Py_PRINT_RAW);
1441+
if (err)
1442+
return NULL;
1443+
}
1444+
err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1445+
Py_PRINT_RAW);
1446+
if (err)
1447+
return NULL;
1448+
}
1449+
1450+
if (end == NULL || end == Py_None)
1451+
err = PyFile_WriteString("\n", file);
1452+
else
1453+
err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1454+
if (err)
1455+
return NULL;
1456+
1457+
Py_RETURN_NONE;
1458+
}
1459+
1460+
PyDoc_STRVAR(print_doc,
1461+
"Print(value, ..., file=None, sep=' ', end='\\n')\n\
1462+
\n\
1463+
Prints the values to a stream, or to sys.stdout by default.\n\
1464+
Optional keyword arguments:\n\
1465+
file: a file-like object (stream); defaults to the current sys.stdout.\n\
1466+
sep: string inserted between values, default a space.\n\
1467+
end: string appended after the last value, default a newline.");
1468+
1469+
14161470
/* Return number of items in range (lo, hi, step), when arguments are
14171471
* PyInt or PyLong objects. step > 0 required. Return a value < 0 if
14181472
* & only if the true value is too large to fit in a signed long.
@@ -2014,6 +2068,7 @@ static PyMethodDef builtin_methods[] = {
20142068
{"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
20152069
{"ord", builtin_ord, METH_O, ord_doc},
20162070
{"pow", builtin_pow, METH_VARARGS, pow_doc},
2071+
{"Print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
20172072
{"range", builtin_range, METH_VARARGS, range_doc},
20182073
{"reload", builtin_reload, METH_O, reload_doc},
20192074
{"repr", builtin_repr, METH_O, repr_doc},

0 commit comments

Comments
 (0)
0