Skip to content

Commit

Permalink
add PygameDebugWarning type that is only active when pygame.debug is …
Browse files Browse the repository at this point in the history
…imported
  • Loading branch information
MyreMylar committed Oct 6, 2024
1 parent 5810b6e commit af331aa
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions buildconfig/stubs/pygame/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions buildconfig/stubs/pygame/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ __version__: str

class error(RuntimeError): ...
class BufferError(Exception): ...
class PygameDebugWarning(Warning): ...

# Always defined
HAVE_NEWBUF: int = 1
Expand Down
2 changes: 1 addition & 1 deletion src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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];

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src_c/include/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
7 changes: 4 additions & 3 deletions src_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import os
import sys
import platform
import warnings

# Choose Windows display driver
if os.name == "nt":
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src_py/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

pygame.print_debug_info()

warnings.filterwarnings("default")
warnings.filterwarnings("default", category=pygame.PygameDebugWarning)

del os, platform, warnings

0 comments on commit af331aa

Please sign in to comment.