Skip to content

Commit 6394188

Browse files
author
Victor Stinner
committed
Use the new Py_ARRAY_LENGTH macro
1 parent b9dcffb commit 6394188

File tree

15 files changed

+26
-28
lines changed

15 files changed

+26
-28
lines changed

Modules/_testcapimodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ test_long_numbits(PyObject *self)
15931593
{-0xfffffffL, 28, -1}};
15941594
int i;
15951595

1596-
for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
1596+
for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
15971597
PyObject *plong = PyLong_FromLong(testcases[i].input);
15981598
size_t nbits = _PyLong_NumBits(plong);
15991599
int sign = _PyLong_Sign(plong);

Modules/faulthandler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static fault_handler_t faulthandler_handlers[] = {
112112
{SIGSEGV, 0, "Segmentation fault", }
113113
};
114114
static const unsigned char faulthandler_nsignals = \
115-
sizeof(faulthandler_handlers) / sizeof(faulthandler_handlers[0]);
115+
Py_ARRAY_LENGTH(faulthandler_handlers);
116116

117117
#ifdef HAVE_SIGALTSTACK
118118
static stack_t stack;

Modules/mathmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ math_factorial(PyObject *self, PyObject *arg)
14351435
}
14361436

14371437
/* use lookup table if x is small */
1438-
if (x < (long)(sizeof(SmallFactorials)/sizeof(SmallFactorials[0])))
1438+
if (x < (long)Py_ARRAY_LENGTH(SmallFactorials))
14391439
return PyLong_FromUnsignedLong(SmallFactorials[x]);
14401440

14411441
/* else express in the form odd_part * 2**two_valuation, and compute as

Modules/ossaudiodev.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ oss_self(PyObject *self, PyObject *unused)
530530
return self;
531531
}
532532

533-
static PyObject *
533+
static PyObject *
534534
oss_exit(PyObject *self, PyObject *unused)
535535
{
536536
PyObject *ret = PyObject_CallMethod(self, "close", NULL);
@@ -1061,8 +1061,8 @@ build_namelists (PyObject *module)
10611061
int num_controls;
10621062
int i;
10631063

1064-
num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
1065-
assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
1064+
num_controls = Py_ARRAY_LENGTH(control_labels);
1065+
assert(num_controls == Py_ARRAY_LENGTH(control_names));
10661066

10671067
labels = PyList_New(num_controls);
10681068
names = PyList_New(num_controls);

Modules/posixmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,9 +2895,9 @@ posix__getfullpathname(PyObject *self, PyObject *args)
28952895
DWORD result;
28962896
PyObject *v;
28972897
result = GetFullPathNameW(wpath,
2898-
sizeof(woutbuf)/sizeof(woutbuf[0]),
2898+
Py_ARRAY_LENGTH(woutbuf),
28992899
woutbuf, &wtemp);
2900-
if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2900+
if (result > Py_ARRAY_LENGTH(woutbuf)) {
29012901
woutbufp = malloc(result * sizeof(Py_UNICODE));
29022902
if (!woutbufp)
29032903
return PyErr_NoMemory();
@@ -2920,7 +2920,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
29202920
PyUnicode_FSConverter, &opath))
29212921
return NULL;
29222922
path = PyBytes_AsString(opath);
2923-
if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2923+
if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
29242924
outbuf, &temp)) {
29252925
win32_error("GetFullPathName", path);
29262926
Py_DECREF(opath);
@@ -4903,7 +4903,7 @@ static PyObject *
49034903
cpu_set_repr(Py_cpu_set *set)
49044904
{
49054905
return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
4906-
}
4906+
}
49074907

49084908
static Py_ssize_t
49094909
cpu_set_len(Py_cpu_set *set)
@@ -5656,7 +5656,7 @@ posix_getlogin(PyObject *self, PyObject *noargs)
56565656
PyObject *result = NULL;
56575657
#ifdef MS_WINDOWS
56585658
wchar_t user_name[UNLEN + 1];
5659-
DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5659+
DWORD num_chars = Py_ARRAY_LENGTH(user_name);
56605660

56615661
if (GetUserNameW(user_name, &num_chars)) {
56625662
/* num_chars is the number of unicode chars plus null terminator */

Modules/socketmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,7 +3812,7 @@ socket_gethostname(PyObject *self, PyObject *unused)
38123812
version of the hostname, whereas we need a Unicode string.
38133813
Otherwise, gethostname apparently also returns the DNS name. */
38143814
wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
3815-
DWORD size = sizeof(buf) / sizeof(wchar_t);
3815+
DWORD size = Py_ARRAY_LENGTH(buf);
38163816
PyObject *result;
38173817
if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) {
38183818
if (GetLastError() == ERROR_MORE_DATA) {
@@ -6281,7 +6281,7 @@ PyInit__socket(void)
62816281
DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
62826282
const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
62836283
int i;
6284-
for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) {
6284+
for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
62856285
PyObject *tmp;
62866286
tmp = PyLong_FromUnsignedLong(codes[i]);
62876287
if (tmp == NULL)

Modules/unicodedata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ unicodedata_decomposition(PyObject *self, PyObject *args)
439439
from Tools/unicode/makeunicodedata.py, it should not be possible
440440
to overflow decomp_prefix. */
441441
prefix_index = decomp_data[index] & 255;
442-
assert(prefix_index < (sizeof(decomp_prefix)/sizeof(*decomp_prefix)));
442+
assert(prefix_index < Py_ARRAY_LENGTH(decomp_prefix));
443443

444444
/* copy prefix */
445445
i = strlen(decomp_prefix[prefix_index]);

Objects/longobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,8 +2444,7 @@ _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e)
24442444
break;
24452445
}
24462446
}
2447-
assert(1 <= x_size &&
2448-
x_size <= (Py_ssize_t)(sizeof(x_digits)/sizeof(digit)));
2447+
assert(1 <= x_size && x_size <= (Py_ssize_t)Py_ARRAY_LENGTH(x_digits));
24492448

24502449
/* Round, and convert to double. */
24512450
x_digits[0] += half_even_correction[x_digits[0] & 7];

Objects/typeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,7 @@ PyObject* PyType_FromSpec(PyType_Spec *spec)
23552355
res->ht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE;
23562356

23572357
for (slot = spec->slots; slot->slot; slot++) {
2358-
if (slot->slot >= sizeof(slotoffsets)/sizeof(slotoffsets[0])) {
2358+
if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) {
23592359
PyErr_SetString(PyExc_RuntimeError, "invalid slot offset");
23602360
goto fail;
23612361
}
@@ -2583,7 +2583,7 @@ type_prepare(PyObject *self, PyObject *args, PyObject *kwds)
25832583
return PyDict_New();
25842584
}
25852585

2586-
/*
2586+
/*
25872587
Merge the __dict__ of aclass into dict, and recursively also all
25882588
the __dict__s of aclass's base classes. The order of merging isn't
25892589
defined, as it's expected that only the final set of dict keys is

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12552,7 +12552,7 @@ void _PyUnicode_Init(void)
1255212552
/* initialize the linebreak bloom filter */
1255312553
bloom_linebreak = make_bloom_mask(
1255412554
PyUnicode_2BYTE_KIND, linebreak,
12555-
sizeof(linebreak) / sizeof(linebreak[0]));
12555+
Py_ARRAY_LENGTH(linebreak));
1255612556

1255712557
PyType_Ready(&EncodingMapType);
1255812558
}

0 commit comments

Comments
 (0)