102 lines
4.4 KiB
Python
102 lines
4.4 KiB
Python
"""Emit the standard exemplar scalars from Windows ICU for the frozen R01 candidate list."""
|
|
import ctypes as ct
|
|
import json
|
|
import sys
|
|
from contextlib import ExitStack
|
|
from pathlib import Path
|
|
|
|
icu = ct.CDLL("icu.dll", winmode=0x00000800) # LOAD_LIBRARY_SEARCH_SYSTEM32
|
|
integer = ct.c_int32
|
|
pointer = ct.c_void_p
|
|
status_ptr = ct.POINTER(integer)
|
|
|
|
|
|
def bind(name, result, *args):
|
|
function = getattr(icu, name)
|
|
function.argtypes = args
|
|
function.restype = result
|
|
return function
|
|
|
|
|
|
open_bundle = bind("ures_open", pointer, ct.c_char_p, ct.c_char_p, status_ptr)
|
|
get_key = bind("ures_getByKey", pointer, pointer, ct.c_char_p, pointer, status_ptr)
|
|
get_locale = bind("ures_getLocaleByType", ct.c_char_p, pointer, integer, status_ptr)
|
|
get_pattern = bind("ures_getString", pointer, pointer, status_ptr, status_ptr)
|
|
close_bundle = bind("ures_close", None, pointer)
|
|
open_set = bind("uset_openPattern", pointer, pointer, integer, status_ptr)
|
|
count_items = bind("uset_getItemCount", integer, pointer)
|
|
get_item = bind("uset_getItem", integer, pointer, integer, status_ptr, status_ptr, pointer, integer, status_ptr)
|
|
close_set = bind("uset_close", None, pointer)
|
|
likely_subtags = bind("uloc_addLikelySubtags", integer, ct.c_char_p, pointer, integer, status_ptr)
|
|
get_version = bind("u_getVersion", None, pointer)
|
|
|
|
|
|
def check(status):
|
|
if status.value > 0:
|
|
raise RuntimeError(f"ICU returned error {status.value}")
|
|
|
|
|
|
def script(locale):
|
|
buffer = ct.create_string_buffer(256)
|
|
status = integer()
|
|
likely_subtags(locale.encode("ascii"), buffer, len(buffer), ct.byref(status))
|
|
check(status)
|
|
return buffer.value.decode("ascii").split("_")[1]
|
|
|
|
|
|
def sample(tag):
|
|
requested = tag.replace("-", "_")
|
|
status = integer()
|
|
with ExitStack() as handles:
|
|
bundle = open_bundle(None, requested.encode("ascii"), ct.byref(status))
|
|
check(status)
|
|
handles.callback(close_bundle, bundle)
|
|
resource = get_key(bundle, b"ExemplarCharacters", None, ct.byref(status))
|
|
check(status)
|
|
handles.callback(close_bundle, resource)
|
|
# ACTUAL_LOCALE belongs to the individual inherited resource.
|
|
actual = get_locale(resource, 0, ct.byref(status)).decode("ascii")
|
|
check(status)
|
|
if not actual or requested.split("_")[0] != actual.split("_")[0] or script(requested) != script(actual):
|
|
return {"tag": tag, "source": actual or "root", "sample": "", "reason": "fallback-language-or-script"}
|
|
|
|
length = integer()
|
|
pattern = get_pattern(resource, ct.byref(length), ct.byref(status))
|
|
check(status)
|
|
exemplar = open_set(pattern, length, ct.byref(status))
|
|
check(status)
|
|
handles.callback(close_set, exemplar)
|
|
points = set()
|
|
for index in range(count_items(exemplar)):
|
|
first, last = integer(), integer()
|
|
# UnicodeSet items are ranges or strings such as Hungarian digraphs.
|
|
status = integer()
|
|
size = get_item(exemplar, index, ct.byref(first), ct.byref(last), None, 0, ct.byref(status))
|
|
if size > 0:
|
|
if status.value != 15: # U_BUFFER_OVERFLOW_ERROR is expected for string preflight.
|
|
check(status)
|
|
status = integer()
|
|
units = (ct.c_uint16 * (size + 1))()
|
|
got = get_item(exemplar, index, ct.byref(first), ct.byref(last), units, len(units), ct.byref(status))
|
|
check(status)
|
|
if got != size:
|
|
raise ValueError("Exemplar string changed during read")
|
|
points.update(bytes(units)[:2 * size].decode("utf-16-le"))
|
|
else:
|
|
check(status)
|
|
points.update(chr(code) for code in range(first.value, last.value + 1))
|
|
if not points:
|
|
raise ValueError(f"Empty exemplar for {tag}")
|
|
return {"tag": tag, "source": actual, "sample": "".join(sorted(points)), "reason": ""}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
tags = [r["tag"] for r in json.loads((Path(__file__).parent / "evidence/runtime-capabilities-r01/node-results.json").read_bytes())["results"]]
|
|
if len(set(tags)) != 60 or len(tags) != 60:
|
|
sys.exit("Unexpected frozen research corpus")
|
|
if sample("zz-ZZ")["sample"]:
|
|
sys.exit("Invalid control accepted host fallback")
|
|
version = (ct.c_uint8 * 4)()
|
|
get_version(version)
|
|
print(json.dumps({"icu": ".".join(map(str, version)), "results": [sample(tag) for tag in tags]}, ensure_ascii=True))
|