Skip to content

Commit a12844e

Browse files
committed
dedup code block
1 parent d93163d commit a12844e

File tree

2 files changed

+25
-71
lines changed

2 files changed

+25
-71
lines changed

src_c/static.c

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

52-
5352
void
5453
import_pygame_window(void)
5554
{
@@ -75,7 +74,6 @@ import_pygame_freetype(void)
7574
{
7675
}
7776

78-
7977
void
8078
import_pygame_bufferproxy(void)
8179
{
@@ -115,8 +113,6 @@ import_pygame_pixelarray(void)
115113
{
116114
}
117115

118-
119-
120116
PyMODINIT_FUNC
121117
PyInit_base(void);
122118
PyMODINIT_FUNC
@@ -232,7 +228,8 @@ load_submodule(const char *parent, PyObject *mod, const char *alias)
232228
{
233229
char fqn[1024];
234230
if (!mod) {
235-
snprintf(fqn, sizeof(fqn), "ERROR: PyInit_%s failed for %s.%s", alias, parent, alias);
231+
snprintf(fqn, sizeof(fqn), "ERROR: PyInit_%s failed for %s.%s", alias,
232+
parent, alias);
236233
puts(fqn);
237234
PyErr_Print();
238235
PyErr_Clear();
@@ -243,9 +240,11 @@ load_submodule(const char *parent, PyObject *mod, const char *alias)
243240

244241
PyObject *pmod = PyDict_GetItemString(modules, parent);
245242
if (!pmod) {
246-
snprintf(fqn, sizeof(fqn), "ERROR: Parent %s not found for %s.%s", parent, parent, alias);
243+
snprintf(fqn, sizeof(fqn), "ERROR: Parent %s not found for %s.%s",
244+
parent, parent, alias);
247245
puts(fqn);
248-
} else {
246+
}
247+
else {
249248
PyDict_SetItemString(modules, fqn, mod);
250249
PyDict_SetItemString(PyModule_GetDict(mod), "__name__",
251250
PyUnicode_FromString(fqn));
@@ -369,14 +368,12 @@ PyInit_pygame_static()
369368
// base, event
370369
load_submodule("pygame", PyInit_pg_time(), "time");
371370

372-
373371
load_submodule("pygame", PyInit_transform(), "transform");
374372
load_submodule("pygame", PyInit_draw(), "draw");
375373

376374
load_submodule("pygame", PyInit_mask(), "mask");
377375
load_submodule("pygame", PyInit_mouse(), "mouse");
378376

379-
380377
load_submodule("pygame", PyInit_pg_mixer(), "mixer");
381378
load_submodule("pygame.mixer", PyInit_mixer_music(), "music");
382379

src_c/surface.c

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4595,7 +4595,7 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
45954595
}
45964596

45974597
static PyMethodDef _surface_methods[] = {{NULL, NULL, 0, NULL}};
4598-
#if !defined(BUILD_STATIC)
4598+
45994599
int
46004600
exec_surface(PyObject *module)
46014601
{
@@ -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,76 +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,
4685-
NULL,
4686-
NULL,
4687-
NULL};
4688-
4689-
return PyModuleDef_Init(&_module);
4690-
}
46914687
#else
4692-
MODINIT_DEFINE(surface)
4693-
{
4694-
PyObject *module;
4695-
4696-
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
4697-
"surface",
4698-
DOC_SURFACE,
4699-
-1,
4700-
_surface_methods,
47014688
NULL,
4689+
#endif
47024690
NULL,
47034691
NULL,
47044692
NULL};
4705-
4706-
module = PyModule_Create(&_module);
4707-
if (module == NULL) {
4708-
return NULL;
4709-
}
4710-
4711-
_IMPORT_PYGAME_MODULE(surflock);
4712-
if (PyErr_Occurred()) {
4713-
return NULL;
4714-
}
4715-
4716-
/* type preparation */
4717-
if (PyType_Ready(&pgSurface_Type) < 0) {
4718-
return NULL;
4719-
}
4720-
4721-
PyObject *apiobj;
4722-
static void *c_api[PYGAMEAPI_SURFACE_NUMSLOTS];
4723-
4724-
if (PyModule_AddObjectRef(module, "SurfaceType",
4725-
(PyObject *)&pgSurface_Type)) {
4726-
return NULL;
4727-
}
4728-
4729-
if (PyModule_AddObjectRef(module, "Surface",
4730-
(PyObject *)&pgSurface_Type)) {
4731-
return NULL;
4732-
}
4733-
4734-
/* export the c api */
4735-
c_api[0] = &pgSurface_Type;
4736-
c_api[1] = pgSurface_New2;
4737-
c_api[2] = pgSurface_Blit;
4738-
c_api[3] = pgSurface_SetSurface;
4739-
apiobj = encapsulate_api(c_api, "surface");
4740-
if (PyModule_Add(module, PYGAMEAPI_LOCAL_ENTRY, apiobj) < 0) {
4741-
return NULL;
4742-
}
4743-
4744-
if (PyModule_AddObjectRef(module, "_dict", pgSurface_Type.tp_dict)) {
4745-
return NULL;
4693+
#ifndef BUILD_STATIC
4694+
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+
}
47464704
}
4747-
47484705
return module;
4749-
}
47504706
#endif
4707+
}

0 commit comments

Comments
 (0)