summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2001-01-17 17:09:53 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2001-01-17 17:09:53 (GMT)
commitad7c98e264bbc9c84e911417c8770f6e95ffb794 (patch)
tree7586edae78b019c6c2db46861da762083e0c3c8a /Objects
parentd5c43065d5aa7bd8b9ec67525bd01d2c09a7759f (diff)
downloadcpython-ad7c98e264bbc9c84e911417c8770f6e95ffb794.zip
cpython-ad7c98e264bbc9c84e911417c8770f6e95ffb794.tar.gz
cpython-ad7c98e264bbc9c84e911417c8770f6e95ffb794.tar.bz2
This patch adds a new builtin unistr() which behaves like str()
except that it always returns Unicode objects. A new C API PyObject_Unicode() is also provided. This closes patch #101664. Written by Marc-Andre Lemburg. Copyright assigned to Guido van Rossum.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/object.c47
-rw-r--r--Objects/unicodeobject.c1
2 files changed, 48 insertions, 0 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 3cad241..20950c1 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -568,6 +568,53 @@ get_inprogress_dict(void)
return inprogress;
}
+PyObject *
+PyObject_Unicode(PyObject *v)
+{
+ PyObject *res;
+
+ if (v == NULL)
+ res = PyString_FromString("<NULL>");
+ else if (PyUnicode_Check(v)) {
+ Py_INCREF(v);
+ return v;
+ }
+ else if (PyString_Check(v))
+ res = v;
+ else if (v->ob_type->tp_str != NULL)
+ res = (*v->ob_type->tp_str)(v);
+ else {
+ PyObject *func;
+ static PyObject *strstr;
+ if (strstr == NULL) {
+ strstr= PyString_InternFromString("__str__");
+ if (strstr == NULL)
+ return NULL;
+ }
+ if (!PyInstance_Check(v) ||
+ (func = PyObject_GetAttr(v, strstr)) == NULL) {
+ PyErr_Clear();
+ res = PyObject_Repr(v);
+ }
+ else {
+ res = PyEval_CallObject(func, (PyObject *)NULL);
+ Py_DECREF(func);
+ }
+ }
+ if (res == NULL)
+ return NULL;
+ if (!PyUnicode_Check(res)) {
+ PyObject* str;
+ str = PyUnicode_FromObject(res);
+ Py_DECREF(res);
+ if (str)
+ res = str;
+ else
+ return NULL;
+ }
+ return res;
+}
+
static PyObject *
make_pair(PyObject *v, PyObject *w)
{
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index a3678d5..c1f3d54 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -413,6 +413,7 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj,
}
else
v = PyUnicode_Decode(s, len, encoding, errors);
+
done:
if (owned) {
Py_DECREF(obj);