bpo-35436: Add missing PyErr_NoMemory() calls and other minor bug fixes. (GH-11015)

Set MemoryError when appropriate, add missing failure checks,
and fix some potential leaks.
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index b83befb..d56c91c 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -2142,7 +2142,7 @@
     if (n > NUM_STACK_ELEMS) {
         diffs = (double *) PyObject_Malloc(n * sizeof(double));
         if (diffs == NULL) {
-            return NULL;
+            return PyErr_NoMemory();
         }
     }
     for (i=0 ; i<n ; i++) {
@@ -2199,8 +2199,9 @@
     n = PyTuple_GET_SIZE(args);
     if (n > NUM_STACK_ELEMS) {
         coordinates = (double *) PyObject_Malloc(n * sizeof(double));
-        if (coordinates == NULL)
-            return NULL;
+        if (coordinates == NULL) {
+            return PyErr_NoMemory();
+        }
     }
     for (i=0 ; i<n ; i++) {
         item = PyTuple_GET_ITEM(args, i);