Skip to content

Python tests: check that the registration order doesn't depend on the fragment link order - #43

Open
Fedr wants to merge 4 commits into
masterfrom
deterministic-type-order
Open

Python tests: check that the registration order doesn't depend on the fragment link order#43
Fedr wants to merge 4 commits into
masterfrom
deterministic-type-order

Conversation

@Fedr

@Fedr Fedr commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Adds a regression check for the deterministic registration order of the Python bindings, locking in the guarantees introduced by 236ba96 (deterministic topological type sort) and f2764c5 (deterministic alias ordering).

What it checks

The types and aliases are registered by static initializers, so their registration order across the fragment TUs follows the link order, and until the two commits above the bindings visibly depended on it: relinking the same objects in a different order moved the __init__ overloads injected from conversion operators (which affects pybind11's overload resolution), and shuffled the Aliases: docstring lines.

test/check_py_registration_order.sh:

  1. generates bindings for test/order_check/input/MR/order.h — 8 classes with conversion operators into one Target class, plus 5 type aliases of it, deliberately spread across the file so that they land in different fragments (adjacent declarations would end up in the same fragment and could never shuffle);
  2. compiles them as 4 fragments and links the same object files in two orders: 0 1 2 3 and 0 3 2 1;
  3. imports both modules and requires their MRBIND_DEBUG=2 type/alias registration log, Target's docstring, and its __init__ overload list to be identical.

Status

  • On current master the check passes: the full MRBIND_DEBUG=2 dump is byte-identical between the two link orders.
  • On 236ba96 it failed on the Aliases: docstring line; before 236ba96 also on the type registration order and the injected __init__ overload positions:
-    3. __init__(self: example.MR.Target, arg0: example.MR.SrcCharlie) -> None
+    3. __init__(self: example.MR.Target, arg0: example.MR.SrcDelta) -> None

The branch history keeps the earlier implementation commits (now superseded by the master ones and reverted), so the net diff is just the check and its input.

Fedr added 2 commits August 7, 2026 20:06
…rden the deterministic type sort

The Aliases: docstring lines were emitted in the iteration order of
Registry::type_aliases, which follows the static-init (= link) order of the
fragment TUs, so they could still differ between two builds of identical
source - sort them, and load the aliases themselves in sorted order too (when
two alias spellings map to the same Python name, the winner used to depend on
that iteration order). Also tie-break exact type-name collisions in the
topological sort by the mangled typeid name (std::sort is unstable, so equal
keys would keep layout-dependent order), include <algorithm>/<cstring>
explicitly instead of transitively, and drop the inner sorted_rdeps that
shadowed the buffer meant to be reused across iterations.
…ation

test/check_py_registration_order.sh builds a small input as 4 fragments,
links the same objects in two different orders, and requires the resulting
modules to have identical type registration order, alias registration,
docstrings, and injected __init__ overload order. The aliases in the input
are deliberately spread across the file so that they land in different
fragments.
@Fedr
Fedr force-pushed the deterministic-type-order branch from 036c171 to adc7e99 Compare August 7, 2026 17:07
@Fedr Fedr changed the title Python: make the type registration order deterministic Python: deterministic alias ordering + a link-order regression check Aug 7, 2026
Fedr added 2 commits August 7, 2026 22:53
@Fedr Fedr changed the title Python: deterministic alias ordering + a link-order regression check Python: add a link-order determinism regression check Aug 7, 2026
@Fedr Fedr changed the title Python: add a link-order determinism regression check Python tests: check that the registration order doesn't depend on the fragment link order Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant