Skip to content

Commit e6be9b5

Browse files
authored
closes bpo-39605: Fix some casts to not cast away const. (pythonGH-18453)
gcc -Wcast-qual turns up a number of instances of casting away constness of pointers. Some of these can be safely modified, by either: Adding the const to the type cast, as in: - return _PyUnicode_FromUCS1((unsigned char*)s, size); + return _PyUnicode_FromUCS1((const unsigned char*)s, size); or, Removing the cast entirely, because it's not necessary (but probably was at one time), as in: - PyDTrace_FUNCTION_ENTRY((char *)filename, (char *)funcname, lineno); + PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); These changes will not change code, but they will make it much easier to check for errors in consts
1 parent 029e840 commit e6be9b5

File tree

11 files changed

+39
-39
lines changed

11 files changed

+39
-39
lines changed

Modules/_io/textio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ find_control_char(int kind, const char *s, const char *end, Py_UCS4 ch)
20252025
{
20262026
if (kind == PyUnicode_1BYTE_KIND) {
20272027
assert(ch < 256);
2028-
return (char *) memchr((void *) s, (char) ch, end - s);
2028+
return (char *) memchr((const void *) s, (char) ch, end - s);
20292029
}
20302030
for (;;) {
20312031
while (PyUnicode_READ(kind, s, 0) > ch)
@@ -2043,7 +2043,7 @@ _PyIO_find_line_ending(
20432043
int translated, int universal, PyObject *readnl,
20442044
int kind, const char *start, const char *end, Py_ssize_t *consumed)
20452045
{
2046-
Py_ssize_t len = ((char*)end - (char*)start)/kind;
2046+
Py_ssize_t len = (end - start)/kind;
20472047

20482048
if (translated) {
20492049
/* Newlines are already translated, only search for \n */

Objects/bytes_methods.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PyObject*
1212
_Py_bytes_isspace(const char *cptr, Py_ssize_t len)
1313
{
1414
const unsigned char *p
15-
= (unsigned char *) cptr;
15+
= (const unsigned char *) cptr;
1616
const unsigned char *e;
1717

1818
/* Shortcut for single character strings */
@@ -42,7 +42,7 @@ PyObject*
4242
_Py_bytes_isalpha(const char *cptr, Py_ssize_t len)
4343
{
4444
const unsigned char *p
45-
= (unsigned char *) cptr;
45+
= (const unsigned char *) cptr;
4646
const unsigned char *e;
4747

4848
/* Shortcut for single character strings */
@@ -72,7 +72,7 @@ PyObject*
7272
_Py_bytes_isalnum(const char *cptr, Py_ssize_t len)
7373
{
7474
const unsigned char *p
75-
= (unsigned char *) cptr;
75+
= (const unsigned char *) cptr;
7676
const unsigned char *e;
7777

7878
/* Shortcut for single character strings */
@@ -123,7 +123,7 @@ _Py_bytes_isascii(const char *cptr, Py_ssize_t len)
123123
/* Help allocation */
124124
const char *_p = p;
125125
while (_p < aligned_end) {
126-
unsigned long value = *(unsigned long *) _p;
126+
unsigned long value = *(const unsigned long *) _p;
127127
if (value & ASCII_CHAR_MASK) {
128128
Py_RETURN_FALSE;
129129
}
@@ -154,7 +154,7 @@ PyObject*
154154
_Py_bytes_isdigit(const char *cptr, Py_ssize_t len)
155155
{
156156
const unsigned char *p
157-
= (unsigned char *) cptr;
157+
= (const unsigned char *) cptr;
158158
const unsigned char *e;
159159

160160
/* Shortcut for single character strings */
@@ -184,7 +184,7 @@ PyObject*
184184
_Py_bytes_islower(const char *cptr, Py_ssize_t len)
185185
{
186186
const unsigned char *p
187-
= (unsigned char *) cptr;
187+
= (const unsigned char *) cptr;
188188
const unsigned char *e;
189189
int cased;
190190

@@ -218,7 +218,7 @@ PyObject*
218218
_Py_bytes_isupper(const char *cptr, Py_ssize_t len)
219219
{
220220
const unsigned char *p
221-
= (unsigned char *) cptr;
221+
= (const unsigned char *) cptr;
222222
const unsigned char *e;
223223
int cased;
224224

@@ -254,7 +254,7 @@ PyObject*
254254
_Py_bytes_istitle(const char *cptr, Py_ssize_t len)
255255
{
256256
const unsigned char *p
257-
= (unsigned char *) cptr;
257+
= (const unsigned char *) cptr;
258258
const unsigned char *e;
259259
int cased, previous_is_cased;
260260

Objects/memoryobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,8 +1682,8 @@ unpack_single(const char *ptr, const char *fmt)
16821682
switch (fmt[0]) {
16831683

16841684
/* signed integers and fast path for 'B' */
1685-
case 'B': uc = *((unsigned char *)ptr); goto convert_uc;
1686-
case 'b': ld = *((signed char *)ptr); goto convert_ld;
1685+
case 'B': uc = *((const unsigned char *)ptr); goto convert_uc;
1686+
case 'b': ld = *((const signed char *)ptr); goto convert_ld;
16871687
case 'h': UNPACK_SINGLE(ld, ptr, short); goto convert_ld;
16881688
case 'i': UNPACK_SINGLE(ld, ptr, int); goto convert_ld;
16891689
case 'l': UNPACK_SINGLE(ld, ptr, long); goto convert_ld;
@@ -2684,8 +2684,8 @@ unpack_cmp(const char *p, const char *q, char fmt,
26842684
switch (fmt) {
26852685

26862686
/* signed integers and fast path for 'B' */
2687-
case 'B': return *((unsigned char *)p) == *((unsigned char *)q);
2688-
case 'b': return *((signed char *)p) == *((signed char *)q);
2687+
case 'B': return *((const unsigned char *)p) == *((const unsigned char *)q);
2688+
case 'b': return *((const signed char *)p) == *((const signed char *)q);
26892689
case 'h': CMP_SINGLE(p, q, short); return equal;
26902690
case 'i': CMP_SINGLE(p, q, int); return equal;
26912691
case 'l': CMP_SINGLE(p, q, long); return equal;

Objects/stringlib/asciilib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
1919
#define STRINGLIB_STR PyUnicode_1BYTE_DATA
2020
#define STRINGLIB_LEN PyUnicode_GET_LENGTH
21-
#define STRINGLIB_NEW(STR,LEN) _PyUnicode_FromASCII((char*)(STR),(LEN))
21+
#define STRINGLIB_NEW(STR,LEN) _PyUnicode_FromASCII((const char*)(STR),(LEN))
2222
#define STRINGLIB_CHECK PyUnicode_Check
2323
#define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact
2424

Objects/stringlib/codecs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ STRINGLIB(utf8_decode)(const char **inptr, const char *end,
4646
/* Read a whole long at a time (either 4 or 8 bytes),
4747
and do a fast unrolled copy if it only contains ASCII
4848
characters. */
49-
unsigned long value = *(unsigned long *) _s;
49+
unsigned long value = *(const unsigned long *) _s;
5050
if (value & ASCII_CHAR_MASK)
5151
break;
5252
#if PY_LITTLE_ENDIAN
@@ -515,7 +515,7 @@ STRINGLIB(utf16_decode)(const unsigned char **inptr, const unsigned char *e,
515515
/* Fast path for runs of in-range non-surrogate chars. */
516516
const unsigned char *_q = q;
517517
while (_q < aligned_end) {
518-
unsigned long block = * (unsigned long *) _q;
518+
unsigned long block = * (const unsigned long *) _q;
519519
if (native_ordering) {
520520
/* Can use buffer directly */
521521
if (block & FAST_CHAR_MASK)

Objects/stringlib/find_max_char.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end)
2828
/* Help register allocation */
2929
const unsigned char *_p = p;
3030
while (_p < aligned_end) {
31-
unsigned long value = *(unsigned long *) _p;
31+
unsigned long value = *(const unsigned long *) _p;
3232
if (value & UCS1_ASCII_CHAR_MASK)
3333
return 255;
3434
_p += SIZEOF_LONG;

Objects/unicodeobject.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ extern "C" {
172172
#define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \
173173
do { \
174174
to_type *_to = (to_type *)(to); \
175-
const from_type *_iter = (from_type *)(begin); \
176-
const from_type *_end = (from_type *)(end); \
175+
const from_type *_iter = (const from_type *)(begin);\
176+
const from_type *_end = (const from_type *)(end);\
177177
Py_ssize_t n = (_end) - (_iter); \
178178
const from_type *_unrolled_end = \
179179
_iter + _Py_SIZE_ROUND_DOWN(n, 4); \
@@ -964,21 +964,21 @@ findchar(const void *s, int kind,
964964
if ((Py_UCS1) ch != ch)
965965
return -1;
966966
if (direction > 0)
967-
return ucs1lib_find_char((Py_UCS1 *) s, size, (Py_UCS1) ch);
967+
return ucs1lib_find_char((const Py_UCS1 *) s, size, (Py_UCS1) ch);
968968
else
969-
return ucs1lib_rfind_char((Py_UCS1 *) s, size, (Py_UCS1) ch);
969+
return ucs1lib_rfind_char((const Py_UCS1 *) s, size, (Py_UCS1) ch);
970970
case PyUnicode_2BYTE_KIND:
971971
if ((Py_UCS2) ch != ch)
972972
return -1;
973973
if (direction > 0)
974-
return ucs2lib_find_char((Py_UCS2 *) s, size, (Py_UCS2) ch);
974+
return ucs2lib_find_char((const Py_UCS2 *) s, size, (Py_UCS2) ch);
975975
else
976-
return ucs2lib_rfind_char((Py_UCS2 *) s, size, (Py_UCS2) ch);
976+
return ucs2lib_rfind_char((const Py_UCS2 *) s, size, (Py_UCS2) ch);
977977
case PyUnicode_4BYTE_KIND:
978978
if (direction > 0)
979-
return ucs4lib_find_char((Py_UCS4 *) s, size, ch);
979+
return ucs4lib_find_char((const Py_UCS4 *) s, size, ch);
980980
else
981-
return ucs4lib_rfind_char((Py_UCS4 *) s, size, ch);
981+
return ucs4lib_rfind_char((const Py_UCS4 *) s, size, ch);
982982
default:
983983
Py_UNREACHABLE();
984984
}
@@ -3420,7 +3420,7 @@ PyUnicode_Decode(const char *s,
34203420

34213421
/* Decode via the codec registry */
34223422
buffer = NULL;
3423-
if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0)
3423+
if (PyBuffer_FillInfo(&info, NULL, (const void *)s, size, 1, PyBUF_FULL_RO) < 0)
34243424
goto onError;
34253425
buffer = PyMemoryView_FromBuffer(&info);
34263426
if (buffer == NULL)
@@ -4921,7 +4921,7 @@ ascii_decode(const char *start, const char *end, Py_UCS1 *dest)
49214921
/* Help allocation */
49224922
const char *_p = p;
49234923
while (_p < aligned_end) {
4924-
unsigned long value = *(unsigned long *) _p;
4924+
unsigned long value = *(const unsigned long *) _p;
49254925
if (value & ASCII_CHAR_MASK)
49264926
break;
49274927
_p += SIZEOF_LONG;
@@ -5472,7 +5472,7 @@ PyUnicode_DecodeUTF32Stateful(const char *s,
54725472
PyObject *errorHandler = NULL;
54735473
PyObject *exc = NULL;
54745474

5475-
q = (unsigned char *)s;
5475+
q = (const unsigned char *)s;
54765476
e = q + size;
54775477

54785478
if (byteorder)
@@ -5797,7 +5797,7 @@ PyUnicode_DecodeUTF16Stateful(const char *s,
57975797
PyObject *exc = NULL;
57985798
const char *encoding;
57995799

5800-
q = (unsigned char *)s;
5800+
q = (const unsigned char *)s;
58015801
e = q + size;
58025802

58035803
if (byteorder)
@@ -6726,7 +6726,7 @@ PyUnicode_DecodeLatin1(const char *s,
67266726
const char *errors)
67276727
{
67286728
/* Latin-1 is equivalent to the first 256 ordinals in Unicode. */
6729-
return _PyUnicode_FromUCS1((unsigned char*)s, size);
6729+
return _PyUnicode_FromUCS1((const unsigned char*)s, size);
67306730
}
67316731

67326732
/* create or adjust a UnicodeEncodeError */
@@ -13803,7 +13803,7 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
1380313803
if (len == -1)
1380413804
len = strlen(ascii);
1380513805

13806-
assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128);
13806+
assert(ucs1lib_find_max_char((const Py_UCS1*)ascii, (const Py_UCS1*)ascii + len) < 128);
1380713807

1380813808
if (writer->buffer == NULL && !writer->overallocate) {
1380913809
PyObject *str;
@@ -13862,7 +13862,7 @@ _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
1386213862
{
1386313863
Py_UCS4 maxchar;
1386413864

13865-
maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len);
13865+
maxchar = ucs1lib_find_max_char((const Py_UCS1*)str, (const Py_UCS1*)str + len);
1386613866
if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1)
1386713867
return -1;
1386813868
unicode_write_cstr(writer->buffer, writer->pos, str, len);

Python/ceval.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5440,7 +5440,7 @@ dtrace_function_entry(PyFrameObject *f)
54405440
funcname = PyUnicode_AsUTF8(f->f_code->co_name);
54415441
lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
54425442

5443-
PyDTrace_FUNCTION_ENTRY((char *)filename, (char *)funcname, lineno);
5443+
PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
54445444
}
54455445

54465446
static void
@@ -5454,7 +5454,7 @@ dtrace_function_return(PyFrameObject *f)
54545454
funcname = PyUnicode_AsUTF8(f->f_code->co_name);
54555455
lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
54565456

5457-
PyDTrace_FUNCTION_RETURN((char *)filename, (char *)funcname, lineno);
5457+
PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
54585458
}
54595459

54605460
/* DTrace equivalent of maybe_call_line_trace. */
@@ -5486,7 +5486,7 @@ maybe_dtrace_line(PyFrameObject *frame,
54865486
co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
54875487
if (!co_name)
54885488
co_name = "?";
5489-
PyDTrace_LINE((char *)co_filename, (char *)co_name, line);
5489+
PyDTrace_LINE(co_filename, co_name, line);
54905490
}
54915491
*instr_prev = frame->f_lasti;
54925492
}

Python/marshal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ r_byte(RFILE *p)
734734
else {
735735
const char *ptr = r_string(1, p);
736736
if (ptr != NULL)
737-
c = *(unsigned char *) ptr;
737+
c = *(const unsigned char *) ptr;
738738
}
739739
return c;
740740
}

Python/pyhash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static PyHash_FuncDef PyHash_Func = {fnv, "fnv", 8 * SIZEOF_PY_HASH_T,
366366
static uint64_t
367367
siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
368368
uint64_t b = (uint64_t)src_sz << 56;
369-
const uint8_t *in = (uint8_t*)src;
369+
const uint8_t *in = (const uint8_t*)src;
370370

371371
uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
372372
uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;

0 commit comments

Comments
 (0)