Skip to content

Commit a82eaef

Browse files
authored
Merge pull request #3496 from pmp-p/wasm/py313
2 parents a3a5568 + a12844e commit a82eaef

File tree

3 files changed

+136
-45
lines changed

3 files changed

+136
-45
lines changed

src_c/static.c

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ import_pygame_surface(void)
4949
{
5050
}
5151

52+
void
53+
import_pygame_window(void)
54+
{
55+
}
56+
5257
void
5358
import_pygame_geometry(void)
5459
{
@@ -59,6 +64,16 @@ import_pygame_color(void)
5964
{
6065
}
6166

67+
void
68+
import_pygame_font(void)
69+
{
70+
}
71+
72+
void
73+
import_pygame_freetype(void)
74+
{
75+
}
76+
6277
void
6378
import_pygame_bufferproxy(void)
6479
{
@@ -79,8 +94,22 @@ import_pygame_joystick(void)
7994
{
8095
}
8196

97+
#undef import_pygame_imageext
98+
#undef import_pygame_render
99+
#undef import_pygame_pixelarray
100+
82101
void
83-
import_pygame_window(void)
102+
import_pygame_imageext(void)
103+
{
104+
}
105+
106+
void
107+
import_pygame_render(void)
108+
{
109+
}
110+
111+
void
112+
import_pygame_pixelarray(void)
84113
{
85114
}
86115

@@ -198,24 +227,30 @@ void
198227
load_submodule(const char *parent, PyObject *mod, const char *alias)
199228
{
200229
char fqn[1024];
201-
snprintf(fqn, sizeof(fqn), "%s.%s", parent, alias);
202-
203-
PyObject *modules = PyImport_GetModuleDict();
204-
205-
PyObject *pmod = PyDict_GetItemString(modules, parent);
206-
207230
if (!mod) {
208-
snprintf(fqn, sizeof(fqn), "ERROR: %s.%s", parent, alias);
231+
snprintf(fqn, sizeof(fqn), "ERROR: PyInit_%s failed for %s.%s", alias,
232+
parent, alias);
209233
puts(fqn);
210234
PyErr_Print();
211235
PyErr_Clear();
212236
}
213237
else {
214-
PyDict_SetItemString(modules, fqn, mod);
215-
PyDict_SetItemString(PyModule_GetDict(mod), "__name__",
216-
PyUnicode_FromString(fqn));
217-
PyModule_AddObjectRef(pmod, alias, mod);
218-
Py_XDECREF(mod);
238+
snprintf(fqn, sizeof(fqn), "%s.%s", parent, alias);
239+
PyObject *modules = PyImport_GetModuleDict();
240+
241+
PyObject *pmod = PyDict_GetItemString(modules, parent);
242+
if (!pmod) {
243+
snprintf(fqn, sizeof(fqn), "ERROR: Parent %s not found for %s.%s",
244+
parent, parent, alias);
245+
puts(fqn);
246+
}
247+
else {
248+
PyDict_SetItemString(modules, fqn, mod);
249+
PyDict_SetItemString(PyModule_GetDict(mod), "__name__",
250+
PyUnicode_FromString(fqn));
251+
PyModule_AddObjectRef(pmod, alias, mod);
252+
Py_XDECREF(mod);
253+
}
219254
}
220255
}
221256

@@ -225,6 +260,7 @@ load_submodule_mphase(const char *parent, PyObject *mdef, PyObject *spec,
225260
{
226261
char fqn[1024];
227262
snprintf(fqn, sizeof(fqn), "%s.%s", parent, alias);
263+
228264
PyObject *modules = PyImport_GetModuleDict();
229265

230266
Py_DECREF(PyObject_GetAttrString(spec, "name"));
@@ -256,6 +292,7 @@ static PyObject *
256292
mod_pygame_import_cython(PyObject *self, PyObject *spec)
257293
{
258294
load_submodule_mphase("pygame._sdl2", PyInit_sdl2(), spec, "sdl2");
295+
259296
load_submodule_mphase("pygame._sdl2", PyInit_mixer(), spec, "mixer");
260297
#if defined(CONTROLLER_NOPYX)
261298
load_submodule("pygame._sdl2", PyInit_controller(), "controller");
@@ -288,47 +325,72 @@ PyInit_pygame_static()
288325
SDL_SetHint("SDL_EMSCRIPTEN_KEYBOARD_ELEMENT", "1");
289326

290327
load_submodule("pygame", PyInit_base(), "base");
328+
329+
//
291330
load_submodule("pygame", PyInit_constants(), "constants");
292-
load_submodule("pygame", PyInit_surflock(), "surflock");
293-
load_submodule("pygame", PyInit_rwobject(), "rwobject");
331+
//
294332
load_submodule("pygame", PyInit_pg_math(), "math");
295-
load_submodule("pygame", PyInit_display(), "display");
296-
load_submodule("pygame", PyInit_surface(), "surface");
297-
load_submodule("pygame", PyInit_system(), "system");
298-
load_submodule("pygame", PyInit_key(), "key");
299333

334+
// base, pygame.colordict
335+
load_submodule("pygame", PyInit_color(), "color");
336+
337+
// base
300338
load_submodule("pygame", PyInit_rect(), "rect");
339+
340+
// base, rect
301341
load_submodule("pygame", PyInit_geometry(), "geometry");
302-
load_submodule("pygame", PyInit_gfxdraw(), "gfxdraw");
303-
load_submodule("pygame", PyInit_pg_time(), "time");
304-
load_submodule("pygame", PyInit__freetype(), "_freetype");
305342

306-
load_submodule("pygame", PyInit_imageext(), "imageext");
343+
load_submodule("pygame", PyInit_bufferproxy(), "bufferproxy");
344+
load_submodule("pygame", PyInit_surflock(), "surflock");
345+
346+
// base, color, rect, bufferproxy, surflock
347+
load_submodule("pygame", PyInit_surface(), "surface");
348+
349+
load_submodule("pygame", PyInit_rwobject(), "rwobject");
307350

351+
// base, color, rect, bufferproxy, surflock, surface, rwobject
352+
load_submodule("pygame", PyInit_imageext(), "imageext");
353+
// base, color, rect, bufferproxy, surflock, surface, rwobject
308354
load_submodule("pygame", PyInit_image(), "image");
355+
356+
load_submodule("pygame", PyInit__freetype(), "_freetype");
309357
load_submodule("pygame", PyInit_font(), "font");
310358
load_submodule("pygame", PyInit_pixelcopy(), "pixelcopy");
311359
load_submodule("pygame", PyInit_newbuffer(), "newbuffer");
312360

313-
load_submodule("pygame", PyInit_color(), "color");
314-
load_submodule("pygame", PyInit_bufferproxy(), "bufferproxy");
361+
// base
362+
load_submodule("pygame", PyInit_joystick(), "joystick");
363+
// base, joystick
364+
load_submodule("pygame", PyInit_event(), "event");
365+
366+
// base, rect, event
367+
load_submodule("pygame", PyInit_key(), "key");
368+
// base, event
369+
load_submodule("pygame", PyInit_pg_time(), "time");
315370

316371
load_submodule("pygame", PyInit_transform(), "transform");
317372
load_submodule("pygame", PyInit_draw(), "draw");
318373

319374
load_submodule("pygame", PyInit_mask(), "mask");
320375
load_submodule("pygame", PyInit_mouse(), "mouse");
321-
load_submodule("pygame", PyInit_event(), "event");
322-
load_submodule("pygame", PyInit_joystick(), "joystick");
323376

324377
load_submodule("pygame", PyInit_pg_mixer(), "mixer");
325378
load_submodule("pygame.mixer", PyInit_mixer_music(), "music");
326379

380+
// base, color, rect, bufferproxy, surflock, surface
327381
load_submodule("pygame", PyInit_window(), "window");
382+
383+
// base, color, rect, surflock, surface, window
384+
load_submodule("pygame", PyInit_display(), "display");
328385
load_submodule("pygame", PyInit__render(), "_render");
329386

330387
load_submodule("pygame", PyInit_pixelarray(), "pixelarray");
331388

389+
// base, color, rect, bufferproxy, surflock, surface
390+
load_submodule("pygame", PyInit_gfxdraw(), "gfxdraw");
391+
392+
load_submodule("pygame", PyInit_system(), "system");
393+
332394
return PyModule_Create(&mod_pygame_static);
333395
}
334396

src_c/surface.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4630,11 +4630,11 @@ exec_surface(PyObject *module)
46304630

46314631
PyObject *apiobj;
46324632
static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS];
4633-
4633+
#ifndef BUILD_STATIC
46344634
if (pg_warn_simd_at_runtime_but_uncompiled() < 0) {
46354635
return -1;
46364636
}
4637-
4637+
#endif
46384638
if (PyModule_AddObjectRef(module, "SurfaceType",
46394639
(PyObject *)&pgSurface_Type)) {
46404640
return -1;
@@ -4664,6 +4664,7 @@ exec_surface(PyObject *module)
46644664

46654665
MODINIT_DEFINE(surface)
46664666
{
4667+
#ifndef BUILD_STATIC
46674668
static PyModuleDef_Slot surf_slots[] = {
46684669
{Py_mod_exec, &exec_surface},
46694670
#if PY_VERSION_HEX >= 0x030c0000
@@ -4675,16 +4676,32 @@ MODINIT_DEFINE(surface)
46754676
{Py_mod_gil, Py_MOD_GIL_USED}, // TODO: support this later
46764677
#endif
46774678
{0, NULL}};
4678-
4679+
#endif
46794680
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
46804681
"surface",
46814682
DOC_SURFACE,
46824683
0,
46834684
_surface_methods,
4685+
#ifndef BUILD_STATIC
46844686
surf_slots,
4687+
#else
4688+
NULL,
4689+
#endif
46854690
NULL,
46864691
NULL,
46874692
NULL};
4688-
4693+
#ifndef BUILD_STATIC
46894694
return PyModuleDef_Init(&_module);
4695+
#else
4696+
// in static mode with want surface module to be ready before python types
4697+
// are evaluated eg pygame.surface.Surface in sprite.py
4698+
PyObject *module = PyModule_Create(&_module);
4699+
if (module) {
4700+
if (exec_surface(module) != 0) {
4701+
Py_DECREF(module);
4702+
return NULL;
4703+
}
4704+
}
4705+
return module;
4706+
#endif
46904707
}

src_py/__init__.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,15 @@ def warn(self):
114114
if pygame_static:
115115
pygame = sys.modules[__name__]
116116

117-
pygame.Color = pygame.color.Color
117+
# pygame.Color = pygame.color.Color
118118

119-
Vector2 = pygame.math.Vector2
120-
Vector3 = pygame.math.Vector3
119+
# Vector2 = pygame.math.Vector2
120+
# Vector3 = pygame.math.Vector3
121+
# Rect = pygame.rect.Rect
122+
# BufferProxy = pygame.bufferproxy.BufferProxy
121123

122-
Rect = pygame.rect.Rect
123-
124-
BufferProxy = pygame.bufferproxy.BufferProxy
124+
# for pygame.surface.Surface type in sprite.py ?
125+
# surface = sys.modules["pygame.surface"]
125126

126127
# cython modules use multiphase initialisation when not in builtin Inittab.
127128

@@ -144,20 +145,29 @@ def warn(self):
144145
from pygame.base import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]
145146
from pygame.constants import * # now has __all__ pylint: disable=wildcard-import; lgtm[py/polluting-import]
146147
from pygame.version import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]
147-
from pygame.rect import Rect, FRect
148148
from pygame.rwobject import encode_string, encode_file_path
149-
import pygame.surflock
149+
150+
151+
import pygame.rect
152+
153+
Rect = pygame.rect.Rect
154+
FRect = pygame.rect.FRect
155+
156+
150157
import pygame.color
151158

152159
Color = pygame.color.Color
160+
153161
import pygame.bufferproxy
154162

155163
BufferProxy = pygame.bufferproxy.BufferProxy
164+
156165
import pygame.math
157166

158167
Vector2 = pygame.math.Vector2
159168
Vector3 = pygame.math.Vector3
160169

170+
161171
from pygame.base import __version__
162172

163173
# next, the "standard" modules
@@ -209,19 +219,14 @@ def Cursor(*args): # pylint: disable=unused-argument
209219
_attribute_undefined("pygame.Cursor")
210220

211221

212-
try:
213-
import pygame.sprite
214-
except (ImportError, OSError):
215-
sprite = MissingModule("sprite", urgent=1)
216-
217-
218222
try:
219223
import pygame.pixelcopy
220224
except (ImportError, OSError):
221225
pixelcopy = MissingModule("pixelcopy", urgent=1)
222226

223227

224228
try:
229+
import pygame.surflock
225230
from pygame.surface import Surface, SurfaceType
226231
except (ImportError, OSError):
227232

@@ -230,6 +235,13 @@ def Surface(size, flags, depth, masks): # pylint: disable=unused-argument
230235

231236
SurfaceType = Surface
232237

238+
# sprite.py is using pygame.surface.Surface type
239+
try:
240+
import pygame.sprite
241+
except (ImportError, OSError):
242+
sprite = MissingModule("sprite", urgent=1)
243+
244+
233245
try:
234246
import pygame.mask
235247
from pygame.mask import Mask

0 commit comments

Comments
 (0)