gh-155742: Get PyBytes_AS_STRING() as `const char*` (#155751)

vstinner <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/079dd5ee39815e330b157369d74830403a11e343
commit: 079dd5ee39815e330b157369d74830403a11e343
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-08-14T12:00:45Z
summary:

gh-155742: Get PyBytes_AS_STRING() as `const char*` (#155751)

Use "const char*" type instead of "char*" to store
PyBytes_AS_STRING(): treat bytes objects as immutable.

files:
M Include/internal/pycore_code.h
M Modules/_sqlite/blob.c
M Modules/socketmodule.c
M Objects/stringlib/codecs.h
M Objects/unicodeobject.c
M Parser/action_helpers.c
M Parser/string_parser.c

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 a9a98b40bf60b8f..43cee9e0f308df7 100644
--- a/Modules/_sqlite/blob.c
+++ b/Modules/_sqlite/blob.c
@@ -472,7 +472,7 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
         return NULL;
     }
     char *res_buf = PyBytesWriter_GetData(writer);
-    char *blob_buf = PyBytes_AS_STRING(blob);
+    const char *blob_buf = PyBytes_AS_STRING(blob);
 
     size_t cur;
     Py_ssize_t i;
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index a01569017937620..9051420179aea11 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1811,7 +1811,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) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.