Use the new Py_ARRAY_LENGTH macro

This commit is contained in:
Victor Stinner 2011-09-29 00:42:28 +02:00
parent b9dcffb51e
commit 639418812f
15 changed files with 26 additions and 28 deletions

View File

@ -1593,7 +1593,7 @@ test_long_numbits(PyObject *self)
{-0xfffffffL, 28, -1}}; {-0xfffffffL, 28, -1}};
int i; int i;
for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
PyObject *plong = PyLong_FromLong(testcases[i].input); PyObject *plong = PyLong_FromLong(testcases[i].input);
size_t nbits = _PyLong_NumBits(plong); size_t nbits = _PyLong_NumBits(plong);
int sign = _PyLong_Sign(plong); int sign = _PyLong_Sign(plong);

View File

@ -112,7 +112,7 @@ static fault_handler_t faulthandler_handlers[] = {
{SIGSEGV, 0, "Segmentation fault", } {SIGSEGV, 0, "Segmentation fault", }
}; };
static const unsigned char faulthandler_nsignals = \ static const unsigned char faulthandler_nsignals = \
sizeof(faulthandler_handlers) / sizeof(faulthandler_handlers[0]); Py_ARRAY_LENGTH(faulthandler_handlers);
#ifdef HAVE_SIGALTSTACK #ifdef HAVE_SIGALTSTACK
static stack_t stack; static stack_t stack;

View File

@ -1435,7 +1435,7 @@ math_factorial(PyObject *self, PyObject *arg)
} }
/* use lookup table if x is small */ /* use lookup table if x is small */
if (x < (long)(sizeof(SmallFactorials)/sizeof(SmallFactorials[0]))) if (x < (long)Py_ARRAY_LENGTH(SmallFactorials))
return PyLong_FromUnsignedLong(SmallFactorials[x]); return PyLong_FromUnsignedLong(SmallFactorials[x]);
/* else express in the form odd_part * 2**two_valuation, and compute as /* else express in the form odd_part * 2**two_valuation, and compute as

View File

@ -1061,8 +1061,8 @@ build_namelists (PyObject *module)
int num_controls; int num_controls;
int i; int i;
num_controls = sizeof(control_labels) / sizeof(control_labels[0]); num_controls = Py_ARRAY_LENGTH(control_labels);
assert(num_controls == sizeof(control_names) / sizeof(control_names[0])); assert(num_controls == Py_ARRAY_LENGTH(control_names));
labels = PyList_New(num_controls); labels = PyList_New(num_controls);
names = PyList_New(num_controls); names = PyList_New(num_controls);

View File

@ -2895,9 +2895,9 @@ posix__getfullpathname(PyObject *self, PyObject *args)
DWORD result; DWORD result;
PyObject *v; PyObject *v;
result = GetFullPathNameW(wpath, result = GetFullPathNameW(wpath,
sizeof(woutbuf)/sizeof(woutbuf[0]), Py_ARRAY_LENGTH(woutbuf),
woutbuf, &wtemp); woutbuf, &wtemp);
if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { if (result > Py_ARRAY_LENGTH(woutbuf)) {
woutbufp = malloc(result * sizeof(Py_UNICODE)); woutbufp = malloc(result * sizeof(Py_UNICODE));
if (!woutbufp) if (!woutbufp)
return PyErr_NoMemory(); return PyErr_NoMemory();
@ -2920,7 +2920,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
PyUnicode_FSConverter, &opath)) PyUnicode_FSConverter, &opath))
return NULL; return NULL;
path = PyBytes_AsString(opath); path = PyBytes_AsString(opath);
if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
outbuf, &temp)) { outbuf, &temp)) {
win32_error("GetFullPathName", path); win32_error("GetFullPathName", path);
Py_DECREF(opath); Py_DECREF(opath);
@ -5656,7 +5656,7 @@ posix_getlogin(PyObject *self, PyObject *noargs)
PyObject *result = NULL; PyObject *result = NULL;
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
wchar_t user_name[UNLEN + 1]; wchar_t user_name[UNLEN + 1];
DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]); DWORD num_chars = Py_ARRAY_LENGTH(user_name);
if (GetUserNameW(user_name, &num_chars)) { if (GetUserNameW(user_name, &num_chars)) {
/* num_chars is the number of unicode chars plus null terminator */ /* num_chars is the number of unicode chars plus null terminator */

View File

