bpo-39882: Py_FatalError() logs the function name (GH-18819)

The Py_FatalError() function is replaced with a macro which logs
automatically the name of the current function, unless the
Py_LIMITED_API macro is defined.

Changes:

* Add _Py_FatalErrorFunc() function.
* Remove the function name from the message of Py_FatalError() calls
  which included the function name.
* Update tests.
diff --git a/Python/ceval.c b/Python/ceval.c
index 20e32e2..04e0824 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -283,7 +283,7 @@
     struct _ceval_runtime_state *ceval = &runtime->ceval;
     PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
     if (tstate == NULL) {
-        Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
+        Py_FatalError("current thread state is NULL");
     }
     take_gil(ceval, tstate);
     exit_thread_if_finalizing(tstate);
@@ -314,7 +314,7 @@
     take_gil(ceval, tstate);
     exit_thread_if_finalizing(tstate);
     if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
-        Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
+        Py_FatalError("non-NULL old thread state");
     }
 }
 
@@ -326,7 +326,7 @@
     _PyRuntimeState *runtime = tstate->interp->runtime;
     PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
     if (new_tstate != tstate) {
-        Py_FatalError("PyEval_ReleaseThread: wrong thread state");
+        Py_FatalError("wrong thread state");
     }
     drop_gil(&runtime->ceval, tstate);
 }
@@ -373,7 +373,7 @@
     struct _ceval_runtime_state *ceval = &runtime->ceval;
     PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
     if (tstate == NULL) {
-        Py_FatalError("PyEval_SaveThread: NULL tstate");
+        Py_FatalError("NULL tstate");
     }
     assert(gil_created(&ceval->gil));
     drop_gil(ceval, tstate);
@@ -1236,7 +1236,7 @@
             if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
                 /* Give another thread a chance */
                 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
-                    Py_FatalError("ceval: tstate mix-up");
+                    Py_FatalError("tstate mix-up");
                 }
                 drop_gil(ceval, tstate);
 
@@ -1248,7 +1248,7 @@
                 exit_thread_if_finalizing(tstate);
 
                 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
-                    Py_FatalError("ceval: orphan tstate");
+                    Py_FatalError("orphan tstate");
                 }
             }
             /* Check for asynchronous exceptions. */
diff --git a/Python/import.c b/Python/import.c
index 392d711..c4a19bc 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -310,7 +310,7 @@
 {
     PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
     if (interp->modules == NULL) {
-        Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
+        Py_FatalError("no module dictionary");
     }
     return interp->modules;
 }
@@ -982,8 +982,7 @@
         _Py_IDENTIFIER(_get_sourcefile);
 
         if (interp == NULL) {
-            Py_FatalError("PyImport_ExecCodeModuleWithPathnames: "
-                          "no interpreter!");
+            Py_FatalError("no interpreter!");
         }
 
         external= PyObject_GetAttrString(interp->importlib,
diff --git a/Python/pathconfig.c b/Python/pathconfig.c
index e37b561..3756e3a 100644
--- a/Python/pathconfig.c
+++ b/Python/pathconfig.c
@@ -515,7 +515,7 @@
         || _Py_path_config.exec_prefix == NULL
         || _Py_path_config.module_search_path == NULL)
     {
-        Py_FatalError("Py_SetPath() failed: out of memory");
+        Py_FatalError("out of memory");
     }
 }
 
@@ -536,7 +536,7 @@
     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
 
     if (_Py_path_config.home == NULL) {
-        Py_FatalError("Py_SetPythonHome() failed: out of memory");
+        Py_FatalError("out of memory");
     }
 }
 
@@ -557,7 +557,7 @@
     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
 
     if (_Py_path_config.program_name == NULL) {
-        Py_FatalError("Py_SetProgramName() failed: out of memory");
+        Py_FatalError("out of memory");
     }
 }
 
@@ -577,7 +577,7 @@
     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
 
     if (_Py_path_config.program_full_path == NULL) {
-        Py_FatalError("_Py_SetProgramFullPath() failed: out of memory");
+        Py_FatalError("out of memory");
     }
 }
 
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 23d74ee..9e3b257 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -1611,10 +1611,10 @@
     PyInterpreterState *interp = tstate->interp;
 
     if (tstate != _PyThreadState_GET()) {
-        Py_FatalError("Py_EndInterpreter: thread is not current");
+        Py_FatalError("thread is not current");
     }
     if (tstate->frame != NULL) {
-        Py_FatalError("Py_EndInterpreter: thread still has a frame");
+        Py_FatalError("thread still has a frame");
     }
     interp->finalizing = 1;
 
@@ -1624,7 +1624,7 @@
     call_py_exitfuncs(tstate);
 
     if (tstate != interp->tstate_head || tstate->next != NULL) {
-        Py_FatalError("Py_EndInterpreter: not the last thread");
+        Py_FatalError("not the last thread");
     }
 
     _PyImport_Cleanup(tstate);
@@ -2241,6 +2241,8 @@
     }
 }
 
+#undef Py_FatalError
+
 void _Py_NO_RETURN
 Py_FatalError(const char *msg)
 {
@@ -2248,6 +2250,12 @@
 }
 
 void _Py_NO_RETURN
