Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simple start towards an implementation of import pygame.debug #3148

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions buildconfig/stubs/mypy_allow_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ pygame\.pypm
pygame\._sdl2\.mixer
pygame\.sysfont.*
pygame\.docs.*
pygame\.debug
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess that this change is here is that there's no debug.pyi stub, correct?

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
11 changes: 3 additions & 8 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,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
16 changes: 16 additions & 0 deletions src_py/debug.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of currently, using:

import pygame.debug
pygame.do_stuff()

Works, as also:

import pygame.debug
import pygame as pg
from pygame import do_stuff

pg.do_stuff()
do_stuff()

But not:

import pygame.debug as pg
from pygame.debug import do_stuff

pg.do_stuff()
do_stuff()

What's your stance on this? If the last behavio(u)r is desired, adding

from pygame import *

Or

def __getattr__(name):
    return getattr(pygame, name)

def __dir__():
    return dir(pygame)

Should suffice, but if doing so, the debug.pyi stub should be created (edit buildconfig/stubs/gen_stubs.py to copy __init__.pyi stub).

Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src_py/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ python_sources = files(
'camera.py',
'colordict.py',
'cursors.py',
'debug.py',
'freetype.py',
'ftfont.py',
'locals.py',
Expand Down
Loading