diff -r ce6a6cc3765d Doc/c-api/unicode.rst --- a/Doc/c-api/unicode.rst Tue Dec 20 22:52:33 2016 +0800 +++ b/Doc/c-api/unicode.rst Wed Dec 21 11:43:31 2016 +0200 @@ -1058,7 +1058,7 @@ These are the UTF-8 codec APIs: raised by the codec. -.. c:function:: char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size) +.. c:function:: const char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size) Return a pointer to the UTF-8 encoding of the Unicode object, and store the size of the encoded representation (in bytes) in *size*. The @@ -1075,13 +1075,19 @@ These are the UTF-8 codec APIs: .. versionadded:: 3.3 - -.. c:function:: char* PyUnicode_AsUTF8(PyObject *unicode) + .. versionchanged:: 3.7 + The return type is now ``const char *`` rather of ``char *``. + + +.. c:function:: const char* PyUnicode_AsUTF8(PyObject *unicode) As :c:func:`PyUnicode_AsUTF8AndSize`, but does not store the size. .. versionadded:: 3.3 + .. versionchanged:: 3.7 + The return type is now ``const char *`` rather of ``char *``. + .. c:function:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors) diff -r ce6a6cc3765d Doc/whatsnew/3.7.rst --- a/Doc/whatsnew/3.7.rst Tue Dec 20 22:52:33 2016 +0800 +++ b/Doc/whatsnew/3.7.rst Wed Dec 21 11:43:31 2016 +0200 @@ -117,6 +117,10 @@ Build and C API Changes and :c:type:`wrapperbase` are now of type ``const char *`` rather of ``char *``. (Contributed by Serhiy Storchaka in :issue:`28761`.) +* The result of :c:func:`PyUnicode_AsUTF8AndSize` and :c:func:`PyUnicode_AsUTF8` + is now of type ``const char *`` rather of ``char *``. + (Contributed by Serhiy Storchaka in :issue:`28769`.) + Deprecated ========== diff -r ce6a6cc3765d Include/unicodeobject.h --- a/Include/unicodeobject.h Tue Dec 20 22:52:33 2016 +0800 +++ b/Include/unicodeobject.h Wed Dec 21 11:43:31 2016 +0200 @@ -1127,7 +1127,7 @@ PyAPI_FUNC(int) PyUnicode_ClearFreeList( */ #ifndef Py_LIMITED_API -PyAPI_FUNC(char *) PyUnicode_AsUTF8AndSize( +PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize( PyObject *unicode, Py_ssize_t *size); #define _PyUnicode_AsStringAndSize PyUnicode_AsUTF8AndSize @@ -1154,7 +1154,7 @@ PyAPI_FUNC(char *) PyUnicode_AsUTF8AndSi */ #ifndef Py_LIMITED_API -PyAPI_FUNC(char *) PyUnicode_AsUTF8(PyObject *unicode); +PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode); #define _PyUnicode_AsString PyUnicode_AsUTF8 #endif diff -r ce6a6cc3765d Modules/_dbmmodule.c --- a/Modules/_dbmmodule.c Tue Dec 20 22:52:33 2016 +0800 +++ b/Modules/_dbmmodule.c Wed Dec 21 11:43:31 2016 +0200 @@ -239,7 +239,7 @@ dbm_contains(PyObject *self, PyObject *a return -1; } if (PyUnicode_Check(arg)) { - key.dptr = PyUnicode_AsUTF8AndSize(arg, &size); + key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size); key.dsize = size; if (key.dptr == NULL) return -1; diff -r ce6a6cc3765d Modules/_decimal/_decimal.c --- a/Modules/_decimal/_decimal.c Tue Dec 20 22:52:33 2016 +0800 +++ b/Modules/_decimal/_decimal.c Wed Dec 21 11:43:31 2016 +0200 @@ -3186,7 +3186,8 @@ dec_format(PyObject *dec, PyObject *args PyObject *fmtarg; PyObject *context; mpd_spec_t spec; - char *fmt; + const char *fmt; + char *fmt_copy = NULL; char *decstring = NULL; uint32_t status = 0; int replace_fillchar = 0; @@ -3207,11 +3208,11 @@ dec_format(PyObject *dec, PyObject *args /* NUL fill character: must be replaced with a valid UTF-8 char before calling mpd_parse_fmt_str(). */ replace_fillchar = 1; - fmt = dec_strdup(fmt, size); - if (fmt == NULL) { + fmt = fmt_copy = dec_strdup(fmt, size); + if (fmt_copy == NULL) { return NULL; } - fmt[0] = '_'; + fmt_copy[0] = '_'; } } else { @@ -3313,7 +3314,7 @@ finish: Py_XDECREF(grouping); Py_XDECREF(sep); Py_XDECREF(dot); - if (replace_fillchar) PyMem_Free(fmt); + if (fmt_copy) PyMem_Free(fmt_copy); if (decstring) mpd_free(decstring); return result; } diff -r ce6a6cc3765d Modules/_gdbmmodule.c --- a/Modules/_gdbmmodule.c Tue Dec 20 22:52:33 2016 +0800 +++ b/Modules/_gdbmmodule.c Wed Dec 21 11:43:31 2016 +0200 @@ -319,7 +319,7 @@ dbm_contains(PyObject *self, PyObject *a return -1; } if (PyUnicode_Check(arg)) { - key.dptr = PyUnicode_AsUTF8AndSize(arg, &size); + key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size); key.dsize = size; if (key.dptr == NULL) return -1; diff -r ce6a6cc3765d Objects/object.c --- a/Objects/object.c Tue Dec 20 22:52:33 2016 +0800 +++ b/Objects/object.c Wed Dec 21 11:43:31 2016 +0200 @@ -890,10 +890,10 @@ PyObject_GetAttr(PyObject *v, PyObject * if (tp->tp_getattro != NULL) return (*tp->tp_getattro)(v, name); if (tp->tp_getattr != NULL) { - char *name_str = PyUnicode_AsUTF8(name); + const char *name_str = PyUnicode_AsUTF8(name); if (name_str == NULL) return NULL; - return (*tp->tp_getattr)(v, name_str); + return (*tp->tp_getattr)(v, (char *)name_str); } PyErr_Format(PyExc_AttributeError, "'%.50s' object has no attribute '%U'", @@ -934,10 +934,10 @@ PyObject_SetAttr(PyObject *v, PyObject * return err; } if (tp->tp_setattr != NULL) { - char *name_str = PyUnicode_AsUTF8(name); + const char *name_str = PyUnicode_AsUTF8(name); if (name_str == NULL) return -1; - err = (*tp->tp_setattr)(v, name_str, value); + err = (*tp->tp_setattr)(v, (char *)name_str, value); Py_DECREF(name); return err; } diff -r ce6a6cc3765d Objects/unicodeobject.c --- a/Objects/unicodeobject.c Tue Dec 20 22:52:33 2016 +0800 +++ b/Objects/unicodeobject.c Wed Dec 21 11:43:31 2016 +0200 @@ -3956,7 +3956,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* } -char* +const char * PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) { PyObject *bytes; @@ -3991,7 +3991,7 @@ PyUnicode_AsUTF8AndSize(PyObject *unicod return PyUnicode_UTF8(unicode); } -char* +const char * PyUnicode_AsUTF8(PyObject *unicode) { return PyUnicode_AsUTF8AndSize(unicode, NULL);