Skip to content

Commit 5fbd014

Browse files
committed
Fix crash on permission errors.
1 parent 3f77a01 commit 5fbd014

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

sdl3/__init__.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -546,31 +546,40 @@ def SDL_GET_OR_GENERATE_DOCS() -> bytes:
546546
if __doc_generator__:
547547
import sdl3.SDL as raw
548548

549-
SDL_VERSIONNUM_STRING: abc.Callable[[int], str] = lambda num: \
549+
SDL_VERSIONNUM_STRING: abc.Callable[[int], str] = lambda num: str(None).lower() if not num else \
550550
f"{SDL_VERSIONNUM_MAJOR(num)}.{SDL_VERSIONNUM_MINOR(num)}.{SDL_VERSIONNUM_MICRO(num)}"
551551

552552
if not __initialized__:
553553
if int(os.environ.get("SDL_CHECK_BINARY_VERSION", "1")) > 0:
554-
for binary, left, right in zip(SDL_BINARY_VAR_MAP.values(), [SDL_GetVersion(), IMG_Version(), Mix_Version(), TTF_Version(), RTF_Version(), SDLNet_Version()], [SDL_VERSION, SDL_IMAGE_VERSION, SDL_MIXER_VERSION, SDL_TTF_VERSION, SDL_RTF_VERSION, SDL_NET_VERSION]):
555-
if left != right: print("\33[35m", f"warning: version mismatch with binary: '{SDL_BINARY_PATTERNS[SDL_SYSTEM][0].format(binary)}' (expected: {SDL_VERSIONNUM_STRING(right)}, got: {SDL_VERSIONNUM_STRING(left)}).", "\33[0m", sep = "", flush = True)
554+
for binary, left, right in zip(SDL_BINARY_VAR_MAP.values(), [SDL_GetVersion, IMG_Version, Mix_Version, TTF_Version, RTF_Version, SDLNet_Version], [SDL_VERSION, SDL_IMAGE_VERSION, SDL_MIXER_VERSION, SDL_TTF_VERSION, SDL_RTF_VERSION, SDL_NET_VERSION]):
555+
if binary in binaryMap and (_ := left()) != right: print("\33[35m", f"warning: version mismatch with binary: '{SDL_BINARY_PATTERNS[SDL_SYSTEM][0].format(binary)}' (expected: {SDL_VERSIONNUM_STRING(right)}, got: {SDL_VERSIONNUM_STRING(_)}).", "\33[0m", sep = "", flush = True)
556556

557-
if __doc_generator__:
558-
if not os.path.exists(__doc_file__):
557+
def SDL_TRY_WRITE_DOCS() -> None:
558+
try:
559559
with open(__doc_file__, "wb") as file:
560560
file.write(SDL_GET_OR_GENERATE_DOCS())
561561

562+
except OSError as exc:
563+
print("\33[31m", f"error: failed to write docs: {str(exc).lower()}.", "\33[0m", sep = "", flush = True)
564+
565+
if __doc_generator__:
566+
if not os.path.exists(__doc_file__):
567+
SDL_TRY_WRITE_DOCS()
568+
562569
from .__doc__ import *
563570
try: exec(getattr(__doc__, "__doc__"), data := {})
564571
except SyntaxError: data = None
565572

566573
if not data or data["meta"]["target"] != f"v{__version__}" or data["meta"]["system"] != SDL_SYSTEM:
567-
with open(__doc_file__, "wb") as file:
568-
file.write(SDL_GET_OR_GENERATE_DOCS())
569-
574+
SDL_TRY_WRITE_DOCS()
570575
del sys.modules["sdl3.__doc__"]
571576
print("\33[35m", f"warning: reloading module: 'sdl3.__doc__'.", "\33[0m", sep = "", flush = True)
572577
from .__doc__ import *
573578

574579
else:
575-
if os.path.exists(__doc_file__):
576-
os.remove(__doc_file__)
580+
try:
581+
if not __frozen__ and int(os.environ.get("SDL_DEBUG", "0")) > 0:
582+
if os.path.exists(__doc_file__): os.remove(__doc_file__)
583+
584+
except PermissionError as exc:
585+
print("\33[31m", f"error: failed to remove docs: '{str(exc).lower()}'.", "\33[0m", sep = "", flush = True)

0 commit comments

Comments
 (0)