Skip to content

Commit ec08aa1

Browse files
authored
gh-124064: Fix -Wconversion warnings in pycore_{long,object}.h (#124177)
Change also the fix for pycore_gc.h and pycore_stackref.h: declare constants as uintptr_t, rather than casting constants.
1 parent ab80c6b commit ec08aa1

File tree

6 files changed

+19
-24
lines changed

6 files changed

+19
-24
lines changed

Include/internal/pycore_gc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ static inline void _PyObject_GC_SET_SHARED_INLINE(PyObject *op) {
140140

141141
/* Bit flags for _gc_prev */
142142
/* Bit 0 is set when tp_finalize is called */
143-
#define _PyGC_PREV_MASK_FINALIZED 1
143+
#define _PyGC_PREV_MASK_FINALIZED ((uintptr_t)1)
144144
/* Bit 1 is set when the object is in generation which is GCed currently. */
145-
#define _PyGC_PREV_MASK_COLLECTING 2
145+
#define _PyGC_PREV_MASK_COLLECTING ((uintptr_t)2)
146146

147147
/* Bit 0 in _gc_next is the old space bit.
148148
* It is set as follows:
@@ -227,7 +227,7 @@ static inline void _PyGC_CLEAR_FINALIZED(PyObject *op) {
227227
_PyObject_CLEAR_GC_BITS(op, _PyGC_BITS_FINALIZED);
228228
#else
229229
PyGC_Head *gc = _Py_AS_GC(op);
230-
gc->_gc_prev &= ~(uintptr_t)_PyGC_PREV_MASK_FINALIZED;
230+
gc->_gc_prev &= ~_PyGC_PREV_MASK_FINALIZED;
231231
#endif
232232
}
233233

Include/internal/pycore_long.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ static inline Py_ssize_t
228228
_PyLong_DigitCount(const PyLongObject *op)
229229
{
230230
assert(PyLong_Check(op));
231-
return op->long_value.lv_tag >> NON_SIZE_BITS;
231+
return (Py_ssize_t)(op->long_value.lv_tag >> NON_SIZE_BITS);
232232
}
233233

234234
/* Equivalent to _PyLong_DigitCount(op) * _PyLong_NonCompactSign(op) */
@@ -263,15 +263,16 @@ _PyLong_SameSign(const PyLongObject *a, const PyLongObject *b)
263263
return (a->long_value.lv_tag & SIGN_MASK) == (b->long_value.lv_tag & SIGN_MASK);
264264
}
265265

266-
#define TAG_FROM_SIGN_AND_SIZE(sign, size) ((1 - (sign)) | ((size) << NON_SIZE_BITS))
266+
#define TAG_FROM_SIGN_AND_SIZE(sign, size) \
267+
((uintptr_t)(1 - (sign)) | ((uintptr_t)(size) << NON_SIZE_BITS))
267268

268269
static inline void
269270
_PyLong_SetSignAndDigitCount(PyLongObject *op, int sign, Py_ssize_t size)
270271
{
271272
assert(size >= 0);
272273
assert(-1 <= sign && sign <= 1);
273274
assert(sign != 0 || size == 0);
274-
op->long_value.lv_tag = TAG_FROM_SIGN_AND_SIZE(sign, (size_t)size);
275+
op->long_value.lv_tag = TAG_FROM_SIGN_AND_SIZE(sign, size);
275276
}
276277

277278
static inline void
@@ -281,7 +282,7 @@ _PyLong_SetDigitCount(PyLongObject *op, Py_ssize_t size)
281282
op->long_value.lv_tag = (((size_t)size) << NON_SIZE_BITS) | (op->long_value.lv_tag & SIGN_MASK);
282283
}
283284

284-
#define NON_SIZE_MASK ~((1 << NON_SIZE_BITS) - 1)
285+
#define NON_SIZE_MASK ~(uintptr_t)((1 << NON_SIZE_BITS) - 1)
285286

