Index: Objects/unicodeobject.c =================================================================== --- Objects/unicodeobject.c (révision 78666) +++ Objects/unicodeobject.c (copie de travail) @@ -1682,6 +1682,19 @@ return _PyUnicode_AsStringAndSize(unicode, NULL); } +char* +_PyUnicode_AsStringOrDefault(PyObject *unicode, char *default_str) +{ + char *str; + str = _PyUnicode_AsStringAndSize(unicode, NULL); + if (str == NULL) { + PyErr_Clear(); + return default_str; + } else { + return str; + } +} + Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) { if (!PyUnicode_Check(unicode)) { Index: Include/unicodeobject.h =================================================================== --- Include/unicodeobject.h (révision 78666) +++ Include/unicodeobject.h (copie de travail) @@ -713,7 +713,7 @@ PyObject *unicode, Py_ssize_t *size); -/* Returns a pointer to the default encoding (normally, UTf-8) of the +/* Returns a pointer to the default encoding (normally, UTF-8) of the Unicode object unicode. Use of this API is DEPRECATED since no size information can be @@ -729,6 +729,21 @@ PyAPI_FUNC(char *) _PyUnicode_AsString(PyObject *unicode); +/* Returns a pointer to the default encoding (normally, UTF-8) of the + Unicode object unicode, or 'default_str' on unicode error. + + *** This API is for interpreter INTERNAL USE ONLY and will likely + *** be removed or changed for Python 3.1. + + *** If you need to access the Unicode object as UTF-8 bytes string, + *** please use PyUnicode_AsUTF8String() instead. + +*/ + +PyAPI_FUNC(char *) _PyUnicode_AsStringOrDefault( + PyObject *unicode, + char *default_str); + /* Returns the currently active default encoding. The default encoding is currently implemented as run-time settable