Skip to content

Commit 90e7230

Browse files
authored
gh-92781: Avoid mixing declarations and code in C API (#92783)
Avoid mixing declarations and code in the C API to fix the compiler warning: "ISO C90 forbids mixed declarations and code" [-Werror=declaration-after-statement].
1 parent db0b455 commit 90e7230

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

Include/cpython/abstract.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,8 @@ static inline PyObject *
111111
PyObject_CallMethodOneArg(PyObject *self, PyObject *name, PyObject *arg)
112112
{
113113
PyObject *args[2] = {self, arg};
114-
115-
assert(arg != NULL);
116114
size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET;
115+
assert(arg != NULL);
117116
return PyObject_VectorcallMethod(name, args, nargsf, _Py_NULL);
118117
}
119118

@@ -160,9 +159,8 @@ static inline PyObject *
160159
_PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg)
161160
{
162161
PyObject *args[2] = {self, arg};
163-
164-
assert(arg != NULL);
165162
size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET;
163+
assert(arg != NULL);
166164
return _PyObject_VectorcallMethodId(name, args, nargsf, _Py_NULL);
167165
}
168166

Include/cpython/cellobject.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ PyAPI_FUNC(PyObject *) PyCell_Get(PyObject *);
2222
PyAPI_FUNC(int) PyCell_Set(PyObject *, PyObject *);
2323

2424
static inline PyObject* PyCell_GET(PyObject *op) {
25+
PyCellObject *cell;
2526
assert(PyCell_Check(op));
26-
PyCellObject *cell = _Py_CAST(PyCellObject*, op);
27+
cell = _Py_CAST(PyCellObject*, op);
2728
return cell->ob_ref;
2829
}
2930
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
3031
# define PyCell_GET(op) PyCell_GET(_PyObject_CAST(op))
3132
#endif
3233

3334
static inline void PyCell_SET(PyObject *op, PyObject *value) {
35+
PyCellObject *cell;
3436
assert(PyCell_Check(op));
35-
PyCellObject *cell = _Py_CAST(PyCellObject*, op);
37+
cell = _Py_CAST(PyCellObject*, op);
3638
cell->ob_ref = value;
3739
}
3840
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000

Include/cpython/dictobject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ PyAPI_FUNC(int) _PyDict_Next(
4747

4848
/* Get the number of items of a dictionary. */
4949
static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
50+
PyDictObject *mp;
5051
assert(PyDict_Check(op));
51-
PyDictObject *mp = _Py_CAST(PyDictObject*, op);
52+
mp = _Py_CAST(PyDictObject*, op);
5253
return mp->ma_used;
5354
}
5455
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000

Include/cpython/unicodeobject.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,9 @@ static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
262262
}
263263

264264
static inline void* _PyUnicode_NONCOMPACT_DATA(PyObject *op) {
265+
void *data;
265266
assert(!PyUnicode_IS_COMPACT(op));
266-
void *data = _PyUnicodeObject_CAST(op)->data.any;
267+
data = _PyUnicodeObject_CAST(op)->data.any;
267268
assert(data != NULL);
268269
return data;
269270
}
@@ -369,11 +370,13 @@ static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
369370
than iterating over the string. */
370371
static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
371372
{
373+
int kind;
374+
372375
if (PyUnicode_IS_ASCII(op)) {
373376
return 0x7fU;
374377
}
375378

376-
int kind = PyUnicode_KIND(op);
379+
kind = PyUnicode_KIND(op);
377380
if (kind == PyUnicode_1BYTE_KIND) {
378381
return 0xffU;
379382
}

Include/cpython/weakrefobject.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
3737
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
3838

3939
static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj) {
40+
PyWeakReference *ref;
41+
PyObject *obj;
4042
assert(PyWeakref_Check(ref_obj));
41-
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
42-
PyObject *obj = ref->wr_object;
43+
ref = _Py_CAST(PyWeakReference*, ref_obj);
44+
obj = ref->wr_object;
4345
// Explanation for the Py_REFCNT() check: when a weakref's target is part
4446
// of a long chain of deallocations which triggers the trashcan mechanism,
4547
// clearing the weakrefs can be delayed long after the target's refcount
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Avoid mixing declarations and code in the C API to fix the compiler warning:
2+
"ISO C90 forbids mixed declarations and code"
3+
[-Werror=declaration-after-statement]. Patch by Victor Stinner.

0 commit comments

Comments
 (0)