diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index daba2ba8eaf..9c81247ab87 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4280,6 +4280,10 @@ Array_subscript(PyObject *myself, PyObject *item) for (cur = start, i = 0; i < slicelen; cur += step, i++) { PyObject *v = Array_item(myself, cur); + if (v == NULL) { + Py_DECREF(np); + return NULL; + } PyList_SET_ITEM(np, i, v); } return np; diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index d3e394041f6..6f9e9def474 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -3009,18 +3009,25 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w, *wcmp = Py_NotImplemented; } } - else if (PyObject_IsInstance(w, Rational)) { - *wcmp = numerator_as_decimal(w, context); - if (*wcmp && !mpd_isspecial(MPD(v))) { - *vcmp = multiply_by_denominator(v, w, context); - if (*vcmp == NULL) { - Py_CLEAR(*wcmp); + else { + int is_instance = PyObject_IsInstance(w, Rational); + if (is_instance < 0) { + *wcmp = NULL; + return 0; + } + if (is_instance) { + *wcmp = numerator_as_decimal(w, context); + if (*wcmp && !mpd_isspecial(MPD(v))) { + *vcmp = multiply_by_denominator(v, w, context); + if (*vcmp == NULL) { + Py_CLEAR(*wcmp); + } } } - } - else { - Py_INCREF(Py_NotImplemented); - *wcmp = Py_NotImplemented; + else { + Py_INCREF(Py_NotImplemented); + *wcmp = Py_NotImplemented; + } } if (*wcmp == NULL || *wcmp == Py_NotImplemented) { @@ -3180,6 +3187,7 @@ dec_format(PyObject *dec, PyObject *args) replace_fillchar = 1; fmt = dec_strdup(fmt, size); if (fmt == NULL) { + PyErr_NoMemory(); return NULL; } fmt[0] = '_'; diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c index b196749aecc..400c3448bab 100644 --- a/Modules/_localemodule.c +++ b/Modules/_localemodule.c @@ -151,8 +151,10 @@ PyLocale_localeconv(PyObject* self) do { \ if (obj == NULL) \ goto failed; \ - if (PyDict_SetItemString(result, key, obj) < 0) \ + if (PyDict_SetItemString(result, key, obj) < 0) { \ + Py_DECREF(obj); \ goto failed; \ + } \ Py_DECREF(obj); \ } while (0) @@ -196,7 +198,6 @@ PyLocale_localeconv(PyObject* self) failed: Py_XDECREF(result); - Py_XDECREF(x); return NULL; } diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 34b95c02a8d..33624542ac5 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -64,6 +64,69 @@ test_config(PyObject *self) return Py_None; } +static PyObject* +test_sizeof_c_types(PyObject *self) +{ +#define CHECK_SIZEOF(TYPE, EXPECTED) \ + if (EXPECTED != sizeof(TYPE)) { \ + PyErr_Format(TestError, \ + "sizeof(%s) = %u instead of %u", \ + #TYPE, sizeof(TYPE), EXPECTED); \ + return (PyObject*)NULL; \ + } +#define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0) +#define CHECK_SIGNNESS(TYPE, SIGNED) \ + if (IS_SIGNED(TYPE) != SIGNED) { \ + PyErr_Format(TestError, \ + "%s signness is, instead of %i", \ + #TYPE, IS_SIGNED(TYPE), SIGNED); \ + return (PyObject*)NULL; \ + } + + /* integer types */ + CHECK_SIZEOF(Py_UCS1, 1); + CHECK_SIZEOF(Py_UCS2, 2); + CHECK_SIZEOF(Py_UCS4, 4); + CHECK_SIGNNESS(Py_UCS1, 0); + CHECK_SIGNNESS(Py_UCS2, 0); + CHECK_SIGNNESS(Py_UCS4, 0); +#ifdef HAVE_INT32_T + CHECK_SIZEOF(PY_INT32_T, 4); + CHECK_SIGNNESS(PY_INT32_T, 1); +#endif +#ifdef HAVE_UINT32_T + CHECK_SIZEOF(PY_UINT32_T, 4); + CHECK_SIGNNESS(PY_UINT32_T, 0); +#endif +#ifdef HAVE_INT64_T + CHECK_SIZEOF(PY_INT64_T, 8); + CHECK_SIGNNESS(PY_INT64_T, 1); +#endif +#ifdef HAVE_UINT64_T + CHECK_SIZEOF(PY_UINT64_T, 8); + CHECK_SIGNNESS(PY_UINT64_T, 0); +#endif + + /* pointer/size types */ + CHECK_SIZEOF(size_t, sizeof(void *)); + CHECK_SIGNNESS(size_t, 0); + CHECK_SIZEOF(Py_ssize_t, sizeof(void *)); + CHECK_SIGNNESS(Py_ssize_t, 1); + + CHECK_SIZEOF(Py_uintptr_t, sizeof(void *)); + CHECK_SIGNNESS(Py_uintptr_t, 0); + CHECK_SIZEOF(Py_intptr_t, sizeof(void *)); + CHECK_SIGNNESS(Py_intptr_t, 1); + + Py_INCREF(Py_None); + return Py_None; + +#undef IS_SIGNED +#undef CHECK_SIGNESS +#undef CHECK_SIZEOF +} + + static PyObject* test_list_api(PyObject *self) { @@ -2783,6 +2846,7 @@ static PyMethodDef TestMethods[] = { {"raise_exception", raise_exception, METH_VARARGS}, {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, {"test_config", (PyCFunction)test_config, METH_NOARGS}, + {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS}, {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, diff --git a/Objects/abstract.c b/Objects/abstract.c index 6c7a6cd2269..91df5da6557 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -2144,6 +2144,8 @@ PyObject_CallFunction(PyObject *callable, const char *format, ...) } else args = PyTuple_New(0); + if (args == NULL) + return NULL; return call_function_tail(callable, args); } diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index a7ea9c8597a..208e5e3db34 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -896,6 +896,19 @@ _PyUnicode_New(Py_ssize_t length) if (unicode == NULL) return NULL; new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); + + _PyUnicode_WSTR_LENGTH(unicode) = length; + _PyUnicode_HASH(unicode) = -1; + _PyUnicode_STATE(unicode).interned = 0; + _PyUnicode_STATE(unicode).kind = 0; + _PyUnicode_STATE(unicode).compact = 0; + _PyUnicode_STATE(unicode).ready = 0; + _PyUnicode_STATE(unicode).ascii = 0; + _PyUnicode_DATA_ANY(unicode) = NULL; + _PyUnicode_LENGTH(unicode) = 0; + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; + _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); if (!_PyUnicode_WSTR(unicode)) { Py_DECREF(unicode); @@ -912,17 +925,7 @@ _PyUnicode_New(Py_ssize_t length) */ _PyUnicode_WSTR(unicode)[0] = 0; _PyUnicode_WSTR(unicode)[length] = 0; - _PyUnicode_WSTR_LENGTH(unicode) = length; - _PyUnicode_HASH(unicode) = -1; - _PyUnicode_STATE(unicode).interned = 0; - _PyUnicode_STATE(unicode).kind = 0; - _PyUnicode_STATE(unicode).compact = 0; - _PyUnicode_STATE(unicode).ready = 0; - _PyUnicode_STATE(unicode).ascii = 0; - _PyUnicode_DATA_ANY(unicode) = NULL; - _PyUnicode_LENGTH(unicode) = 0; - _PyUnicode_UTF8(unicode) = NULL; - _PyUnicode_UTF8_LENGTH(unicode) = 0; + assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); return unicode; }