Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
using System.Text.Json;
namespace Laya.Tests;
/// <summary>
/// <see cref="PythonJsonReader"/> error texts against CPython 3.12 <c>json.loads</c>: the message, and the
/// <c>line L column C (char P)</c> position counted in code points. Expectations were produced by Python.
/// </summary>
public sealed class PythonJsonErrorTests
{
public static TheoryData<string, string?> Cases() => new()
{
{ "", "Expecting value: line 1 column 1 (char 0)" },
{ " ", "Expecting value: line 1 column 3 (char 2)" },
{ "[1,]", "Expecting value: line 1 column 4 (char 3)" },
{ "[,1]", "Expecting value: line 1 column 2 (char 1)" },
{ "{\"a\":1,}", "Expecting property name enclosed in double quotes: line 1 column 8 (char 7)" },
{ "{\"a\"", "Expecting ':' delimiter: line 1 column 5 (char 4)" },
{ "{\"a\":", "Expecting value: line 1 column 6 (char 5)" },
{ "{\"a\":1", "Expecting ',' delimiter: line 1 column 7 (char 6)" },
{ "[1", "Expecting ',' delimiter: line 1 column 3 (char 2)" },
{ "[", "Expecting value: line 1 column 2 (char 1)" },
{ "{", "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" },
{ "{,}", "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" },
{ "\"abc", "Unterminated string starting at: line 1 column 1 (char 0)" },
{ "\"a\u000ab\"", "Invalid control character at: line 1 column 3 (char 2)" },
{ "\"a\\x\"", "Invalid \\escape: line 1 column 3 (char 2)" },
{ "\"\\u12\"", "Invalid \\uXXXX escape: line 1 column 3 (char 2)" },
{ "\"\\u12zz\"", "Invalid \\uXXXX escape: line 1 column 3 (char 2)" },
{ "[1 2]", "Expecting ',' delimiter: line 1 column 4 (char 3)" },
{ "{\"a\" 1}", "Expecting ':' delimiter: line 1 column 6 (char 5)" },
{ "{1:2}", "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" },
{ "\u000a\u000a nul", "Expecting value: line 3 column 3 (char 4)" },
{ "tru", "Expecting value: line 1 column 1 (char 0)" },
{ "NaN1", "Extra data: line 1 column 4 (char 3)" },
{ "1.5.5", "Extra data: line 1 column 4 (char 3)" },
{ "1e", "Extra data: line 1 column 2 (char 1)" },
{ "01", "Extra data: line 1 column 2 (char 1)" },
{ "-", "Expecting value: line 1 column 1 (char 0)" },
{ "[-]", "Expecting value: line 1 column 2 (char 1)" },
{ "{\"a\":-}", "Expecting value: line 1 column 6 (char 5)" },
{ "x", "Expecting value: line 1 column 1 (char 0)" },
{ "\ufeff[1]", "Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)" },
{ "[1]\u000a x", "Extra data: line 2 column 2 (char 5)" },
{ "[1]]", "Extra data: line 1 column 4 (char 3)" },
{ "{\"a\":1 \"b\":2}", "Expecting ',' delimiter: line 1 column 8 (char 7)" },
{ "[\"a\",\u000a nope]", "Expecting value: line 2 column 3 (char 8)" },
{ "\"\\ud83d\\u0041\"", null },
{ "\"abc\\", "Unterminated string starting at: line 1 column 1 (char 0)" },
{ "\"abc\\u", "Invalid \\uXXXX escape: line 1 column 6 (char 5)" },
{ "[1,2,", "Expecting value: line 1 column 6 (char 5)" },
{ "{\"a\":1,\u000a\"b\"", "Expecting ':' delimiter: line 2 column 4 (char 11)" },
{ "\ud835\udc00 [ x", "Expecting value: line 1 column 1 (char 0)" },
{ "\"\ud835\udc00\" x", "Extra data: line 1 column 5 (char 4)" },
{ "{\"a\":[1,{\"b\":}]}", "Expecting value: line 1 column 14 (char 13)" },
{ "[1,\u000a\u000a ]", "Expecting value: line 3 column 3 (char 7)" },
{ "1 2", "Extra data: line 1 column 3 (char 2)" },
{ "\"\\", "Unterminated string starting at: line 1 column 1 (char 0)" },
{ "nul", "Expecting value: line 1 column 1 (char 0)" },
{ "-Infinit", "Expecting value: line 1 column 1 (char 0)" },
{ "Infinit", "Expecting value: line 1 column 1 (char 0)" },
{ "NaN NaN", "Extra data: line 1 column 5 (char 4)" },
{ "[\"a\" \"b\"]", "Expecting ',' delimiter: line 1 column 6 (char 5)" },
{ "{\"a\":\"b\" x}", "Expecting ',' delimiter: line 1 column 10 (char 9)" },
{ "{\"a\"x}", "Expecting ':' delimiter: line 1 column 5 (char 4)" },
{ "{x}", "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" },
{ "[\u0009\u000d\u000a1,\u0009]", "Expecting value: line 2 column 4 (char 7)" },
{ "{\"a\":1}}", "Extra data: line 1 column 8 (char 7)" },
{ "[\u000a1,\u000a2\u000a3]", "Expecting ',' delimiter: line 4 column 1 (char 7)" },
{ "\ud835\udc00\ud835\udc00 x", "Expecting value: line 1 column 1 (char 0)" },
{ "\"\u0001\"", "Invalid control character at: line 1 column 2 (char 1)" },
{ "\"ab\u001fcd\"", "Invalid control character at: line 1 column 4 (char 3)" },
{ "{\"a\": tru}", "Expecting value: line 1 column 7 (char 6)" },
{ "[1,\u00a02]", "Expecting value: line 1 column 4 (char 3)" },
{ "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", "Exceeds the limit (4300 digits) for integer string conversion: value has 4301 digits; use sys.set_int_max_str_digits() to increase the limit" },
{ "-11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", "Exceeds the limit (4300 digits) for integer string conversion: value has 4301 digits; use sys.set_int_max_str_digits() to increase the limit" },
{ "[11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111]", "Exceeds the limit (4300 digits) for integer string conversion: value has 4301 digits; use sys.set_int_max_str_digits() to increase the limit" },
{ "+1", "Expecting value: line 1 column 1 (char 0)" },
{ ".5", "Expecting value: line 1 column 1 (char 0)" },
{ "1.", "Extra data: line 1 column 2 (char 1)" },
{ "[01]", "Expecting ',' delimiter: line 1 column 3 (char 2)" },
{ "{\"a\":01}", "Expecting ',' delimiter: line 1 column 7 (char 6)" },
{ "{'a':1}", "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" },
{ "[1,2]x", "Extra data: line 1 column 6 (char 5)" },
{ "\"unterminated", "Unterminated string starting at: line 1 column 1 (char 0)" },
{ "{\"k\": \"v\", \"k2\"", "Expecting ':' delimiter: line 1 column 16 (char 15)" },
{ "\"\\q\"", "Invalid \\escape: line 1 column 2 (char 1)" },
{ "[true, nul]", "Expecting value: line 1 column 8 (char 7)" },
{ "{\"a\":{\"b\":{\"c\":}}}", "Expecting value: line 1 column 16 (char 15)" },
};
[Theory]
[MemberData(nameof(Cases))]
public void ErrorTextMatchesPython(string text, string? message)
{
if (message is null)
{
PythonJsonReader.Parse(text);
return;
}
Assert.Equal(message, Assert.ThrowsAny<JsonException>(() => PythonJsonReader.Parse(text)).Message);
}
private static string Nested(int levels, string inner) =>
"{\"cls_token\": \"[CLS]\", \"x\": " + new string('[', levels) + inner + new string(']', levels) + "}";
// Python loads this much nesting (RecursionError past 2997 on 3.12); a re-emitted tokenizer_config must too.
[Theory]
[InlineData(10)]
[InlineData(65)]
[InlineData(900)]
[InlineData(2990)]
public void DeepNestingSurvivesTheReEmission(int levels)
{
using var doc = PythonJsonReader.ParseDocument(Nested(levels, "1"), out _);
Assert.Equal("[CLS]", doc.RootElement.GetProperty("cls_token").GetString());
var depth = 0;
var element = doc.RootElement.GetProperty("x");
while (element.ValueKind == JsonValueKind.Array) { depth++; element = element[0]; }
Assert.Equal(levels, depth);
}
[Fact]
public void DeepNestingNeedsNoStackProportionalToItsDepth()
{
// 2990 levels on a 256 KB thread. Neither the parse nor the re-emission may recurse, because the
// caller's stack is the one thing this port cannot ask anyone to make bigger. Before both walks became
// iterative this test killed the test host outright ("Stack overflow ... Abort trap: 6") -- which is
// the defect itself: .NET cannot catch a stack overflow, so a document CPython loads had no way to be
// reported as an error either.
var text = Nested(2990, "1");
Exception? caught = null;
var thread = new Thread(
() =>
{
try
{
using var doc = PythonJsonReader.ParseDocument(text, out _);
if (doc.RootElement.GetProperty("cls_token").GetString() != "[CLS]")
throw new InvalidOperationException("re-emitted payload lost a field");
var depth = 0;
var element = doc.RootElement.GetProperty("x");
while (element.ValueKind == JsonValueKind.Array) { depth++; element = element[0]; }
if (depth != 2990) throw new InvalidOperationException($"depth {depth} != 2990");
}
catch (Exception e) { caught = e; }
},
maxStackSize: 256 * 1024);
thread.Start();
thread.Join();
Assert.Null(caught);
}
[Fact]
public void NestingPastPythonsLimitIsRejected() =>
Assert.ThrowsAny<JsonException>(() => PythonJsonReader.Parse(new string((char)91, 2998) + new string((char)93, 2998)));
[Fact]
public void ANestingAtPythonsLimitLoads() =>
Assert.IsType<List<object?>>(PythonJsonReader.Parse(new string((char)91, 2997) + new string((char)93, 2997)));
[Fact]
public void AReadTokenFieldThatCannotSurviveReEmissionIsFlagged()
{
PythonJsonReader.ParseDocument("{\"cls_token\": NaN, \"sep_token\": \"a\", \"pad_token\": \"\\ud800\"}", out var tree);
var fields = Assert.IsAssignableFrom<IReadOnlyDictionary<string, object?>>(tree);
Assert.True(PythonJsonReader.IsAlteredByReEmission(fields["cls_token"]));
Assert.False(PythonJsonReader.IsAlteredByReEmission(fields["sep_token"]));
Assert.True(PythonJsonReader.IsAlteredByReEmission(fields["pad_token"]));
}
}