Skip to content

Commit 0ea8aba

Browse files
committed
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.
1 parent cd98657 commit 0ea8aba

9 files changed

Lines changed: 12 additions & 10 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
@@ -457,7 +457,7 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
457457
}
458458
char *res_buf = PyBytesWriter_GetData(writer);
459459

460-
char *blob_buf = PyBytes_AS_STRING(blob);
460+
const char *blob_buf = PyBytes_AS_STRING(blob);
461461
for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) {
462462
res_buf[i] = blob_buf[j];
463463
}

Modules/grpmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ static PyObject *
216216
grp_getgrnam_impl(PyObject *module, PyObject *name)
217217
/*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/
218218
{
219-
char *buf = NULL, *buf2 = NULL, *name_chars;
219+
char *buf = NULL, *buf2 = NULL;
220+
char *name_chars;
220221
int nomem = 0;
221222
struct group *p;
222223
PyObject *bytes, *retval = NULL;

Modules/pwdmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ static PyObject *
226226
pwd_getpwnam_impl(PyObject *module, PyObject *name)
227227
/*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/
228228
{
229-
char *buf = NULL, *buf2 = NULL, *name_chars;
229+
char *buf = NULL, *buf2 = NULL;
230+
char *name_chars;
230231
int nomem = 0;
231232
struct passwd *p;
232233
PyObject *bytes, *retval = NULL;

Modules/socketmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
18031803
(in particular, numeric IP addresses). */
18041804
struct maybe_idna {
18051805
PyObject *obj;
1806-
char *buf;
1806+
const char *buf;
18071807
};
18081808

18091809
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)