Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 2a404b6

Browse files
Issue python#28769: The result of PyUnicode_AsUTF8AndSize() and PyUnicode_AsUTF8()
is now of type "const char *" rather of "char *".
1 parent d528791 commit 2a404b6

File tree

9 files changed

+26
-13
lines changed

9 files changed

+26
-13
lines changed

Doc/c-api/unicode.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ These are the UTF-8 codec APIs:
10381038
raised by the codec.
10391039
10401040
1041-
.. c:function:: char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size)
1041+
.. c:function:: const char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size)
10421042
10431043
Return a pointer to the UTF-8 encoding of the Unicode object, and
10441044
store the size of the encoded representation (in bytes) in *size*. The
@@ -1055,13 +1055,19 @@ These are the UTF-8 codec APIs:
10551055
10561056
.. versionadded:: 3.3
10571057
1058+
.. versionchanged:: 3.7
1059+
The return type is now ``const char *`` rather of ``char *``.
1060+
10581061
1059-
.. c:function:: char* PyUnicode_AsUTF8(PyObject *unicode)
1062+
.. c:function:: const char* PyUnicode_AsUTF8(PyObject *unicode)
10601063
10611064
As :c:func:`PyUnicode_AsUTF8AndSize`, but does not store the size.
10621065
10631066
.. versionadded:: 3.3
10641067
1068+
.. versionchanged:: 3.7
1069+
The return type is now ``const char *`` rather of ``char *``.
1070+
10651071
10661072
.. c:function:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
10671073

Doc/whatsnew/3.7.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ Build and C API Changes
124124
and :c:type:`wrapperbase` are now of type ``const char *`` rather of
125125
``char *``. (Contributed by Serhiy Storchaka in :issue:`28761`.)
126126

127+
* The result of :c:func:`PyUnicode_AsUTF8AndSize` and :c:func:`PyUnicode_AsUTF8`
128+
is now of type ``const char *`` rather of ``char *``.
129+
(Contributed by Serhiy Storchaka in :issue:`28769`.)
130+
127131

128132
Deprecated
129133
==========

Include/unicodeobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ PyAPI_FUNC(int) PyUnicode_ClearFreeList(void);
11351135
*/
11361136

11371137
#ifndef Py_LIMITED_API
1138-
PyAPI_FUNC(char *) PyUnicode_AsUTF8AndSize(
1138+
PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
11391139
PyObject *unicode,
11401140
Py_ssize_t *size);
11411141
#define _PyUnicode_AsStringAndSize PyUnicode_AsUTF8AndSize
@@ -1162,7 +1162,7 @@ PyAPI_FUNC(char *) PyUnicode_AsUTF8AndSize(
11621162
*/
11631163

11641164
#ifndef Py_LIMITED_API
1165-
PyAPI_FUNC(char *) PyUnicode_AsUTF8(PyObject *unicode);
1165+
PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
11661166
#define _PyUnicode_AsString PyUnicode_AsUTF8
11671167
#endif
11681168

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ Windows
614614
C API
615615
-----
616616

617+
- Issue #28769: The result of PyUnicode_AsUTF8AndSize() and PyUnicode_AsUTF8()
618+
is now of type "const char *" rather of "char *".
619+
617620
- Issue #29058: All stable API extensions added after Python 3.2 are now
618621
available only when Py_LIMITED_API is set to the PY_VERSION_HEX value of
619622
the minimum Python version supporting this API.

Modules/_dbmmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ dbm_contains(PyObject *self, PyObject *arg)
239239
return -1;
240240
}
241241
if (PyUnicode_Check(arg)) {
242-
key.dptr = PyUnicode_AsUTF8AndSize(arg, &size);
242+
key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size);
243243
key.dsize = size;
244244
if (key.dptr == NULL)
245245
return -1;

Modules/_decimal/_decimal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3199,7 +3199,7 @@ dec_format(PyObject *dec, PyObject *args)
31993199
}
32003200

32013201
if (PyUnicode_Check(fmtarg)) {
3202-
fmt = PyUnicode_AsUTF8AndSize(fmtarg, &size);
3202+
fmt = (char *)PyUnicode_AsUTF8AndSize(fmtarg, &size);
32033203
if (fmt == NULL) {
32043204
return NULL;
32053205
}

Modules/_gdbmmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ dbm_contains(PyObject *self, PyObject *arg)
319319
return -1;
320320
}
321321
if (PyUnicode_Check(arg)) {
322-
key.dptr = PyUnicode_AsUTF8AndSize(arg, &size);
322+
key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size);
323323
key.dsize = size;
324324
if (key.dptr == NULL)
325325
return -1;

Objects/object.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,10 @@ PyObject_GetAttr(PyObject *v, PyObject *name)
890890
if (tp->tp_getattro != NULL)
891891
return (*tp->tp_getattro)(v, name);
892892
if (tp->tp_getattr != NULL) {
893-
char *name_str = PyUnicode_AsUTF8(name);
893+
const char *name_str = PyUnicode_AsUTF8(name);
894894
if (name_str == NULL)
895895
return NULL;
896-
return (*tp->tp_getattr)(v, name_str);
896+
return (*tp->tp_getattr)(v, (char *)name_str);
897897
}
898898
PyErr_Format(PyExc_AttributeError,
899899
"'%.50s' object has no attribute '%U'",
@@ -934,10 +934,10 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
934934
return err;
935935
}
936936
if (tp->tp_setattr != NULL) {
937-
char *name_str = PyUnicode_AsUTF8(name);
937+
const char *name_str = PyUnicode_AsUTF8(name);
938938
if (name_str == NULL)
939939
return -1;
940-
err = (*tp->tp_setattr)(v, name_str, value);
940+
err = (*tp->tp_setattr)(v, (char *)name_str, value);
941941
Py_DECREF(name);
942942
return err;
943943
}

Objects/unicodeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,7 +3972,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
39723972
}
39733973

39743974

3975-
char*
3975+
const char *
39763976
PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
39773977
{
39783978
PyObject *bytes;
@@ -4007,7 +4007,7 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
40074007
return PyUnicode_UTF8(unicode);
40084008
}
40094009

4010-
char*
4010+
const char *
40114011
PyUnicode_AsUTF8(PyObject *unicode)
40124012
{
40134013
return PyUnicode_AsUTF8AndSize(unicode, NULL);

0 commit comments

Comments
 (0)