From 639418812f11749f99d1160b26325bdfa3a26a6f Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 29 Sep 2011 00:42:28 +0200 Subject: Use the new Py_ARRAY_LENGTH macro --- Modules/_testcapimodule.c | 2 +- Modules/faulthandler.c | 2 +- Modules/mathmodule.c | 2 +- Modules/ossaudiodev.c | 6 +++--- Modules/posixmodule.c | 10 +++++----- Modules/socketmodule.c | 4 ++-- Modules/unicodedata.c | 2 +- Objects/longobject.c | 3 +-- Objects/typeobject.c | 4 ++-- Objects/unicodeobject.c | 2 +- PC/frozen_dllmain.c | 4 ++-- PC/getpathp.c | 6 +++--- Python/codecs.c | 2 +- Python/dynload_aix.c | 3 +-- Python/sysmodule.c | 2 +- 15 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 2e36c73..f1eac91 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -1593,7 +1593,7 @@ test_long_numbits(PyObject *self) {-0xfffffffL, 28, -1}}; 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); size_t nbits = _PyLong_NumBits(plong); int sign = _PyLong_Sign(plong); diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index 15d6863..8ee0630 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -112,7 +112,7 @@ static fault_handler_t faulthandler_handlers[] = { {SIGSEGV, 0, "Segmentation fault", } }; static const unsigned char faulthandler_nsignals = \ - sizeof(faulthandler_handlers) / sizeof(faulthandler_handlers[0]); + Py_ARRAY_LENGTH(faulthandler_handlers); #ifdef HAVE_SIGALTSTACK static stack_t stack; diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 7e73bfe..c4cc46e 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1435,7 +1435,7 @@ math_factorial(PyObject *self, PyObject *arg) } /* 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]); /* else express in the form odd_part * 2**two_valuation, and compute as diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c index 95a23b7..bcab9da 100644 --- a/Modules/ossaudiodev.c +++ b/Modules/ossaudiodev.c @@ -530,7 +530,7 @@ oss_self(PyObject *self, PyObject *unused) return self; } -static PyObject * +static PyObject * oss_exit(PyObject *self, PyObject *unused) { PyObject *ret = PyObject_CallMethod(self, "close", NULL); @@ -1061,8 +1061,8 @@ build_namelists (PyObject *module) int num_controls; int i; - num_controls = sizeof(control_labels) / sizeof(control_labels[0]); - assert(num_controls == sizeof(control_names) / sizeof(control_names[0])); + num_controls = Py_ARRAY_LENGTH(control_labels); + assert(num_controls == Py_ARRAY_LENGTH(control_names)); labels = PyList_New(num_controls); names = PyList_New(num_controls); diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 25e7f0d..c35e8a1 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2895,9 +2895,9 @@ posix__getfullpathname(PyObject *self, PyObject *args) DWORD result; PyObject *v; result = GetFullPathNameW(wpath, - sizeof(woutbuf)/sizeof(woutbuf[0]), + Py_ARRAY_LENGTH(woutbuf), woutbuf, &wtemp); - if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { + if (result > Py_ARRAY_LENGTH(woutbuf)) { woutbufp = malloc(result * sizeof(Py_UNICODE)); if (!woutbufp) return PyErr_NoMemory(); @@ -2920,7 +2920,7 @@ posix__getfullpathname(PyObject *self, PyObject *args) PyUnicode_FSConverter, &opath)) return NULL; path = PyBytes_AsString(opath); - if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]), + if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), outbuf, &temp)) { win32_error("GetFullPathName", path); Py_DECREF(opath); @@ -4903,7 +4903,7 @@ static PyObject * cpu_set_repr(Py_cpu_set *set) { return PyUnicode_FromFormat("", set->ncpus); -} +} static Py_ssize_t cpu_set_len(Py_cpu_set *set) @@ -5656,7 +5656,7 @@ posix_getlogin(PyObject *self, PyObject *noargs) PyObject *result = NULL; #ifdef MS_WINDOWS 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)) { /* num_chars is the number of unicode chars plus null terminator */ diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 79ccae8..62b3fe9 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -3812,7 +3812,7 @@ socket_gethostname(PyObject *self, PyObject *unused) version of the hostname, whereas we need a Unicode string. Otherwise, gethostname apparently also returns the DNS name. */ wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; - DWORD size = sizeof(buf) / sizeof(wchar_t); + DWORD size = Py_ARRAY_LENGTH(buf); PyObject *result; if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) { if (GetLastError() == ERROR_MORE_DATA) { @@ -6281,7 +6281,7 @@ PyInit__socket(void) DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS}; const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"}; int i; - for(i = 0; iht_type.tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE; 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"); goto fail; } @@ -2583,7 +2583,7 @@ type_prepare(PyObject *self, PyObject *args, PyObject *kwds) return PyDict_New(); } -/* +/* Merge the __dict__ of aclass into dict, and recursively also all the __dict__s of aclass's base classes. The order of merging isn't defined, as it's expected that only the final set of dict keys is diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index bff74d9..387974d 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -12552,7 +12552,7 @@ void _PyUnicode_Init(void) /* initialize the linebreak bloom filter */ bloom_linebreak = make_bloom_mask( PyUnicode_2BYTE_KIND, linebreak, - sizeof(linebreak) / sizeof(linebreak[0])); + Py_ARRAY_LENGTH(linebreak)); PyType_Ready(&EncodingMapType); } diff --git a/PC/frozen_dllmain.c b/PC/frozen_dllmain.c index a8cc885..e1e4eda 100644 --- a/PC/frozen_dllmain.c +++ b/PC/frozen_dllmain.c @@ -77,7 +77,7 @@ void PyWinFreeze_ExeTerm(void) { // Must go backwards char **modName; - for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2; + for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2; modName >= possibleModules; *modName--) { /* printf("Terminating '%s'\n", *modName);*/ @@ -103,7 +103,7 @@ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { // Must go backwards char **modName; - for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2; + for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2; modName >= possibleModules; *modName--) CallModuleDllMain(*modName, DLL_PROCESS_DETACH); diff --git a/PC/getpathp.c b/PC/getpathp.c index cd3a4b2..8921aa0 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -251,7 +251,7 @@ getpythonregpath(HKEY keyBase, int skipcore) if (keyBuf==NULL) goto done; memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR)); - keyBufPtr += sizeof(keyPrefix)/sizeof(WCHAR) - 1; + keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1; mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen); keyBufPtr += versionLen; /* NULL comes with this one! */ @@ -708,8 +708,8 @@ Py_GetProgramFullPath(void) return progpath; } -/* Load python3.dll before loading any extension module that might refer - to it. That way, we can be sure that always the python3.dll corresponding +/* Load python3.dll before loading any extension module that might refer + to it. That way, we can be sure that always the python3.dll corresponding to this python DLL is loaded, not a python3.dll that might be on the path by chance. Return whether the DLL was found. diff --git a/Python/codecs.c b/Python/codecs.c index be2e833..4c2fc5d 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -1044,7 +1044,7 @@ static int _PyCodecRegistry_Init(void) interp->codec_error_registry = PyDict_New(); 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); int res; if (!func) diff --git a/Python/dynload_aix.c b/Python/dynload_aix.c index 74c7b32..6287c86 100644 --- a/Python/dynload_aix.c +++ b/Python/dynload_aix.c @@ -129,7 +129,6 @@ aix_loaderror(const char *pathname) {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) 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++) { int nerr = atoi(message[i]); - for (j=0; j