Author: guido.van.rossum
Date: Thu Nov 30 23:13:52 2006
New Revision: 52877

Modified:
   python/branches/p3yk/Python/bltinmodule.c
Log:
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.


Modified: python/branches/p3yk/Python/bltinmodule.c
==============================================================================
--- python/branches/p3yk/Python/bltinmodule.c   (original)
+++ python/branches/p3yk/Python/bltinmodule.c   Thu Nov 30 23:13:52 2006
@@ -1413,6 +1413,60 @@
 
 
 
+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 @@
        {"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},
_______________________________________________
Python-3000-checkins mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000-checkins

Reply via email to