bpo-1635741: _PyUnicode_Name_CAPI moves to internal C API (GH-22713)

The private _PyUnicode_Name_CAPI structure of the PyCapsule API
unicodedata.ucnhash_CAPI moves to the internal C API. Moreover, the
structure gets a new state member which must be passed to the
getcode() and getname() functions.

* Move Include/ucnhash.h to Include/internal/pycore_ucnhash.h
* unicodedata module is now built with Py_BUILD_CORE_MODULE.
* unicodedata: move hashAPI variable into unicodedata_module_state.
diff --git a/Modules/Setup b/Modules/Setup
index 87f3a7c..6f9bb81 100644
--- a/Modules/Setup
+++ b/Modules/Setup
@@ -185,7 +185,7 @@
 #_json -I$(srcdir)/Include/internal -DPy_BUILD_CORE_BUILTIN _json.c	# _json speedups
 #_statistics _statisticsmodule.c # statistics accelerator
 
-#unicodedata unicodedata.c    # static Unicode character database
+#unicodedata unicodedata.c -DPy_BUILD_CORE_BUILTIN   # static Unicode character database
 
 
 # Modules with some UNIX dependencies -- on by default:
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c
index 941fd2f..bfd8ab5 100644
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -16,7 +16,7 @@
 #define PY_SSIZE_T_CLEAN
 
 #include "Python.h"
-#include "ucnhash.h"
+#include "pycore_ucnhash.h"       // _PyUnicode_Name_CAPI
 #include "structmember.h"         // PyMemberDef
 
 #include <stdbool.h>
@@ -97,6 +97,8 @@ typedef struct {
     // Borrowed reference to &UCD_Type. It is used to prepare the code
     // to convert the UCD_Type static type to a heap type.
     PyTypeObject *ucd_type;
+
+    _PyUnicode_Name_CAPI capi;
 } unicodedata_module_state;
 
 // bpo-1635741: Temporary global state until the unicodedata module
@@ -1180,10 +1182,11 @@ _getucname(unicodedata_module_state *state, PyObject *self,
 }
 
 static int
-capi_getucname(PyObject *self, Py_UCS4 code, char* buffer, int buflen,
+capi_getucname(void *state_raw, PyObject *self, Py_UCS4 code,
+               char* buffer, int buflen,
                int with_alias_and_seq)
 {
-    unicodedata_module_state *state = &global_module_state;
+    unicodedata_module_state *state = (unicodedata_module_state *)state_raw;
     return _getucname(state, self, code, buffer, buflen, with_alias_and_seq);
 
 }
@@ -1323,21 +1326,15 @@ _getcode(unicodedata_module_state *state, PyObject* self,
 }
 
 static int
-capi_getcode(PyObject* self, const char* name, int namelen, Py_UCS4* code,
+capi_getcode(void *state_raw, PyObject* self,
+             const char* name, int namelen, Py_UCS4* code,
              int with_named_seq)
 {
-    unicodedata_module_state *state = &global_module_state;
+    unicodedata_module_state *state = (unicodedata_module_state *)state_raw;
     return _getcode(state, self, name, namelen, code, with_named_seq);
 
 }
 
-static const _PyUnicode_Name_CAPI hashAPI =
-{
-    sizeof(_PyUnicode_Name_CAPI),
-    capi_getucname,
-    capi_getcode
-};
-
 /* -------------------------------------------------------------------- */
 /* Python bindings */
 
@@ -1510,6 +1507,11 @@ PyInit_unicodedata(void)
     PyObject *m, *v;
     unicodedata_module_state *state = &global_module_state;
 
+    state->capi.size = sizeof(_PyUnicode_Name_CAPI);
+    state->capi.state = state;
+    state->capi.getname = capi_getucname;
+    state->capi.getcode = capi_getcode;
+
     Py_SET_TYPE(&UCD_Type, &PyType_Type);
     state->ucd_type = &UCD_Type;
 
@@ -1528,7 +1530,7 @@ PyInit_unicodedata(void)
         PyModule_AddObject(m, "ucd_3_2_0", v);
 
     /* Export C API */
-    v = PyCapsule_New((void *)&hashAPI, PyUnicodeData_CAPSULE_NAME, NULL);
+    v = PyCapsule_New((void *)&state->capi, PyUnicodeData_CAPSULE_NAME, NULL);
     if (v != NULL)
         PyModule_AddObject(m, "ucnhash_CAPI", v);
     return m;