From 0ea8abae2b963d32ff8be1b53d1718f4412db4e0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 13 Aug 2026 20:22:56 +0200 Subject: [PATCH 1/2] gh-155742: Get PyBytes_AS_STRING() as `const char*` Use "const char*" type instead of "char*" to store PyBytes_AS_STRING(). Treat immutable bytes as immutable. --- Include/internal/pycore_code.h | 2 +- Modules/_sqlite/blob.c | 2 +- Modules/grpmodule.c | 3 ++- Modules/pwdmodule.c | 3 ++- Modules/socketmodule.c | 2 +- Objects/stringlib/codecs.h | 2 +- Objects/unicodeobject.c | 2 +- Parser/action_helpers.c | 4 ++-- Parser/string_parser.c | 2 +- 9 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h index 32242f89b812e69..293c1ea4414e23e 100644 --- a/Include/internal/pycore_code.h +++ b/Include/internal/pycore_code.h @@ -205,7 +205,7 @@ _PyLocals_GetKind(PyObject *kinds, int i) { assert(PyBytes_Check(kinds)); assert(0 <= i && i < PyBytes_GET_SIZE(kinds)); - char *ptr = PyBytes_AS_STRING(kinds); + const char *ptr = PyBytes_AS_STRING(kinds); return (_PyLocals_Kind)(ptr[i]); } diff --git a/Modules/_sqlite/blob.c b/Modules/_sqlite/blob.c index d81784409e5d91a..cae1806d4ba2840 100644 --- a/Modules/_sqlite/blob.c +++ b/Modules/_sqlite/blob.c @@ -457,7 +457,7 @@ subscript_slice(pysqlite_Blob *self, PyObject *item) } char *res_buf = PyBytesWriter_GetData(writer); - char *blob_buf = PyBytes_AS_STRING(blob); + const char *blob_buf = PyBytes_AS_STRING(blob); for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) { res_buf[i] = blob_buf[j]; } diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c index 32ead2598036146..d9f04c794ab25fa 100644 --- a/Modules/grpmodule.c +++ b/Modules/grpmodule.c @@ -216,7 +216,8 @@ static PyObject * grp_getgrnam_impl(PyObject *module, PyObject *name) /*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/ { - char *buf = NULL, *buf2 = NULL, *name_chars; + char *buf = NULL, *buf2 = NULL; + char *name_chars; int nomem = 0; struct group *p; PyObject *bytes, *retval = NULL; diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c index 4a2b33f8700d101..5fa4013f288c4ad 100644 --- a/Modules/pwdmodule.c +++ b/Modules/pwdmodule.c @@ -226,7 +226,8 @@ static PyObject * pwd_getpwnam_impl(PyObject *module, PyObject *name) /*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/ { - char *buf = NULL, *buf2 = NULL, *name_chars; + char *buf = NULL, *buf2 = NULL; + char *name_chars; int nomem = 0; struct passwd *p; PyObject *bytes, *retval = NULL; diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index f9c77c631b5d2af..66525df2ec4bba2 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -1803,7 +1803,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) (in particular, numeric IP addresses). */ struct maybe_idna { PyObject *obj; - char *buf; + const char *buf; }; static void diff --git a/Objects/stringlib/codecs.h b/Objects/stringlib/codecs.h index 9e53fab842909a0..6ea7ef1c9a92bf4 100644 --- a/Objects/stringlib/codecs.h +++ b/Objects/stringlib/codecs.h @@ -406,7 +406,7 @@ STRINGLIB(utf8_encoder)(PyObject *unicode, writer->overallocate = (newpos < size); } - char *rep_str; + const char *rep_str; Py_ssize_t rep_len; if (PyBytes_Check(rep)) { rep_str = PyBytes_AS_STRING(rep); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 45d61c8b8b765a6..2249280d0af8110 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -7314,7 +7314,7 @@ unicode_encode_ucs1(PyObject *unicode, writer->overallocate = (newpos < size); } - char *rep_str; + const char *rep_str; Py_ssize_t rep_len; if (PyBytes_Check(rep)) { /* Directly copy bytes result to output. */ diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c index 809f3c0a7b270e8..8690dca8331b5ce 100644 --- a/Parser/action_helpers.c +++ b/Parser/action_helpers.c @@ -1481,7 +1481,7 @@ expr_ty _PyPegen_decoded_constant_from_token(Parser* p, Token* tok) { } expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) { - char* bstr = PyBytes_AsString(tok->bytes); + const char* bstr = PyBytes_AsString(tok->bytes); if (bstr == NULL) { return NULL; } @@ -1499,7 +1499,7 @@ expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) { } expr_ty _PyPegen_constant_from_string(Parser* p, Token* tok) { - char* the_str = PyBytes_AsString(tok->bytes); + const char* the_str = PyBytes_AsString(tok->bytes); if (the_str == NULL) { return NULL; } diff --git a/Parser/string_parser.c b/Parser/string_parser.c index b164dfbc81a9339..431d2703d88e2c4 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -70,7 +70,7 @@ warn_invalid_escape_sequence(Parser *p, const char* buffer, const char *first_in char first_quote = 0; if (lineno == t->lineno) { int quote_count = 0; - char* tok = PyBytes_AsString(t->bytes); + const char* tok = PyBytes_AsString(t->bytes); for (int i = 0; i < PyBytes_Size(t->bytes); i++) { if (tok[i] == '\'' || tok[i] == '\"') { if (quote_count == 0) { From f2ab8655fed344ea56376ba2e3265b6f1eaef6cf Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 13 Aug 2026 21:07:54 +0200 Subject: [PATCH 2/2] Revert useless changes --- Modules/grpmodule.c | 3 +-- Modules/pwdmodule.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c index d9f04c794ab25fa..32ead2598036146 100644 --- a/Modules/grpmodule.c +++ b/Modules/grpmodule.c @@ -216,8 +216,7 @@ static PyObject * grp_getgrnam_impl(PyObject *module, PyObject *name) /*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/ { - char *buf = NULL, *buf2 = NULL; - char *name_chars; + char *buf = NULL, *buf2 = NULL, *name_chars; int nomem = 0; struct group *p; PyObject *bytes, *retval = NULL; diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c index 5fa4013f288c4ad..4a2b33f8700d101 100644 --- a/Modules/pwdmodule.c +++ b/Modules/pwdmodule.c @@ -226,8 +226,7 @@ static PyObject * pwd_getpwnam_impl(PyObject *module, PyObject *name) /*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/ { - char *buf = NULL, *buf2 = NULL; - char *name_chars; + char *buf = NULL, *buf2 = NULL, *name_chars; int nomem = 0; struct passwd *p; PyObject *bytes, *retval = NULL;