Skip to content

Commit 079dd5e

Browse files
authored
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.
1 parent a0e77f2 commit 079dd5e

7 files changed

Lines changed: 8 additions & 8 deletions

File tree

Include/internal/pycore_code.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ _PyLocals_GetKind(PyObject *kinds, int i)
205205
{
206206
assert(PyBytes_Check(kinds));
207207
assert(0 <= i && i < PyBytes_GET_SIZE(kinds));
208-
char *ptr = PyBytes_AS_STRING(kinds);
208+
const char *ptr = PyBytes_AS_STRING(kinds);
209209
return (_PyLocals_Kind)(ptr[i]);
210210
}
211211

Modules/_sqlite/blob.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
472472
return NULL;
473473
}
474474
char *res_buf = PyBytesWriter_GetData(writer);
475-
char *blob_buf = PyBytes_AS_STRING(blob);
475+
const char *blob_buf = PyBytes_AS_STRING(blob);
476476

477477
size_t cur;
478478
Py_ssize_t i;

Modules/socketmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
18111811
(in particular, numeric IP addresses). */
18121812
struct maybe_idna {
18131813
PyObject *obj;
1814-
char *buf;
1814+
const char *buf;
18151815
};
18161816

18171817
static void

Objects/stringlib/codecs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
406406
writer->overallocate = (newpos < size);
407407
}
408408

409-
char *rep_str;
409+
const char *rep_str;
410410
Py_ssize_t rep_len;
411411
if (PyBytes_Check(rep)) {
412412
rep_str = PyBytes_AS_STRING(rep);

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7314,7 +7314,7 @@ unicode_encode_ucs1(PyObject *unicode,
73147314
writer->overallocate = (newpos < size);
73157315
}
73167316

7317-
char *rep_str;
7317+
const char *rep_str;
73187318
Py_ssize_t rep_len;
73197319
if (PyBytes_Check(rep)) {
73207320
/* Directly copy bytes result to output. */

Parser/action_helpers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ expr_ty _PyPegen_decoded_constant_from_token(Parser* p, Token* tok) {
14811481
}
14821482

14831483
expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) {
1484-
char* bstr = PyBytes_AsString(tok->bytes);
1484+
const char* bstr = PyBytes_AsString(tok->bytes);
14851485
if (bstr == NULL) {
14861486
return NULL;
14871487
}
@@ -1499,7 +1499,7 @@ expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) {
14991499
}
15001500

15011501
expr_ty _PyPegen_constant_from_string(Parser* p, Token* tok) {
1502-
char* the_str = PyBytes_AsString(tok->bytes);
1502+
const char* the_str = PyBytes_AsString(tok->bytes);
15031503
if (the_str == NULL) {
15041504
return NULL;
15051505
}

Parser/string_parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ warn_invalid_escape_sequence(Parser *p, const char* buffer, const char *first_in
7070
char first_quote = 0;
7171
if (lineno == t->lineno) {
7272
int quote_count = 0;
73-
char* tok = PyBytes_AsString(t->bytes);
73+
const char* tok = PyBytes_AsString(t->bytes);
7474
for (int i = 0; i < PyBytes_Size(t->bytes); i++) {
7575
if (tok[i] == '\'' || tok[i] == '\"') {
7676
if (quote_count == 0) {

0 commit comments

Comments
 (0)