286287
static inline void
287288
_PyLong_FlipSign(PyLongObject *op) {

Include/internal/pycore_object.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ static inline void _PyObject_GC_TRACK(
442442
_PyGCHead_SET_NEXT(last, gc);
443443
_PyGCHead_SET_PREV(gc, last);
444444
/* Young objects will be moved into the visited space during GC, so set the bit here */
445-
gc->_gc_next = ((uintptr_t)generation0) | interp->gc.visited_space;
445+
gc->_gc_next = ((uintptr_t)generation0) | (uintptr_t)interp->gc.visited_space;
446446
generation0->_gc_prev = (uintptr_t)gc;
447447
#endif
448448
}
@@ -758,9 +758,9 @@ _PyType_PreHeaderSize(PyTypeObject *tp)
758758
{
759759
return (
760760
#ifndef Py_GIL_DISABLED
761-
_PyType_IS_GC(tp) * sizeof(PyGC_Head) +
761+
(size_t)_PyType_IS_GC(tp) * sizeof(PyGC_Head) +
762762
#endif
763-
_PyType_HasFeature(tp, Py_TPFLAGS_PREHEADER) * 2 * sizeof(PyObject *)
763+
(size_t)_PyType_HasFeature(tp, Py_TPFLAGS_PREHEADER) * 2 * sizeof(PyObject *)
764764
);
765765
}
766766

@@ -821,7 +821,7 @@ static inline PyDictValues *
821821
_PyObject_InlineValues(PyObject *obj)
822822
{
823823
PyTypeObject *tp = Py_TYPE(obj);
824-
assert(tp->tp_basicsize > 0 && tp->tp_basicsize % sizeof(PyObject *) == 0);
824+
assert(tp->tp_basicsize > 0 && (size_t)tp->tp_basicsize % sizeof(PyObject *) == 0);
825825
assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_INLINE_VALUES);
826826
assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT);
827827
return (PyDictValues *)((char *)obj + tp->tp_basicsize);

Include/internal/pycore_stackref.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ typedef union _PyStackRef {
5656

5757
#define Py_TAG_DEFERRED (1)
5858

59-
#define Py_TAG_PTR (0)
60-
#define Py_TAG_BITS (1)
59+
#define Py_TAG_PTR ((uintptr_t)0)
60+
#define Py_TAG_BITS ((uintptr_t)1)
6161

6262
#ifdef Py_GIL_DISABLED
6363
static const _PyStackRef PyStackRef_NULL = { .bits = 0 | Py_TAG_DEFERRED};
@@ -98,7 +98,7 @@ typedef union _PyStackRef {
9898
static inline PyObject *
9999
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
100100
{
101-
PyObject *cleared = ((PyObject *)((stackref).bits & (~(uintptr_t)Py_TAG_BITS)));
101+
PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
102102
return cleared;
103103
}
104104
#else

Tools/build/.warningignore_macos

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
Include/internal/mimalloc/mimalloc/internal.h 4
77
Include/internal/pycore_backoff.h 1
88
Include/internal/pycore_dict.h 2
9-
Include/internal/pycore_long.h 2
10-
Include/internal/pycore_object.h 4
119
Modules/_asynciomodule.c 3
1210
Modules/_bisectmodule.c 2
1311
Modules/_bz2module.c 5
@@ -67,7 +65,7 @@ Modules/_testcapi/vectorcall.c 3
6765
Modules/_testcapi/watchers.c 3
6866
Modules/_testcapimodule.c 3
6967
Modules/_testclinic.c 14
70-
Modules/_testexternalinspection.c 8
68+
Modules/_testexternalinspection.c 7
7169
Modules/_testinternalcapi.c 8
7270
Modules/_testinternalcapi/pytime.c 8
7371
Modules/_testinternalcapi/test_critical_sections.c 1
@@ -201,7 +199,7 @@ Python/fileutils.c 7
201199
Python/flowgraph.c 8
202200
Python/formatter_unicode.c 7
203201
Python/frame.c 4
204-
Python/gc.c 8
202+
Python/gc.c 7
205203
Python/generated_cases.c.h 35
206204
Python/getargs.c 11
207205
Python/import.c 5

Tools/build/.warningignore_ubuntu

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ Include/internal/pycore_backoff.h 3
2020
Include/internal/pycore_blocks_output_buffer.h 1
2121
Include/internal/pycore_dict.h 2
2222
Include/internal/pycore_interp.h 1
23-
Include/internal/pycore_long.h 3
24-
Include/internal/pycore_object.h 4
2523
Include/internal/pycore_obmalloc.h 1
2624
Include/internal/pycore_pymath.h 1
2725
Include/internal/pycore_runtime_init.h 1
@@ -96,7 +94,7 @@ Modules/_testcapi/watchers.c 3
9694
Modules/_testcapimodule.c 1
9795
Modules/_testclinic.c 14
9896
Modules/_testclinic.c 14
99-
Modules/_testexternalinspection.c 7
97+
Modules/_testexternalinspection.c 6
10098
Modules/_testinternalcapi.c 10
10199
Modules/_testinternalcapi/test_critical_sections.c 1
102100
Modules/_testinternalcapi/test_lock.c 4
@@ -224,9 +222,7 @@ Python/fileutils.c 11
224222
Python/flowgraph.c 7
225223
Python/formatter_unicode.c 6
226224
Python/frame.c 3
227-
Python/gc.c 9
228-
Python/gc.c 9
229-
Python/generated_cases.c.h 27
225+
Python/gc.c 8
230226
Python/generated_cases.c.h 27
231227
Python/getargs.c 7
232228
Python/hashtable.c 1

0 commit comments

Comments
 (0)