+_Py_FatalErrorFunc(const char *func, const char *msg)
+{
+    fatal_error(func, msg, -1);
+}
+
+void _Py_NO_RETURN
 Py_ExitStatusException(PyStatus status)
 {
     if (_PyStatus_IS_EXIT(status)) {
diff --git a/Python/pystate.c b/Python/pystate.c
index 9002669..a1eb523 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -327,20 +327,20 @@
     PyInterpreterState **p;
     for (p = &interpreters->head; ; p = &(*p)->next) {
         if (*p == NULL) {
-            Py_FatalError("PyInterpreterState_Delete: invalid interp");
+            Py_FatalError("invalid interp");
         }
         if (*p == interp) {
             break;
         }
     }
     if (interp->tstate_head != NULL) {
-        Py_FatalError("PyInterpreterState_Delete: remaining threads");
+        Py_FatalError("remaining threads");
     }
     *p = interp->next;
     if (interpreters->main == interp) {
         interpreters->main = NULL;
         if (interpreters->head != NULL) {
-            Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters");
+            Py_FatalError("remaining subinterpreters");
         }
     }
     HEAD_UNLOCK(runtime);
@@ -363,7 +363,7 @@
 
     PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
     if (tstate != NULL && tstate->interp != interpreters->main) {
-        Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter");
+        Py_FatalError("not main interpreter");
     }
 
     HEAD_LOCK(runtime);
@@ -389,7 +389,7 @@
     HEAD_UNLOCK(runtime);
 
     if (interpreters->head == NULL) {
-        Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main");
+        Py_FatalError("missing main");
     }
     _PyThreadState_Swap(gilstate, tstate);
 }
@@ -400,11 +400,11 @@
 {
     PyThreadState *tstate = _PyThreadState_GET();
     if (tstate == NULL) {
-        Py_FatalError("_PyInterpreterState_Get(): no current thread state");
+        Py_FatalError("no current thread state");
     }
     PyInterpreterState *interp = tstate->interp;
     if (interp == NULL) {
-        Py_FatalError("_PyInterpreterState_Get(): no current interpreter");
+        Py_FatalError("no current interpreter");
     }
     return interp;
 }
@@ -695,7 +695,7 @@
 PyState_AddModule(PyObject* module, struct PyModuleDef* def)
 {
     if (!def) {
-        Py_FatalError("PyState_AddModule: Module Definition is NULL");
+        Py_FatalError("Module Definition is NULL");
         return -1;
     }
 
@@ -706,7 +706,7 @@
         index < PyList_GET_SIZE(interp->modules_by_index) &&
         module == PyList_GET_ITEM(interp->modules_by_index, index))
     {
-        Py_FatalError("PyState_AddModule: Module already added!");
+        Py_FatalError("Module already added");
         return -1;
     }
     return _PyState_AddModule(tstate, module, def);
@@ -724,15 +724,15 @@
     }
     state = _PyInterpreterState_GET_UNSAFE();
     if (index == 0) {
-        Py_FatalError("PyState_RemoveModule: Module index invalid.");
+        Py_FatalError("Module index invalid");
         return -1;
     }
     if (state->modules_by_index == NULL) {
-        Py_FatalError("PyState_RemoveModule: Interpreters module-list not accessible.");
+        Py_FatalError("Interpreters module-list not accessible.");
         return -1;
     }
     if (index > PyList_GET_SIZE(state->modules_by_index)) {
-        Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
+        Py_FatalError("Module index out of bounds.");
         return -1;
     }
     Py_INCREF(Py_None);
@@ -819,11 +819,11 @@
 {
     _PyRuntimeState *runtime = tstate->interp->runtime;
     if (tstate == NULL) {
-        Py_FatalError("PyThreadState_Delete: NULL tstate");
+        Py_FatalError("NULL tstate");
     }
     PyInterpreterState *interp = tstate->interp;
     if (interp == NULL) {
-        Py_FatalError("PyThreadState_Delete: NULL interp");
+        Py_FatalError("NULL interp");
     }
     HEAD_LOCK(runtime);
     if (tstate->prev)
@@ -850,7 +850,7 @@
     struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
     if (check_current) {
         if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
-            Py_FatalError("PyThreadState_Delete: tstate is still current");
+            Py_FatalError("tstate is still current");
         }
     }
     tstate_delete_common(tstate, gilstate);
@@ -869,9 +869,9 @@
 {
     struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
     PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
-    if (tstate == NULL)
-        Py_FatalError(
-            "PyThreadState_DeleteCurrent: no current tstate");
+    if (tstate == NULL) {
+        Py_FatalError("no current tstate");
+    }
     tstate_delete_common(tstate, gilstate);
     _PyRuntimeGILState_SetThreadState(gilstate, NULL);
     PyEval_ReleaseLock();
@@ -932,9 +932,9 @@
 PyThreadState_Get(void)
 {
     PyThreadState *tstate = _PyThreadState_GET();
-    if (tstate == NULL)
-        Py_FatalError("PyThreadState_Get: no current thread");
-
+    if (tstate == NULL) {
+        Py_FatalError("no current thread");
+    }
     return tstate;
 }