summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorNeil Schemenauer <nascheme@enme.ucalgary.ca>2005-08-12 17:34:58 (GMT)
committerNeil Schemenauer <nascheme@enme.ucalgary.ca>2005-08-12 17:34:58 (GMT)
commitcf52c0784364c09818ffd3fcaabffec667dba01d (patch)
tree6376fa09a2c4ba2ff6db8a3a3d6445d55fba5824 /Objects
parentba7d95e21588c649a262e30103a33a237cc142a0 (diff)
downloadcpython-cf52c0784364c09818ffd3fcaabffec667dba01d.zip
cpython-cf52c0784364c09818ffd3fcaabffec667dba01d.tar.gz
cpython-cf52c0784364c09818ffd3fcaabffec667dba01d.tar.bz2
Change the %s format specifier for str objects so that it returns a
unicode instance if the argument is not an instance of basestring and calling __str__ on the argument returns a unicode instance.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/object.c38
-rw-r--r--Objects/stringobject.c12
2 files changed, 33 insertions, 17 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 975c967..1895697 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -331,22 +331,48 @@ PyObject_Repr(PyObject *v)
}
PyObject *
-PyObject_Str(PyObject *v)
+_PyObject_Str(PyObject *v)
{
PyObject *res;
-
+ int type_ok;
if (v == NULL)
return PyString_FromString("<NULL>");
if (PyString_CheckExact(v)) {
Py_INCREF(v);
return v;
}
+#ifdef Py_USING_UNICODE
+ if (PyUnicode_CheckExact(v)) {
+ Py_INCREF(v);
+ return v;
+ }
+#endif
if (v->ob_type->tp_str == NULL)
return PyObject_Repr(v);
res = (*v->ob_type->tp_str)(v);
if (res == NULL)
return NULL;
+ type_ok = PyString_Check(res);
+#ifdef Py_USING_UNICODE
+ type_ok = type_ok || PyUnicode_Check(res);
+#endif
+ if (!type_ok) {
+ PyErr_Format(PyExc_TypeError,
+ "__str__ returned non-string (type %.200s)",
+ res->ob_type->tp_name);
+ Py_DECREF(res);
+ return NULL;
+ }
+ return res;
+}
+
+PyObject *
+PyObject_Str(PyObject *v)
+{
+ PyObject *res = _PyObject_Str(v);
+ if (res == NULL)
+ return NULL;
#ifdef Py_USING_UNICODE
if (PyUnicode_Check(res)) {
PyObject* str;
@@ -358,13 +384,7 @@ PyObject_Str(PyObject *v)
return NULL;
}
#endif
- if (!PyString_Check(res)) {
- PyErr_Format(PyExc_TypeError,
- "__str__ returned non-string (type %.200s)",
- res->ob_type->tp_name);
- Py_DECREF(res);
- return NULL;
- }
+ assert(PyString_Check(res));
return res;
}
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 8a9dc52..9bcae0f 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -3853,7 +3853,6 @@ formatchar(char *buf, size_t buflen, PyObject *v)
return 1;
}
-
/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
FORMATBUFLEN is the length of the buffer in which the floats, ints, &
@@ -4079,7 +4078,9 @@ PyString_Format(PyObject *format, PyObject *args)
break;
case 's':
#ifdef Py_USING_UNICODE
- if (PyUnicode_Check(v)) {
+ temp = _PyObject_Str(v);
+ if (temp != NULL && PyUnicode_Check(temp)) {
+ Py_DECREF(temp);
fmt = fmt_start;
argidx = argidx_start;
goto unicode;
@@ -4087,16 +4088,11 @@ PyString_Format(PyObject *format, PyObject *args)
#endif
/* Fall through */
case 'r':
- if (c == 's')
- temp = PyObject_Str(v);
- else
+ if (c == 'r')
temp = PyObject_Repr(v);
if (temp == NULL)
goto error;
if (!PyString_Check(temp)) {
- /* XXX Note: this should never happen,
- since PyObject_Repr() and
- PyObject_Str() assure this */
PyErr_SetString(PyExc_TypeError,
"%s argument has non-string str()");
Py_DECREF(temp);