@ -3812,7 +3812,7 @@ socket_gethostname(PyObject *self, PyObject *unused)
version of the hostname, whereas we need a Unicode string. version of the hostname, whereas we need a Unicode string.
Otherwise, gethostname apparently also returns the DNS name. */ Otherwise, gethostname apparently also returns the DNS name. */
wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = sizeof(buf) / sizeof(wchar_t); DWORD size = Py_ARRAY_LENGTH(buf);
PyObject *result; PyObject *result;
if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) { if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) {
if (GetLastError() == ERROR_MORE_DATA) { if (GetLastError() == ERROR_MORE_DATA) {
@ -6281,7 +6281,7 @@ PyInit__socket(void)
DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
int i; int i;
for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) { for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
PyObject *tmp; PyObject *tmp;
tmp = PyLong_FromUnsignedLong(codes[i]); tmp = PyLong_FromUnsignedLong(codes[i]);
if (tmp == NULL) if (tmp == NULL)

View File

@ -439,7 +439,7 @@ unicodedata_decomposition(PyObject *self, PyObject *args)
from Tools/unicode/makeunicodedata.py, it should not be possible from Tools/unicode/makeunicodedata.py, it should not be possible
to overflow decomp_prefix. */ to overflow decomp_prefix. */
prefix_index = decomp_data[index] & 255; prefix_index = decomp_data[index] & 255;
assert(prefix_index < (sizeof(decomp_prefix)/sizeof(*decomp_prefix))); assert(prefix_index < Py_ARRAY_LENGTH(decomp_prefix));
/* copy prefix */ /* copy prefix */
i = strlen(decomp_prefix[prefix_index]); i = strlen(decomp_prefix[prefix_index]);

View File

@ -2444,8 +2444,7 @@ _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e)
break; break;
} }
} }
assert(1 <= x_size && assert(1 <= x_size && x_size <= (Py_ssize_t)Py_ARRAY_LENGTH(x_digits));
x_size <= (Py_ssize_t)(sizeof(x_digits)/sizeof(digit)));
/* Round, and convert to double. */ /* Round, and convert to double. */
x_digits[0] += half_even_correction[x_digits[0] & 7]; x_digits[0] += half_even_correction[x_digits[0] & 7];

View File

@ -2355,7 +2355,7 @@ PyObject* PyType_FromSpec(PyType_Spec *spec)
res->ht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; res->ht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE;
for (slot = spec->slots; slot->slot; slot++) { for (slot = spec->slots; slot->slot; slot++) {
if (slot->slot >= sizeof(slotoffsets)/sizeof(slotoffsets[0])) { if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) {
PyErr_SetString(PyExc_RuntimeError, "invalid slot offset"); PyErr_SetString(PyExc_RuntimeError, "invalid slot offset");
goto fail; goto fail;
} }

View File

@ -12552,7 +12552,7 @@ void _PyUnicode_Init(void)
/* initialize the linebreak bloom filter */ /* initialize the linebreak bloom filter */
bloom_linebreak = make_bloom_mask( bloom_linebreak = make_bloom_mask(
PyUnicode_2BYTE_KIND, linebreak, PyUnicode_2BYTE_KIND, linebreak,
sizeof(linebreak) / sizeof(linebreak[0])); Py_ARRAY_LENGTH(linebreak));
PyType_Ready(&EncodingMapType); PyType_Ready(&EncodingMapType);
} }

View File

@ -77,7 +77,7 @@ void PyWinFreeze_ExeTerm(void)
{ {
// Must go backwards // Must go backwards
char **modName; char **modName;
for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2; for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2;
modName >= possibleModules; modName >= possibleModules;
*modName--) { *modName--) {
/* printf("Terminating '%s'\n", *modName);*/ /* printf("Terminating '%s'\n", *modName);*/
@ -103,7 +103,7 @@ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{ {
// Must go backwards // Must go backwards
char **modName; char **modName;
for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2; for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2;
modName >= possibleModules; modName >= possibleModules;
*modName--) *modName--)
CallModuleDllMain(*modName, DLL_PROCESS_DETACH); CallModuleDllMain(*modName, DLL_PROCESS_DETACH);

View File

@ -251,7 +251,7 @@ getpythonregpath(HKEY keyBase, int skipcore)
if (keyBuf==NULL) goto done; if (keyBuf==NULL) goto done;
memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR)); memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
keyBufPtr += sizeof(keyPrefix)/sizeof(WCHAR) - 1; keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen); mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
keyBufPtr += versionLen; keyBufPtr += versionLen;
/* NULL comes with this one! */ /* NULL comes with this one! */

View File

@ -1044,7 +1044,7 @@ static int _PyCodecRegistry_Init(void)
interp->codec_error_registry = PyDict_New(); interp->codec_error_registry = PyDict_New();
if (interp->codec_error_registry) { if (interp->codec_error_registry) {
for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) { for (i = 0; i < Py_ARRAY_LENGTH(methods); ++i) {
PyObject *func = PyCFunction_New(&methods[i].def, NULL); PyObject *func = PyCFunction_New(&methods[i].def, NULL);
int res; int res;
if (!func) if (!func)

View File

@ -129,7 +129,6 @@ aix_loaderror(const char *pathname)
{L_ERROR_ERRNO, NULL} {L_ERROR_ERRNO, NULL}
}; };
#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname); PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname);
@ -140,7 +139,7 @@ aix_loaderror(const char *pathname)
} }
for(i = 0; message[i] && *message[i]; i++) { for(i = 0; message[i] && *message[i]; i++) {
int nerr = atoi(message[i]); int nerr = atoi(message[i]);
for (j=0; j<LOAD_ERRTAB_LEN ; j++) { for (j=0; j < Py_ARRAY_LENGTH(load_errtab); j++) {
if (nerr == load_errtab[j].errNo && load_errtab[j].errstr) if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
ERRBUF_APPEND(load_errtab[j].errstr); ERRBUF_APPEND(load_errtab[j].errstr);
} }

View File

@ -1771,7 +1771,7 @@ sys_update_path(int argc, wchar_t **argv)
the argument must be the full path anyway. */ the argument must be the full path anyway. */
wchar_t *ptemp; wchar_t *ptemp;
if (GetFullPathNameW(argv0, if (GetFullPathNameW(argv0,
sizeof(fullpath)/sizeof(fullpath[0]), Py_ARRAY_LENGTH(fullpath),
fullpath, fullpath,
&ptemp)) { &ptemp)) {
argv0 = fullpath; argv0 = fullpath;