From 1a3a8d9e7dc349d67c286880b74308713f10ef47 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 16:57:30 +0100 Subject: [PATCH 1/8] Simple implementation of pygame.debug --- src_py/__init__.py | 9 +++------ src_py/debug.py | 15 +++++++++++++++ src_py/meson.build | 1 + 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src_py/debug.py diff --git a/src_py/__init__.py b/src_py/__init__.py index 5f7e540547..e119aaf8f2 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": @@ -395,11 +396,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") # 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..530b8ed744 --- /dev/null +++ b/src_py/debug.py @@ -0,0 +1,15 @@ +import os +import platform +import warnings +import pygame +from pygame.base import get_sdl_version + +if "PYGAME_HIDE_SUPPORT_PROMPT" not in os.environ: + print( + f"pygame-ce {pygame.version.ver} (SDL {'.'.join(map(str, get_sdl_version()))}, " + f"Python {platform.python_version()})" + ) + + pygame.print_debug_info() + +warnings.filterwarnings("default") 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', From cc3ce86ebcee45c54dd76642b4d9859e01d37d5f Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 17:10:26 +0100 Subject: [PATCH 2/8] Simple implementation of pygame.debug --- src_py/debug.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src_py/debug.py b/src_py/debug.py index 530b8ed744..3c8525f424 100644 --- a/src_py/debug.py +++ b/src_py/debug.py @@ -2,14 +2,15 @@ import platform import warnings import pygame -from pygame.base import get_sdl_version if "PYGAME_HIDE_SUPPORT_PROMPT" not in os.environ: print( - f"pygame-ce {pygame.version.ver} (SDL {'.'.join(map(str, get_sdl_version()))}, " + 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") + +del os, platform, warnings From 67fda3837b2b53309763a0c59c0f923769c3837f Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 17:12:20 +0100 Subject: [PATCH 3/8] Simple implementation of pygame.debug --- src_py/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src_py/__init__.py b/src_py/__init__.py index e119aaf8f2..22690f581b 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -94,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: From aaeeb6bec36b3da5c67cfd731cc232c7f25fe3c2 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 17:45:24 +0100 Subject: [PATCH 4/8] Simple implementation of pygame.debug --- buildconfig/stubs/mypy_allow_list.txt | 1 + 1 file changed, 1 insertion(+) 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 From 1e67a15781a6e1460ecc2d790a6633da5284d88e Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 18:34:47 +0100 Subject: [PATCH 5/8] make set_length warn stack level 2, so it gets hidden by default. --- src_c/color.c | 2 +- src_py/__init__.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src_c/color.c b/src_c/color.c index 845a4c0314..77f39ecd90 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -1718,7 +1718,7 @@ _color_set_length(pgColorObject *color, PyObject *args) if (PyErr_WarnEx(PyExc_DeprecationWarning, "pygame.Color.set_length deprecated since 2.1.3", - 1) == -1) { + 2) == -1) { return NULL; } diff --git a/src_py/__init__.py b/src_py/__init__.py index 22690f581b..6b68217441 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -394,7 +394,5 @@ def __color_reduce(c): copyreg.pickle(Color, __color_reduce, __color_constructor) -warnings.filterwarnings("ignore") - # cleanup namespace del pygame, os, sys, platform, warnings, MissingModule, copyreg, packager_imports From 47727b13607ccb94c491c599dbc8b47eea4ab69c Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 18:36:13 +0100 Subject: [PATCH 6/8] revent __init__.py warning changes --- src_py/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src_py/__init__.py b/src_py/__init__.py index 6b68217441..476b9b2782 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -26,7 +26,6 @@ import os import sys import platform -import warnings # Choose Windows display driver if os.name == "nt": @@ -94,6 +93,8 @@ 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,4 +396,4 @@ def __color_reduce(c): copyreg.pickle(Color, __color_reduce, __color_constructor) # cleanup namespace -del pygame, os, sys, platform, warnings, MissingModule, copyreg, packager_imports +del pygame, os, sys, platform, MissingModule, copyreg, packager_imports From 5810b6eb22928963c5a56b9ac76632d7ab2af712 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 18:41:58 +0100 Subject: [PATCH 7/8] revert color.c changes --- src_c/color.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_c/color.c b/src_c/color.c index 77f39ecd90..845a4c0314 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -1718,7 +1718,7 @@ _color_set_length(pgColorObject *color, PyObject *args) if (PyErr_WarnEx(PyExc_DeprecationWarning, "pygame.Color.set_length deprecated since 2.1.3", - 2) == -1) { + 1) == -1) { return NULL; } From af331aae7b67a65168aa22cc0ac3665a5458b3d4 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 6 Oct 2024 19:59:47 +0100 Subject: [PATCH 8/8] 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