Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

## 0.156.4 (2026-11-27)

- **Fixed: `calendar.validate_calendar_config` names the profile's size bounds.** Since 0.156.0 the reader refuses a calendar past a bound (more than 256 eras, 64 months, a week of more than 64 steps or names, 16 festival days, an `except` longer than 8, a name or template longer than 256 characters, a year longer than 2^53 - 1 steps), but the validator said nothing about it. It now gives the npm SDK's words for each, such as `A calendar holds at most 256 eras`; a calendar with no months whose `year.steps` is past 2^53 - 1 included, which the npm SDK names from 4.212.2. Held to the npm SDK's validator on 3,000 randomized calendars near each bound, none differing.

## 0.156.3 (2026-11-27)

- **Faster: the depth check before a parse reads a dense file in about half the time of 0.156.1.** 0.156.1 made the check linear by stepping through every bracket, quote and backslash in Python, which on many small objects (50,000 of them, 2.2 MB) took 129 ms where 0.156.0 took 54. The strings are now cut out by one pattern that never backtracks, and the brackets left are counted as a running sum: 61 ms on the same file, and still linear on a string left open and full of escaped quotes (17 ms at 1 MB). Its answers were held to 0.156.1's on 500,000 random texts; none differed.
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.156.3"
__version__ = "0.156.4"
50 changes: 50 additions & 0 deletions src/onlyworlds/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,9 @@ def validate_calendar_config(cfg: Mapping[str, Any]) -> list[str]:
errors.append(f'Era "{name}": end must be after start')
if _strict_eq(direction, "down") and not _nullish(end) and _js_le(start, end):
errors.append(f'Era "{name}": a counting-down era must end below its start')
bound = _bound_words(cfg)
if bound:
errors.append(bound)
domains = _era_domains(_items(eras))
for i, a in enumerate(domains):
for b in domains[i + 1 :]:
Expand Down Expand Up @@ -767,6 +770,53 @@ def _past_a_bound(cfg: object, months: object, eras: list[Any], entries: list[An
return False


def _bound_words(cfg: object) -> str | None:
"""The words of the first size bound ``cfg`` is past, as the npm SDK's validator gives them, or None:
more than 256 eras, 64 months, a week of more than 64 steps or 64 names, 16 intercalary entries, a
version-2 ``except`` longer than 8, a name or template longer than 256 characters, or a longest year (the
months' lengths plus every intercalary entry, or with no months ``year.steps``) past 2**53 - 1. Shapes
it does not hold are left to the other checks."""

def listed(v: object) -> list[Any]:
return v if isinstance(v, list) else []

months, week = _bundle(cfg, "months"), _bundle(cfg, "week")
eras, inter = listed(_prop(cfg, "eras")), listed(_prop(cfg, "intercalary"))
month_names, day_names = listed(_prop(months, "names")), listed(_prop(week, "names"))
if len(eras) > _MAX_ERAS:
return f"A calendar holds at most {_MAX_ERAS} eras"
if len(month_names) > _MAX_MONTHS:
return f"A calendar holds at most {_MAX_MONTHS} months"
week_length = _prop(week, "length")
if (_is_num(week_length) and _num(week_length) > _MAX_WEEK) or len(day_names) > _MAX_WEEK:
return f"A week holds at most {_MAX_WEEK} steps and {_MAX_WEEK} names"
if len(inter) > _MAX_INTERCALARY:
return f"A calendar holds at most {_MAX_INTERCALARY} festival days"
if _strict_eq(_prop(cfg, "version"), 2) and any(len(listed(_prop(i, "except"))) > _MAX_EXCEPT for i in inter):
return f"An except list holds at most {_MAX_EXCEPT} numbers"
if (
any(_too_long(_prop(cfg, k)) for k in ("grain", "step_name", "format", "format_intercalary"))
or any(_too_long(_prop(e, "name")) or _too_long(_prop(e, "abbrev")) for e in eras)
or any(map(_too_long, month_names))
or any(map(_too_long, day_names))
or any(_too_long(_prop(i, "name")) for i in inter)
):
return f"A name or template holds at most {_MAX_TEXT} characters"
lengths = _prop(months, "lengths")
if _truthy(months) and isinstance(lengths, list):
longest = float(len(inter))
for length in lengths:
if _is_num(length):
longest = longest + _num(length) # left to right from the entries, as the SDK's validator sums
if longest > _SAFE:
return "A year holds at most 2^53 - 1 steps"
elif _truthy(_bundle(cfg, "year")):
steps = _prop(_bundle(cfg, "year"), "steps")
if _is_num(steps) and _num(steps) > _SAFE:
return "A year holds at most 2^53 - 1 steps"
return None


def _leap_rule_problem(ic: object) -> str | None:
"""Why an entry's version-2 leap keys can't be read, or None. ``except`` a list of whole numbers, each
above and a multiple of the one before (the first of ``every_years``); ``offset`` a whole number;
Expand Down
31 changes: 31 additions & 0 deletions tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,37 @@ def test_surface_helpers() -> None:
assert format_duration({"version": 1, "grain": "day", "step_name": "sol"}, 14) == "14 sols"


def test_validate_names_each_size_bound_as_the_sdk_does() -> None:
"""The SDK's words for each size bound (``calendar-kernel.test.mjs``), a year of ``year.steps`` with no
months included; the reader refuses each."""
day: dict[str, Any] = {"version": 2, "grain": "day"}

def words(cfg: Mapping[str, Any]) -> list[str]:
return [w for w in validate_calendar_config(cfg) if "at most" in w]

def names(n: int, p: str) -> list[str]:
return [f"{p}{i}" for i in range(n)]

eras = [{"name": n, "start": i * 10, "end": i * 10 + 10} for i, n in enumerate(names(257, "E"))]
assert words({**day, "eras": eras}) == ["A calendar holds at most 256 eras"]
months = {"months": {"names": names(65, "M"), "lengths": [30] * 65}}
assert words({**day, "bundles": months}) == ["A calendar holds at most 64 months"]
week = {"week": {"length": 65, "names": names(65, "D")}}
assert words({**day, "bundles": week}) == ["A week holds at most 64 steps and 64 names"]
two = {"months": {"names": ["A", "B"], "lengths": [30, 30]}}
festivals = [{"name": n, "after_month": 1} for n in names(17, "F")]
assert words({**day, "bundles": two, "intercalary": festivals}) == ["A calendar holds at most 16 festival days"]
leap = {"name": "F", "after_month": 1, "every_years": 4, "except": [8, 16, 32, 64, 128, 256, 512, 1024, 2048]}
assert words({**day, "bundles": two, "intercalary": [leap]}) == ["An except list holds at most 8 numbers"]
assert words({**day, "grain": "g" * 257}) == ["A name or template holds at most 256 characters"]
long_months = {"months": {"names": ["A", "B"], "lengths": [9007199254740990, 2]}}
assert words({**day, "bundles": long_months}) == ["A year holds at most 2^53 - 1 steps"]
past = {**day, "bundles": {"year": {"steps": 9007199254740992}}}
assert words(past) == ["A year holds at most 2^53 - 1 steps"]
assert words({**day, "bundles": {"year": {"steps": 9007199254740991}}}) == []
assert read_calendar_config(past) is None


def test_duration_wrong_kind_step_name_and_whole_counts() -> None:
"""A step_name that is not a string reads as absent; a whole count is exact up to the safe integers."""
day = {"version": 2, "grain": "day"}
Expand Down
Loading