Issue #18408: Fix PyDict_New() to handle correctly new_keys_object() failure
(MemoryError).
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 250c890..9d8696a 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -389,6 +389,7 @@
 new_dict(PyDictKeysObject *keys, PyObject **values)
 {
     PyDictObject *mp;
+    assert(keys != NULL);
     if (numfree) {
         mp = free_list[--numfree];
         assert (mp != NULL);
@@ -431,7 +432,10 @@
 PyObject *
 PyDict_New(void)
 {
-    return new_dict(new_keys_object(PyDict_MINSIZE_COMBINED), NULL);
+    PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
+    if (keys == NULL)
+        return NULL;
+    return new_dict(keys, NULL);
 }
 
 /*