diff --git a/buildconfig/stubs/mypy_allow_list.txt b/buildconfig/stubs/mypy_allow_list.txt index 1e7c532d72..2dbc9f3d1d 100644 --- a/buildconfig/stubs/mypy_allow_list.txt +++ b/buildconfig/stubs/mypy_allow_list.txt @@ -27,3 +27,4 @@ pygame\.pypm pygame\._sdl2\.mixer pygame\.sysfont.* pygame\.docs.* +pygame\.debug 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 5f7e540547..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,11 +394,7 @@ def __color_reduce(c): copyreg.pickle(Color, __color_reduce, __color_constructor) -if "PYGAME_HIDE_SUPPORT_PROMPT" not in os.environ: - print( - f"pygame-ce {ver} (SDL {'.'.join(map(str, get_sdl_version()))}, " - f"Python {platform.python_version()})" - ) +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 new file mode 100644 index 0000000000..c427c11d44 --- /dev/null +++ b/src_py/debug.py @@ -0,0 +1,16 @@ +import os +import platform +import warnings +import pygame + +if "PYGAME_HIDE_SUPPORT_PROMPT" not in os.environ: + print( + f"pygame-ce {pygame.version.ver} (SDL {'.'.join(map(str, pygame.base.get_sdl_version()))}, " + f"Python {platform.python_version()})" + ) + + pygame.print_debug_info() + +warnings.filterwarnings("default", category=pygame.PygameDebugWarning) + +del os, platform, warnings diff --git a/src_py/meson.build b/src_py/meson.build index 541c54cd69..44c75884ae 100644 --- a/src_py/meson.build +++ b/src_py/meson.build @@ -8,6 +8,7 @@ python_sources = files( 'camera.py', 'colordict.py', 'cursors.py', + 'debug.py', 'freetype.py', 'ftfont.py', 'locals.py',