Index: py_cvs/Include/object.h =================================================================== --- py_cvs.orig/Include/object.h 2005-08-12 11:21:35.000000000 -0600 +++ py_cvs/Include/object.h 2005-08-22 11:21:20.000000000 -0600 @@ -371,7 +371,6 @@ PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int); PyAPI_FUNC(void) _PyObject_Dump(PyObject *); PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); -PyAPI_FUNC(PyObject *) _PyObject_Str(PyObject *); PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *); #ifdef Py_USING_UNICODE PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *); Index: py_cvs/Lib/email/Header.py =================================================================== --- py_cvs.orig/Lib/email/Header.py 2005-08-10 16:40:30.000000000 -0600 +++ py_cvs/Lib/email/Header.py 2005-08-22 11:06:16.000000000 -0600 @@ -58,7 +58,8 @@ occurs (e.g. a base64 decoding exception). """ # If no encoding, just return the header - header = str(header) + if isinstance(header, unicode): + header = header.encode('ascii') if not ecre.search(header): return [(header, None)] decoded = [] Index: py_cvs/Objects/stringobject.c =================================================================== --- py_cvs.orig/Objects/stringobject.c 2005-08-12 11:20:15.000000000 -0600 +++ py_cvs/Objects/stringobject.c 2005-08-22 11:31:46.000000000 -0600 @@ -3320,32 +3320,58 @@ {NULL, NULL} /* sentinel */ }; -static PyObject * -str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); - -static PyObject * -string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +PyObject * +PyString_New(PyObject *v) { - PyObject *x = NULL; - static char *kwlist[] = {"object", 0}; + PyObject *res; + int type_ok; + if (v == NULL) + return PyString_FromString(""); + 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); - if (type != &PyString_Type) - return str_subtype_new(type, args, kwds); - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) + res = (*v->ob_type->tp_str)(v); + if (res == NULL) return NULL; - if (x == NULL) - return PyString_FromString(""); - return PyObject_Str(x); + 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; } static PyObject * str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *tmp, *pnew; + PyObject *x = NULL; + static char *kwlist[] = {"object", 0}; int n; assert(PyType_IsSubtype(type, &PyString_Type)); - tmp = string_new(&PyString_Type, args, kwds); + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) + return NULL; + if (x == NULL) + tmp = PyString_FromString(""); + else + tmp = PyObject_Str(x); if (tmp == NULL) return NULL; assert(PyString_CheckExact(tmp)); @@ -3362,6 +3388,21 @@ } static PyObject * +string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *x = NULL; + static char *kwlist[] = {"object", 0}; + + if (type != &PyString_Type) + return str_subtype_new(type, args, kwds); + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) + return NULL; + if (x == NULL) + return PyString_FromString(""); + return PyString_New(x); +} + +static PyObject * basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyErr_SetString(PyExc_TypeError, @@ -4078,7 +4119,7 @@ break; case 's': #ifdef Py_USING_UNICODE - temp = _PyObject_Str(v); + temp = PyString_New(v); if (temp != NULL && PyUnicode_Check(temp)) { Py_DECREF(temp); fmt = fmt_start; Index: py_cvs/Doc/api/concrete.tex =================================================================== --- py_cvs.orig/Doc/api/concrete.tex 2005-08-22 11:03:39.000000000 -0600 +++ py_cvs/Doc/api/concrete.tex 2005-08-22 11:19:25.000000000 -0600 @@ -617,6 +617,12 @@ exactly two arguments. \end{cfuncdesc} +\begin{cfuncdesc}{PyObject*}{PyString_New}{PyObject *o} + Compute a string representation of object \var{o}. Returns + a string or Unicode instance on success, \NULL{} on failure. This + is the equivalent of the Python expression \samp{str(\var{o})}. +\end{cfuncdesc} + \begin{cfuncdesc}{int}{PyString_Size}{PyObject *string} Returns the length of the string in string object \var{string}. \end{cfuncdesc} Index: py_cvs/Objects/object.c =================================================================== --- py_cvs.orig/Objects/object.c 2005-08-12 11:21:02.000000000 -0600 +++ py_cvs/Objects/object.c 2005-08-22 11:15:29.000000000 -0600 @@ -331,46 +331,9 @@ } PyObject * -_PyObject_Str(PyObject *v) -{ - PyObject *res; - int type_ok; - if (v == NULL) - return PyString_FromString(""); - 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); + PyObject *res = PyString_New(v); if (res == NULL) return NULL; #ifdef Py_USING_UNICODE Index: py_cvs/Include/stringobject.h =================================================================== --- py_cvs.orig/Include/stringobject.h 2005-08-08 15:16:20.000000000 -0600 +++ py_cvs/Include/stringobject.h 2005-08-22 11:20:48.000000000 -0600 @@ -64,6 +64,7 @@ Py_GCC_ATTRIBUTE((format(printf, 1, 0))); PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...) Py_GCC_ATTRIBUTE((format(printf, 1, 2))); +PyAPI_FUNC(PyObject *) PyString_New(PyObject *); PyAPI_FUNC(int) PyString_Size(PyObject *); PyAPI_FUNC(char *) PyString_AsString(PyObject *); PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);