Skip to content

Replace AddObject() with AddObjectRef() #3406

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

Merged
merged 3 commits into from
May 18, 2025
Merged
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
9 changes: 3 additions & 6 deletions src_c/_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -1988,15 +1988,12 @@ MODINIT_DEFINE(_camera)
return NULL;
}

Py_INCREF(&pgCamera_Type);
if (PyModule_AddObject(module, "CameraType", (PyObject *)&pgCamera_Type)) {
Py_DECREF(&pgCamera_Type);
if (PyModule_AddObjectRef(module, "CameraType",
(PyObject *)&pgCamera_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgCamera_Type);
if (PyModule_AddObject(module, "Camera", (PyObject *)&pgCamera_Type)) {
Py_DECREF(&pgCamera_Type);
if (PyModule_AddObjectRef(module, "Camera", (PyObject *)&pgCamera_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
5 changes: 2 additions & 3 deletions src_c/_freetype.c
Original file line number Diff line number Diff line change
Expand Up @@ -2383,9 +2383,8 @@ MODINIT_DEFINE(_freetype)
FREETYPE_MOD_STATE(module)->cache_size = 0;
FREETYPE_MOD_STATE(module)->resolution = PGFT_DEFAULT_RESOLUTION;

Py_INCREF(&pgFont_Type);
if (PyModule_AddObject(module, FONT_TYPE_NAME, (PyObject *)&pgFont_Type)) {
Py_DECREF(&pgFont_Type);
if (PyModule_AddObjectRef(module, FONT_TYPE_NAME,
(PyObject *)&pgFont_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
4 changes: 1 addition & 3 deletions src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -2404,9 +2404,7 @@ MODINIT_DEFINE(base)
pgExc_BufferError =
PyErr_NewException("pygame.BufferError", PyExc_BufferError, NULL);
/* Because we need a reference to BufferError in the base module */
Py_XINCREF(pgExc_BufferError);
if (PyModule_AddObject(module, "BufferError", pgExc_BufferError)) {
Py_XDECREF(pgExc_BufferError);
if (PyModule_AddObjectRef(module, "BufferError", pgExc_BufferError)) {
goto error;
}

Expand Down
6 changes: 2 additions & 4 deletions src_c/bufferproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,8 @@ MODINIT_DEFINE(bufferproxy)
return NULL;
}

Py_INCREF(&pgBufferProxy_Type);
if (PyModule_AddObject(module, "BufferProxy",
(PyObject *)&pgBufferProxy_Type)) {
Py_DECREF(&pgBufferProxy_Type);
if (PyModule_AddObjectRef(module, "BufferProxy",
(PyObject *)&pgBufferProxy_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
9 changes: 2 additions & 7 deletions src_c/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -2485,15 +2485,10 @@ MODINIT_DEFINE(color)
goto error;
}

Py_INCREF(&pgColor_Type);
if (PyModule_AddObject(module, "Color", (PyObject *)&pgColor_Type)) {
Py_DECREF(&pgColor_Type);
if (PyModule_AddObjectRef(module, "Color", (PyObject *)&pgColor_Type)) {
goto error;
}
Py_INCREF(_COLORDICT);
if (PyModule_AddObject(module, "THECOLORS", _COLORDICT)) {
/* Yes, _COLORDICT is decref'd twice here and we want that */
Py_DECREF(_COLORDICT);
if (PyModule_AddObjectRef(module, "THECOLORS", _COLORDICT)) {
goto error;
}

Expand Down
9 changes: 3 additions & 6 deletions src_c/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -2578,15 +2578,12 @@ MODINIT_DEFINE(event)
return NULL;
}

Py_INCREF(&pgEvent_Type);
if (PyModule_AddObject(module, "EventType", (PyObject *)&pgEvent_Type)) {
Py_DECREF(&pgEvent_Type);
if (PyModule_AddObjectRef(module, "EventType",
(PyObject *)&pgEvent_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgEvent_Type);
if (PyModule_AddObject(module, "Event", (PyObject *)&pgEvent_Type)) {
Py_DECREF(&pgEvent_Type);
if (PyModule_AddObjectRef(module, "Event", (PyObject *)&pgEvent_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
8 changes: 2 additions & 6 deletions src_c/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -1422,16 +1422,12 @@ MODINIT_DEFINE(font)
return NULL;
}

Py_INCREF(&PyFont_Type);
if (PyModule_AddObject(module, "FontType", (PyObject *)&PyFont_Type)) {
Py_DECREF(&PyFont_Type);
if (PyModule_AddObjectRef(module, "FontType", (PyObject *)&PyFont_Type)) {
Py_DECREF(module);
return NULL;
}

Py_INCREF(&PyFont_Type);
if (PyModule_AddObject(module, "Font", (PyObject *)&PyFont_Type)) {
Py_DECREF(&PyFont_Type);
if (PyModule_AddObjectRef(module, "Font", (PyObject *)&PyFont_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
8 changes: 2 additions & 6 deletions src_c/geometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,12 @@ MODINIT_DEFINE(geometry)
return NULL;
}

Py_INCREF(&pgCircle_Type);
if (PyModule_AddObject(module, "Circle", (PyObject *)&pgCircle_Type)) {
Py_DECREF(&pgCircle_Type);
if (PyModule_AddObjectRef(module, "Circle", (PyObject *)&pgCircle_Type)) {
Py_DECREF(module);
return NULL;
}

Py_INCREF(&pgLine_Type);
if (PyModule_AddObject(module, "Line", (PyObject *)&pgLine_Type)) {
Py_DECREF(&pgLine_Type);
if (PyModule_AddObjectRef(module, "Line", (PyObject *)&pgLine_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
6 changes: 2 additions & 4 deletions src_c/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,8 @@ MODINIT_DEFINE(joystick)
return NULL;
}

Py_INCREF(&pgJoystick_Type);
if (PyModule_AddObject(module, "JoystickType",
(PyObject *)&pgJoystick_Type)) {
Py_DECREF(&pgJoystick_Type);
if (PyModule_AddObjectRef(module, "JoystickType",
(PyObject *)&pgJoystick_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
6 changes: 2 additions & 4 deletions src_c/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,8 @@ MODINIT_DEFINE(key)
return NULL;
}

Py_INCREF(&pgScancodeWrapper_Type);
if (PyModule_AddObject(module, _PG_SCANCODEWRAPPER_TYPE_NAME,
(PyObject *)&pgScancodeWrapper_Type)) {
Py_DECREF(&pgScancodeWrapper_Type);
if (PyModule_AddObjectRef(module, _PG_SCANCODEWRAPPER_TYPE_NAME,
(PyObject *)&pgScancodeWrapper_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
8 changes: 2 additions & 6 deletions src_c/mask.c
Original file line number Diff line number Diff line change
Expand Up @@ -2649,16 +2649,12 @@ MODINIT_DEFINE(mask)
if (module == NULL) {
return NULL;
}
Py_INCREF(&pgMask_Type);
if (PyModule_AddObject(module, "MaskType", (PyObject *)&pgMask_Type)) {
Py_DECREF(&pgMask_Type);
if (PyModule_AddObjectRef(module, "MaskType", (PyObject *)&pgMask_Type)) {
Py_DECREF(module);
return NULL;
}

Py_INCREF(&pgMask_Type);
if (PyModule_AddObject(module, "Mask", (PyObject *)&pgMask_Type)) {
Py_DECREF(&pgMask_Type);
if (PyModule_AddObjectRef(module, "Mask", (PyObject *)&pgMask_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
32 changes: 8 additions & 24 deletions src_c/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -4573,31 +4573,15 @@ MODINIT_DEFINE(math)
}

/* add extension types to module */
Py_INCREF(&pgVector2_Type);
Py_INCREF(&pgVector3_Type);
Py_INCREF(&pgVectorIter_Type);
Py_INCREF(&pgVectorElementwiseProxy_Type);
if ((PyModule_AddObject(module, "Vector2", (PyObject *)&pgVector2_Type) !=
if ((PyModule_AddObjectRef(module, "Vector2",
(PyObject *)&pgVector2_Type) < 0) ||
(PyModule_AddObjectRef(module, "Vector3",
(PyObject *)&pgVector3_Type) < 0) ||
(PyModule_AddObjectRef(module, "VectorElementwiseProxy",
(PyObject *)&pgVectorElementwiseProxy_Type) <
0) ||
(PyModule_AddObject(module, "Vector3", (PyObject *)&pgVector3_Type) !=
0) ||
(PyModule_AddObject(module, "VectorElementwiseProxy",
(PyObject *)&pgVectorElementwiseProxy_Type) !=
0) ||
(PyModule_AddObject(module, "VectorIterator",
(PyObject *)&pgVectorIter_Type) != 0)) {
if (!PyObject_HasAttrString(module, "Vector2")) {
Py_DECREF(&pgVector2_Type);
}
if (!PyObject_HasAttrString(module, "Vector3")) {
Py_DECREF(&pgVector3_Type);
}
if (!PyObject_HasAttrString(module, "VectorElementwiseProxy")) {
Py_DECREF(&pgVectorElementwiseProxy_Type);
}
if (!PyObject_HasAttrString(module, "VectorIterator")) {
Py_DECREF(&pgVectorIter_Type);
}
(PyModule_AddObjectRef(module, "VectorIterator",
(PyObject *)&pgVectorIter_Type) < 0)) {
Py_DECREF(module);
return NULL;
}
Expand Down
20 changes: 7 additions & 13 deletions src_c/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2101,28 +2101,22 @@ MODINIT_DEFINE(mixer)
return NULL;
}

Py_INCREF(&pgSound_Type);
if (PyModule_AddObject(module, "Sound", (PyObject *)&pgSound_Type)) {
Py_DECREF(&pgSound_Type);
if (PyModule_AddObjectRef(module, "Sound", (PyObject *)&pgSound_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgSound_Type);
if (PyModule_AddObject(module, "SoundType", (PyObject *)&pgSound_Type)) {
Py_DECREF(&pgSound_Type);
if (PyModule_AddObjectRef(module, "SoundType",
(PyObject *)&pgSound_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgChannel_Type);
if (PyModule_AddObject(module, "ChannelType",
(PyObject *)&pgChannel_Type)) {
Py_DECREF(&pgChannel_Type);
if (PyModule_AddObjectRef(module, "ChannelType",
(PyObject *)&pgChannel_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgChannel_Type);
if (PyModule_AddObject(module, "Channel", (PyObject *)&pgChannel_Type)) {
Py_DECREF(&pgChannel_Type);
if (PyModule_AddObjectRef(module, "Channel",
(PyObject *)&pgChannel_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
11 changes: 4 additions & 7 deletions src_c/newbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,16 +869,13 @@ MODINIT_DEFINE(newbuffer)
return NULL;
}

Py_INCREF(&BufferMixin_Type);
if (PyModule_AddObject(module, "BufferMixin",
(PyObject *)&BufferMixin_Type)) {
Py_DECREF(&BufferMixin_Type);
if (PyModule_AddObjectRef(module, "BufferMixin",
(PyObject *)&BufferMixin_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&Py_buffer_Type);
if (PyModule_AddObject(module, "Py_buffer", (PyObject *)&Py_buffer_Type)) {
Py_DECREF(&Py_buffer_Type);
if (PyModule_AddObjectRef(module, "Py_buffer",
(PyObject *)&Py_buffer_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
6 changes: 2 additions & 4 deletions src_c/pixelarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -1936,10 +1936,8 @@ MODINIT_DEFINE(pixelarray)
if (!module) {
return NULL;
}
Py_INCREF(&pgPixelArray_Type);
if (PyModule_AddObject(module, "PixelArray",
(PyObject *)&pgPixelArray_Type)) {
Py_DECREF((PyObject *)&pgPixelArray_Type);
if (PyModule_AddObjectRef(module, "PixelArray",
(PyObject *)&pgPixelArray_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
17 changes: 5 additions & 12 deletions src_c/rect.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,27 +801,20 @@ MODINIT_DEFINE(rect)
return NULL;
}

Py_INCREF(&pgRect_Type);
if (PyModule_AddObject(module, "RectType", (PyObject *)&pgRect_Type)) {
Py_DECREF(&pgRect_Type);
if (PyModule_AddObjectRef(module, "RectType", (PyObject *)&pgRect_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgRect_Type);
if (PyModule_AddObject(module, "Rect", (PyObject *)&pgRect_Type)) {
Py_DECREF(&pgRect_Type);
if (PyModule_AddObjectRef(module, "Rect", (PyObject *)&pgRect_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgFRect_Type);
if (PyModule_AddObject(module, "FRectType", (PyObject *)&pgFRect_Type)) {
Py_DECREF(&pgFRect_Type);
if (PyModule_AddObjectRef(module, "FRectType",
(PyObject *)&pgFRect_Type)) {
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgFRect_Type);
if (PyModule_AddObject(module, "FRect", (PyObject *)&pgFRect_Type)) {
Py_DECREF(&pgFRect_Type);
if (PyModule_AddObjectRef(module, "FRect", (PyObject *)&pgFRect_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
14 changes: 5 additions & 9 deletions src_c/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,19 @@ MODINIT_DEFINE(_render)
return NULL;
}

Py_INCREF(&pgRenderer_Type);
if (PyModule_AddObject(module, "Renderer", (PyObject *)&pgRenderer_Type)) {
Py_DECREF(&pgRenderer_Type);
if (PyModule_AddObjectRef(module, "Renderer",
(PyObject *)&pgRenderer_Type)) {
Py_DECREF(module);
return NULL;
}

Py_INCREF(&pgTexture_Type);
if (PyModule_AddObject(module, "Texture", (PyObject *)&pgTexture_Type)) {
Py_DECREF(&pgTexture_Type);
if (PyModule_AddObjectRef(module, "Texture",
(PyObject *)&pgTexture_Type)) {
Py_DECREF(module);
return NULL;
}

Py_INCREF(&pgImage_Type);
if (PyModule_AddObject(module, "Image", (PyObject *)&pgImage_Type)) {
Py_DECREF(&pgImage_Type);
if (PyModule_AddObjectRef(module, "Image", (PyObject *)&pgImage_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
15 changes: 5 additions & 10 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -4327,17 +4327,14 @@ MODINIT_DEFINE(surface)
Py_DECREF(module);
return NULL;
}
Py_INCREF(&pgSurface_Type);
if (PyModule_AddObject(module, "SurfaceType",
(PyObject *)&pgSurface_Type)) {
Py_DECREF(&pgSurface_Type);
if (PyModule_AddObjectRef(module, "SurfaceType",
(PyObject *)&pgSurface_Type)) {
Py_DECREF(module);
return NULL;
}

Py_INCREF(&pgSurface_Type);
if (PyModule_AddObject(module, "Surface", (PyObject *)&pgSurface_Type)) {
Py_DECREF(&pgSurface_Type);
if (PyModule_AddObjectRef(module, "Surface",
(PyObject *)&pgSurface_Type)) {
Py_DECREF(module);
return NULL;
}
Expand All @@ -4353,9 +4350,7 @@ MODINIT_DEFINE(surface)
Py_DECREF(module);
return NULL;
}
Py_XINCREF(pgSurface_Type.tp_dict);
if (PyModule_AddObject(module, "_dict", pgSurface_Type.tp_dict)) {
Py_XDECREF(pgSurface_Type.tp_dict);
if (PyModule_AddObjectRef(module, "_dict", pgSurface_Type.tp_dict)) {
Py_DECREF(module);
return NULL;
}
Expand Down
4 changes: 1 addition & 3 deletions src_c/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,7 @@ MODINIT_DEFINE(time)
return NULL;
}

Py_INCREF(&PyClock_Type);
if (PyModule_AddObject(module, "Clock", (PyObject *)&PyClock_Type)) {
Py_DECREF(&PyClock_Type);
if (PyModule_AddObjectRef(module, "Clock", (PyObject *)&PyClock_Type)) {
Py_DECREF(module);
return NULL;
}
Expand Down
Loading
Loading