bpo-36722: Don't define ALT_SOABI for Py_TRACE_REFS build (GH-12973)

Py_TRACE_REFS ABI is incompatible with release and debug (Py_DEBUG)
ABI.
diff --git a/Include/pyport.h b/Include/pyport.h
index acbae5b..568ab8f 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -797,9 +797,9 @@
 #endif /* Py_BUILD_CORE */
 
 #ifdef __ANDROID__
-/* The Android langinfo.h header is not used. */
-#undef HAVE_LANGINFO_H
-#undef CODESET
+   /* The Android langinfo.h header is not used. */
+#  undef HAVE_LANGINFO_H
+#  undef CODESET
 #endif
 
 /* Maximum value of the Windows DWORD type */
@@ -810,7 +810,13 @@
  * for compatibility.
  */
 #ifndef WITH_THREAD
-#define WITH_THREAD
+#  define WITH_THREAD
+#endif
+
+/* Check that ALT_SOABI is consistent with Py_TRACE_REFS:
+   ./configure --with-trace-refs should must be used to define Py_TRACE_REFS */
+#if defined(ALT_SOABI) && defined(Py_TRACE_REFS)
+#  error "Py_TRACE_REFS ABI is not compatible with release and debug ABI"
 #endif
 
 #endif /* Py_PYPORT_H */
diff --git a/configure b/configure
index b2775cf..7f26fab 100755
--- a/configure
+++ b/configure
@@ -15128,7 +15128,8 @@
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $SOABI" >&5
 $as_echo "$SOABI" >&6; }
 
-if test "$Py_DEBUG" = 'true'; then
+# Release and debug (Py_DEBUG) ABI are compatible, but not Py_TRACE_REFS ABI
+if test "$Py_DEBUG" = 'true' -a "$with_trace_refs" != "yes"; then
   # Similar to SOABI but remove "d" flag from ABIFLAGS
 
   ALT_SOABI='cpython-'`echo $VERSION | tr -d .``echo $ABIFLAGS | tr -d d`${PLATFORM_TRIPLET:+-$PLATFORM_TRIPLET}
diff --git a/configure.ac b/configure.ac
index 3127587..157e9bf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4627,7 +4627,8 @@
 SOABI='cpython-'`echo $VERSION | tr -d .`${ABIFLAGS}${PLATFORM_TRIPLET:+-$PLATFORM_TRIPLET}
 AC_MSG_RESULT($SOABI)
 
-if test "$Py_DEBUG" = 'true'; then
+# Release and debug (Py_DEBUG) ABI are compatible, but not Py_TRACE_REFS ABI
+if test "$Py_DEBUG" = 'true' -a "$with_trace_refs" != "yes"; then
   # Similar to SOABI but remove "d" flag from ABIFLAGS
   AC_SUBST(ALT_SOABI)
   ALT_SOABI='cpython-'`echo $VERSION | tr -d .``echo $ABIFLAGS | tr -d d`${PLATFORM_TRIPLET:+-$PLATFORM_TRIPLET}