Repository navigation
Prevent empty registry rows from retaining unused entities #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HexDecimal
merged 3 commits into
HexDecimal:main
from
antonsoo:fix/prevent-empty-entity-rows
Oct 18, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| """Regression tests for unreferenced entity lifetimes.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import gc | ||
| import weakref | ||
|
|
||
| import pytest | ||
|
|
||
| from tcod.ecs import Registry | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "operation", | ||
| [ | ||
| "clear", | ||
| "clear_empty", | ||
| "clear_tags", | ||
| "discard_missing_tag", | ||
| "read_missing_component", | ||
| "read_inherited_component", | ||
| "delete_missing_component", | ||
| "delete_missing_relation_component", | ||
| "delete_missing_relation_component_type", | ||
| ], | ||
| ) | ||
| def test_unused_entity_is_released(operation: str) -> None: | ||
| """Reads and cleanup must leave no registry reference to an unused entity.""" | ||
| registry = Registry() | ||
| prefab = registry["prefab"] | ||
| prefab_value = 7 | ||
| prefab.components[int] = prefab_value | ||
| entity = prefab.instantiate() if operation == "read_inherited_component" else registry.new_entity() | ||
| if operation == "clear": | ||
| entity.components[int] = 3 | ||
| entity.tags.add("inventory") | ||
| entity.clear() | ||
| elif operation == "clear_empty": | ||
| entity.clear() | ||
| elif operation == "clear_tags": | ||
| entity.tags.add("inventory") | ||
| entity.tags.clear() | ||
| elif operation == "discard_missing_tag": | ||
| entity.tags.discard("inventory") | ||
| elif operation == "read_missing_component": | ||
| assert entity.components.get(str) is None | ||
| elif operation == "read_inherited_component": | ||
| assert entity.components[int] == prefab_value | ||
| entity.relation_tags_many.clear() | ||
| elif operation == "delete_missing_component": | ||
| with pytest.raises(KeyError, match="str"): | ||
| del entity.components[str] | ||
| elif operation == "delete_missing_relation_component": | ||
| with pytest.raises(KeyError, match="target"): | ||
| del entity.relation_components[str][registry["target"]] | ||
| else: | ||
| assert operation == "delete_missing_relation_component_type" | ||
| entity.relation_components[int][prefab] = 3 | ||
| with pytest.raises(KeyError, match="target"): | ||
| del entity.relation_components[str][registry["target"]] | ||
| assert entity.relation_components[int] == {prefab: 3} | ||
| del entity.relation_components[int][prefab] | ||
| reference = weakref.ref(entity) | ||
| del entity | ||
| gc.collect() | ||
| assert reference() is None | ||
| assert registry["prefab"] is prefab | ||
| assert prefab.components[int] == prefab_value | ||
|
|
||
|
|
||
| def test_incoming_relation_retains_cleared_entity() -> None: | ||
| """An incoming relation intentionally keeps its target alive until removed.""" | ||
| registry = Registry() | ||
| owner, target = registry["owner"], registry.new_entity() | ||
| owner.relation_tag["target"] = target | ||
| reference = weakref.ref(target) | ||
| target.clear() | ||
| del target | ||
| gc.collect() | ||
| assert reference() is not None | ||
| del owner.relation_tag["target"] | ||
| gc.collect() | ||
| assert reference() is None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.