diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-07-02 18:19:47 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-07-02 18:19:47 (GMT) |
commit | 7d6b01417f5e97e122c4fe2237ebd24ea3437d52 (patch) | |
tree | f3e405f25c572084e711555e7fb4cf8292746fcd /Python/bltinmodule.c | |
parent | f92146938377651100db8a00bb3356ceff760037 (diff) | |
download | cpython-7d6b01417f5e97e122c4fe2237ebd24ea3437d52.zip cpython-7d6b01417f5e97e122c4fe2237ebd24ea3437d52.tar.gz cpython-7d6b01417f5e97e122c4fe2237ebd24ea3437d52.tar.bz2 |
Merged revisions 73776 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73776 | benjamin.peterson | 2009-07-02 13:16:45 -0500 (Thu, 02 Jul 2009) | 1 line
when print() gets unicode arguments, sep and end should be unicode by default #4618
........
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 92 |
1 files changed, 74 insertions, 18 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 4d7dec1..015e16b 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1556,14 +1556,40 @@ static PyObject * builtin_print(PyObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"sep", "end", "file", 0}; - static PyObject *dummy_args; + static PyObject *dummy_args = NULL; + static PyObject *unicode_newline = NULL, *unicode_space = NULL; + static PyObject *str_newline = NULL, *str_space = NULL; + PyObject *newline, *space; PyObject *sep = NULL, *end = NULL, *file = NULL; - int i, err; + int i, err, use_unicode = 0; if (dummy_args == NULL) { if (!(dummy_args = PyTuple_New(0))) return NULL; } + if (str_newline == NULL) { + str_newline = PyString_FromString("\n"); + if (str_newline == NULL) + return NULL; + str_space = PyString_FromString(" "); + if (str_space == NULL) { + Py_CLEAR(str_newline); + return NULL; + } + unicode_newline = PyUnicode_FromString("\n"); + if (unicode_newline == NULL) { + Py_CLEAR(str_newline); + Py_CLEAR(str_space); + return NULL; + } + unicode_space = PyUnicode_FromString(" "); + if (unicode_space == NULL) { + Py_CLEAR(str_newline); + Py_CLEAR(str_space); + Py_CLEAR(unicode_space); + return NULL; + } + } if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print", kwlist, &sep, &end, &file)) return NULL; @@ -1573,26 +1599,56 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds) if (file == Py_None) Py_RETURN_NONE; } + if (sep == Py_None) { + sep = NULL; + } + else if (sep) { + if (PyUnicode_Check(sep)) { + use_unicode = 1; + } + else if (!PyString_Check(sep)) { + PyErr_Format(PyExc_TypeError, + "sep must be None, str or unicode, not %.200s", + sep->ob_type->tp_name); + return NULL; + } + } + if (end == Py_None) + end = NULL; + else if (end) { + if (PyUnicode_Check(end)) { + use_unicode = 1; + } + else if (!PyString_Check(end)) { + PyErr_Format(PyExc_TypeError, + "end must be None, str or unicode, not %.200s", + end->ob_type->tp_name); + return NULL; + } + } - if (sep && sep != Py_None && !PyString_Check(sep) && - !PyUnicode_Check(sep)) { - PyErr_Format(PyExc_TypeError, - "sep must be None, str or unicode, not %.200s", - sep->ob_type->tp_name); - return NULL; + if (!use_unicode) { + for (i = 0; i < PyTuple_Size(args); i++) { + if (PyUnicode_Check(PyTuple_GET_ITEM(args, i))) { + use_unicode = 1; + break; + } + } } - if (end && end != Py_None && !PyString_Check(end) && - !PyUnicode_Check(end)) { - PyErr_Format(PyExc_TypeError, - "end must be None, str or unicode, not %.200s", - end->ob_type->tp_name); - return NULL; + if (use_unicode) { + newline = unicode_newline; + space = unicode_space; + } + else { + newline = str_newline; + space = str_space; } for (i = 0; i < PyTuple_Size(args); i++) { if (i > 0) { - if (sep == NULL || sep == Py_None) - err = PyFile_WriteString(" ", file); + if (sep == NULL) + err = PyFile_WriteObject(space, file, + Py_PRINT_RAW); else err = PyFile_WriteObject(sep, file, Py_PRINT_RAW); @@ -1605,8 +1661,8 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds) return NULL; } - if (end == NULL || end == Py_None) - err = PyFile_WriteString("\n", file); + if (end == NULL) + err = PyFile_WriteObject(newline, file, Py_PRINT_RAW); else err = PyFile_WriteObject(end, file, Py_PRINT_RAW); if (err) |