[Python-checkins] CVS: python/dist/src/Objects unicodeobject.c,2.114,2.115
M.-A. Lemburg
lemburg@users.sourceforge.net
Thu, 20 Sep 2001 05:53:18 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv12271/Objects
Modified Files:
unicodeobject.c
Log Message:
Implement the changes proposed in patch #413333. unicode(obj) now
works just like str(obj) in that it tries __str__/tp_str on the object
in case it finds that the object is not a string or buffer.
Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.114
retrieving revision 2.115
diff -C2 -d -r2.114 -r2.115
*** unicodeobject.c 2001/09/20 10:35:46 2.114
--- unicodeobject.c 2001/09/20 12:53:16 2.115
***************
*** 399,406 ****
const char *errors)
{
! const char *s;
int len;
int owned = 0;
PyObject *v;
if (obj == NULL) {
--- 399,407 ----
const char *errors)
{
! const char *s = NULL;
int len;
int owned = 0;
PyObject *v;
+ int reclevel;
if (obj == NULL) {
***************
*** 410,460 ****
/* Coerce object */
! if (PyInstance_Check(obj)) {
! PyObject *func;
! func = PyObject_GetAttrString(obj, "__str__");
! if (func == NULL) {
! PyErr_SetString(PyExc_TypeError,
! "coercing to Unicode: instance doesn't define __str__");
! return NULL;
}
! obj = PyEval_CallObject(func, NULL);
! Py_DECREF(func);
! if (obj == NULL)
! return NULL;
! owned = 1;
! }
! if (PyUnicode_Check(obj)) {
! if (encoding) {
! PyErr_SetString(PyExc_TypeError,
! "decoding Unicode is not supported");
! return NULL;
}
! if (PyUnicode_CheckExact(obj)) {
! Py_INCREF(obj);
! v = obj;
}
- else {
- /* For a subclass of unicode, return a true unicode object
- with the same string value. */
- v = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
- PyUnicode_GET_SIZE(obj));
- }
- goto done;
- }
- else if (PyString_Check(obj)) {
- s = PyString_AS_STRING(obj);
- len = PyString_GET_SIZE(obj);
}
! else if (PyObject_AsCharBuffer(obj, &s, &len)) {
! /* Overwrite the error message with something more useful in
! case of a TypeError. */
! if (PyErr_ExceptionMatches(PyExc_TypeError))
! PyErr_Format(PyExc_TypeError,
! "coercing to Unicode: need string or buffer, "
! "%.80s found",
! obj->ob_type->tp_name);
goto onError;
}
!
/* Convert to Unicode */
if (len == 0) {
--- 411,473 ----
/* Coerce object */
! for (reclevel = 0; reclevel < 2; reclevel++) {
!
! if (PyUnicode_Check(obj)) {
! if (encoding) {
! PyErr_SetString(PyExc_TypeError,
! "decoding Unicode is not supported");
! goto onError;
! }
! if (PyUnicode_CheckExact(obj)) {
! Py_INCREF(obj);
! v = obj;
! }
! else {
! /* For a subclass of unicode, return a true unicode object
! with the same string value. */
! v = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
! PyUnicode_GET_SIZE(obj));
! }
! goto done;
}
! else if (PyString_Check(obj)) {
! s = PyString_AS_STRING(obj);
! len = PyString_GET_SIZE(obj);
! break;
}
! else {
! PyObject *w;
!
! /* Try char buffer interface */
! if (PyObject_AsCharBuffer(obj, &s, &len))
! PyErr_Clear();
! else
! break;
!
! /* Mimic the behaviour of str(object) if everything else
! fails (see PyObject_Str()); this also covers instances
! which implement __str__. */
! if (obj->ob_type->tp_str == NULL)
! w = PyObject_Repr(obj);
! else
! w = (*obj->ob_type->tp_str)(obj);
! if (w == NULL)
! goto onError;
! if (owned) {
! Py_DECREF(obj);
! }
! obj = w;
! owned = 1;
}
}
!
! if (s == NULL) {
! PyErr_Format(PyExc_TypeError,
! "coercing to Unicode: __str__ recursion limit exceeded "
! "(last type: %.80s)",
! obj->ob_type->tp_name);
goto onError;
}
!
/* Convert to Unicode */
if (len == 0) {