Skip to content

Commit

Permalink
Detect the condition of an assertion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aermoss committed Feb 8, 2025
1 parent c5607b6 commit 6cc2e80
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions sdl3/SDL_assert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .__init__ import os, inspect, ctypes, \
from .__init__ import os, inspect, ctypes, re, \
SDL_FUNC, SDL_SET_CURRENT_BINARY, SDL_GET_BINARY, SDL_BINARY

SDL_SET_CURRENT_BINARY(SDL_BINARY)
Expand Down Expand Up @@ -33,17 +33,17 @@ class SDL_AssertData(ctypes.Structure):
SDL_AssertBreakpoint = lambda: SDL_TriggerBreakpoint()

def SDL_disabled_assert(condition: ctypes.c_bool) -> None:
...
"""Do not call this function directly."""

def SDL_enabled_assert(condition: ctypes.c_bool) -> None:
while not condition:
current = inspect.currentframe()

while "assert" in current.f_code.co_name or ("<lambda>" in current.f_code.co_name):
current = current.f_back
"""Do not call this function directly."""

while not condition:
data = SDL_AssertData()
data.condition = "<condition>".encode()
module = current = inspect.currentframe().f_back.f_back
while module.f_code.co_name != "<module>": module = module.f_back
match = re.search(r"\((.*?)\)", inspect.getsource(module).split("\n")[current.f_lineno - 1])
data.condition = (match.group(1) if match else "<condition>").encode()
state = SDL_GET_BINARY(SDL_BINARY).SDL_ReportAssertion \
(ctypes.byref(data), current.f_code.co_name.encode(), os.path.split(current.f_code.co_filename)[-1].encode(), current.f_lineno)

Expand Down

0 comments on commit 6cc2e80

Please # to comment.