McRogueFace/tests/regression/subclass_callback_segfault_test.py
John McCardle 6d5e99a114 Remove legacy string enum comparisons from InputState/Key/MouseButton, closes #306
Removed custom __eq__/__ne__ that allowed comparing enums to legacy string
names (e.g., Key.ESCAPE == "Escape"). Removed _legacy_names dicts and
to_legacy_string() functions. Kept from_legacy_string() in PyKey.cpp as
it's used by C++ event dispatch. Updated ~50 Python test/demo/cookbook
files to use enum members instead of string comparisons. Also updates
grid.position -> grid.pos in files that had both types of changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 22:19:02 -04:00

87 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Minimal reproduction of segfault when calling subclass method callback.
The issue: When a Frame subclass assigns self.on_click = self._on_click,
reading it back works but there's a segfault during cleanup.
"""
import mcrfpy
import sys
import gc
class MyFrame(mcrfpy.Frame):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.on_click = self._on_click
def _on_click(self, pos, button, action):
print(f"Clicked at {pos}, button={button}, action={action}")
def test_minimal():
"""Minimal test case."""
print("Creating MyFrame...")
obj = MyFrame(pos=(100, 100), size=(100, 100))
print(f"Reading on_click: {obj.on_click}")
print(f"Type: {type(obj.on_click)}")
print("Attempting to call on_click...")
try:
obj.on_click((50, 50), mcrfpy.MouseButton.LEFT, mcrfpy.InputState.PRESSED)
print("Call succeeded!")
except Exception as e:
print(f"Exception: {type(e).__name__}: {e}")
print("Clearing callback...")
obj.on_click = None
print("Deleting object...")
del obj
print("Running GC...")
gc.collect()
print("About to exit...")
sys.exit(0)
def test_without_callback_clear():
"""Test without clearing callback first."""
print("Creating MyFrame...")
obj = MyFrame(pos=(100, 100), size=(100, 100))
print("Calling...")
obj.on_click((50, 50), mcrfpy.MouseButton.LEFT, mcrfpy.InputState.PRESSED)
print("Deleting without clearing callback...")
del obj
gc.collect()
print("About to exit...")
sys.exit(0)
def test_added_to_scene():
"""Test when added to scene."""
print("Creating scene and MyFrame...")
scene = mcrfpy.Scene("test")
obj = MyFrame(pos=(100, 100), size=(100, 100))
scene.children.append(obj)
print("Calling via scene.children[0]...")
scene.children[0].on_click((50, 50), mcrfpy.MouseButton.LEFT, mcrfpy.InputState.PRESSED)
print("About to exit...")
sys.exit(0)
if __name__ == "__main__":
# Try different scenarios
import sys
if len(sys.argv) > 1:
if sys.argv[1] == "2":
test_without_callback_clear()
elif sys.argv[1] == "3":
test_added_to_scene()
else:
test_minimal()