From af331aae7b67a65168aa22cc0ac3665a5458b3d4 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 19:59:47 +0100 Subject: [PATCH] add PygameDebugWarning type that is only active when pygame.debug is imported --- buildconfig/stubs/pygame/__init__.pyi | 1 + buildconfig/stubs/pygame/base.pyi | 1 + src_c/_pygame.h | 2 +- src_c/base.c | 14 +++++++++++++- src_c/include/_pygame.h | 2 ++ src_py/__init__.py | 7 ++++--- src_py/debug.py | 2 +- 7 files changed, 23 insertions(+), 6 deletions(-) diff --git a/buildconfig/stubs/pygame/__init__.pyi b/buildconfig/stubs/pygame/__init__.pyi index a8ecdd7f25..035cbfd138 100644 --- a/buildconfig/stubs/pygame/__init__.pyi +++ b/buildconfig/stubs/pygame/__init__.pyi @@ -70,6 +70,7 @@ from .base import ( quit as quit, register_quit as register_quit, set_error as set_error, + PygameDebugWarning as PygameDebugWarning, ) from .rwobject import ( diff --git a/buildconfig/stubs/pygame/base.pyi b/buildconfig/stubs/pygame/base.pyi index 4aa5d42149..c41801d046 100644 --- a/buildconfig/stubs/pygame/base.pyi +++ b/buildconfig/stubs/pygame/base.pyi @@ -4,6 +4,7 @@ __version__: str class error(RuntimeError): ... class BufferError(Exception): ... +class PygameDebugWarning(Warning): ... # Always defined HAVE_NEWBUF: int = 1 diff --git a/src_c/_pygame.h b/src_c/_pygame.h index e87986d776..650522e460 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -533,7 +533,7 @@ typedef enum { #define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2 #define PYGAMEAPI_COLOR_NUMSLOTS 5 #define PYGAMEAPI_MATH_NUMSLOTS 2 -#define PYGAMEAPI_BASE_NUMSLOTS 29 +#define PYGAMEAPI_BASE_NUMSLOTS 30 #define PYGAMEAPI_EVENT_NUMSLOTS 10 #define PYGAMEAPI_WINDOW_NUMSLOTS 1 #define PYGAMEAPI_GEOMETRY_NUMSLOTS 1 diff --git a/src_c/base.c b/src_c/base.c index 573bca3aa2..d578e9a862 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -2225,6 +2225,7 @@ static PyMethodDef _base_methods[] = { // generated at runtime. // when building static make global accessible symbol directly. static PyObject *pgExc_SDLError; +static PyObject *pgExc_PygameDebugWarning; #endif MODINIT_DEFINE(base) @@ -2234,6 +2235,7 @@ MODINIT_DEFINE(base) #if !(defined(BUILD_STATIC) && defined(NO_PYGAME_C_API)) // only pointer via C-api will be used, no need to keep global. PyObject *pgExc_SDLError; + PyObject *pgExc_PygameDebugWarning; #endif static void *c_api[PYGAMEAPI_BASE_NUMSLOTS]; @@ -2284,6 +2286,15 @@ MODINIT_DEFINE(base) goto error; } + /* create the exceptions */ + pgExc_PygameDebugWarning = + PyErr_NewException("pygame.PygameDebugWarning", PyExc_Warning, NULL); + if (PyModule_AddObject(module, "PygameDebugWarning", + pgExc_PygameDebugWarning)) { + Py_XDECREF(pgExc_PygameDebugWarning); + goto error; + } + /* export the c api */ c_api[0] = pgExc_SDLError; c_api[1] = pg_RegisterQuit; @@ -2314,8 +2325,9 @@ MODINIT_DEFINE(base) c_api[26] = pg_TwoDoublesFromFastcallArgs; c_api[27] = pg_GetDefaultConvertFormat; c_api[28] = pg_SetDefaultConvertFormat; + c_api[29] = pgExc_PygameDebugWarning; -#define FILLED_SLOTS 29 +#define FILLED_SLOTS 30 #if PYGAMEAPI_BASE_NUMSLOTS != FILLED_SLOTS #error export slot count mismatch diff --git a/src_c/include/_pygame.h b/src_c/include/_pygame.h index 5ff4882dfb..8700074afe 100644 --- a/src_c/include/_pygame.h +++ b/src_c/include/_pygame.h @@ -189,6 +189,8 @@ typedef struct pg_bufferinfo_s { #define pg_SetDefaultConvertFormat \ (*(SDL_PixelFormat * (*)(Uint32)) PYGAMEAPI_GET_SLOT(base, 28)) +#define pgExc_PygameDebugWarning ((PyObject *)PYGAMEAPI_GET_SLOT(base, 29)) + #define import_pygame_base() IMPORT_PYGAME_MODULE(base) #endif /* ~PYGAMEAPI_BASE_INTERNAL */ diff --git a/src_py/__init__.py b/src_py/__init__.py index 476b9b2782..359119b7d1 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -26,6 +26,7 @@ import os import sys import platform +import warnings # Choose Windows display driver if os.name == "nt": @@ -93,8 +94,6 @@ def warn(self): msg_type = "import" if self.urgent else "use" message = f"{msg_type} {self.name}: {self.info}\n({self.reason})" try: - import warnings - level = 4 if self.urgent else 3 warnings.warn(message, RuntimeWarning, level) except ImportError: @@ -395,5 +394,7 @@ def __color_reduce(c): copyreg.pickle(Color, __color_reduce, __color_constructor) +warnings.filterwarnings("ignore", category=pygame.base.PygameDebugWarning) + # cleanup namespace -del pygame, os, sys, platform, MissingModule, copyreg, packager_imports +del pygame, os, sys, platform, warnings, MissingModule, copyreg, packager_imports diff --git a/src_py/debug.py b/src_py/debug.py index 3c8525f424..c427c11d44 100644 --- a/src_py/debug.py +++ b/src_py/debug.py @@ -11,6 +11,6 @@ pygame.print_debug_info() -warnings.filterwarnings("default") +warnings.filterwarnings("default", category=pygame.PygameDebugWarning) del os, platform, warnings