Skip to content

Commit 7e8b238

Browse files
authored
Merge pull request #3677 from Starbuck5/minor-simplifications
Small simplification of type initializations
2 parents a0c6f0a + 9146067 commit 7e8b238

File tree

3 files changed

+9
-24
lines changed

3 files changed

+9
-24
lines changed

src_c/_camera.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,24 +1979,13 @@ MODINIT_DEFINE(_camera)
19791979
return NULL;
19801980
}
19811981

1982-
/* type preparation */
1983-
pgCamera_Type.tp_new = PyType_GenericNew;
1984-
if (PyType_Ready(&pgCamera_Type) < 0) {
1985-
return NULL;
1986-
}
1987-
19881982
/* create the module */
19891983
module = PyModule_Create(&_module);
19901984
if (!module) {
19911985
return NULL;
19921986
}
19931987

1994-
if (PyModule_AddObjectRef(module, "CameraType",
1995-
(PyObject *)&pgCamera_Type)) {
1996-
Py_DECREF(module);
1997-
return NULL;
1998-
}
1999-
if (PyModule_AddObjectRef(module, "Camera", (PyObject *)&pgCamera_Type)) {
1988+
if (PyModule_AddType(module, &pgCamera_Type)) {
20001989
Py_DECREF(module);
20011990
return NULL;
20021991
}

src_c/font.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,7 @@ static PyTypeObject PyFont_Type = {
13931393
.tp_methods = font_methods,
13941394
.tp_getset = font_getsets,
13951395
.tp_init = (initproc)font_init,
1396+
.tp_new = PyType_GenericNew,
13961397
};
13971398

13981399
/*font module methods*/
@@ -1499,7 +1500,6 @@ MODINIT_DEFINE(font)
14991500
if (PyType_Ready(&PyFont_Type) < 0) {
15001501
return NULL;
15011502
}
1502-
PyFont_Type.tp_new = PyType_GenericNew;
15031503

15041504
module = PyModule_Create(&_module);
15051505
if (module == NULL) {

src_c/key.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ key_get_repeat(PyObject *self, PyObject *_null)
6868
* https://github.com/pygame-community/pygame-ce/issues/519
6969
* so that they work with SDL_GetKeyboardState().
7070
*/
71-
#define _PG_SCANCODEWRAPPER_TYPE_NAME "ScancodeWrapper"
72-
#define _PG_SCANCODEWRAPPER_TYPE_FULLNAME \
73-
"pygame.key." _PG_SCANCODEWRAPPER_TYPE_NAME
71+
#define _PG_SCANCODEWRAPPER_TYPE_FULLNAME "pygame.key.ScancodeWrapper"
7472

7573
typedef struct {
7674
PyTupleObject tuple;
@@ -155,7 +153,7 @@ pg_scancodewrapper_repr(pgScancodeWrapper *self)
155153
}
156154

157155
static PyTypeObject pgScancodeWrapper_Type = {
158-
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.key.ScancodeWrapper",
156+
PyVarObject_HEAD_INIT(NULL, 0).tp_name = _PG_SCANCODEWRAPPER_TYPE_FULLNAME,
159157
.tp_repr = (reprfunc)pg_scancodewrapper_repr,
160158
.tp_as_mapping = &pg_scancodewrapper_mapping,
161159
.tp_iter = (getiterfunc)pg_iter_raise,
@@ -655,20 +653,18 @@ MODINIT_DEFINE(key)
655653
if (PyErr_Occurred()) {
656654
return NULL;
657655
}
658-
/* type preparation */
659-
pgScancodeWrapper_Type.tp_base = &PyTuple_Type;
660-
if (PyType_Ready(&pgScancodeWrapper_Type) < 0) {
661-
return NULL;
662-
}
663656

664657
/* create the module */
665658
module = PyModule_Create(&_module);
666659
if (module == NULL) {
667660
return NULL;
668661
}
669662

670-
if (PyModule_AddObjectRef(module, _PG_SCANCODEWRAPPER_TYPE_NAME,
671-
(PyObject *)&pgScancodeWrapper_Type)) {
663+
// has to be set in init function rather than statically per note on
664+
// https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_base
665+
pgScancodeWrapper_Type.tp_base = &PyTuple_Type;
666+
667+
if (PyModule_AddType(module, &pgScancodeWrapper_Type)) {
672668
Py_DECREF(module);
673669
return NULL;
674670
}

0 commit comments

Comments
 (0)