8000 gh-86493: Use PyModule_Add() instead of PyModule_AddObjectRef() (GH-1… · python/cpython@a293fa5 · GitHub
[go: up one dir, main page]

Skip to content

Commit a293fa5

Browse files
gh-86493: Use PyModule_Add() instead of PyModule_AddObjectRef() (GH-106860)
1 parent 40f3f11 commit a293fa5

File tree

14 files changed

+26
-116
lines changed

14 files changed

+26
-116
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5740,15 +5740,9 @@ _ctypes_add_objects(PyObject *mod)
57405740
{
57415741
#define MOD_ADD(name, expr) \
57425742
do { \
5743-
PyObject *obj = (expr); \
5744-
if (obj == NULL) { \
5743+
if (PyModule_Add(mod, name, (expr)) < 0) { \
57455744
return -1; \
57465745
} \
5747-
if (PyModule_AddObjectRef(mod, name, obj) < 0) { \
5748-
Py_DECREF(obj); \
5749-
return -1; \
5750-
} \
5751-
Py_DECREF(obj); \
57525746
} while (0)
57535747

57545748
MOD_ADD("_pointer_type_cache", Py_NewRef(_ctypes_ptrtype_cache));

Modules/_hashopenssl.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,6 @@ hashlib_init_constructors(PyObject *module)
21892189
*/
21902190
PyModuleDef *mdef;
21912191
PyMethodDef *fdef;
2192-
PyObject *proxy;
21932192
PyObject *func, *name_obj;
21942193
_hashlibstate *state = get_hashlib_state(module);
21952194

@@ -2224,17 +2223,8 @@ hashlib_init_constructors(PyObject *module)
22242223
}
22252224
}
22262225

2227-
proxy = PyDictProxy_New(state->constructs);
2228-
if (proxy == NULL) {
2229-
return -1;
2230-
}
2231-
2232-
int rc = PyModule_AddObjectRef(module, "_constructors", proxy);
2233-
Py_DECREF(proxy);
2234-
if (rc < 0) {
2235-
return -1;
2236-
}
2237-
return 0;
2226+
return PyModule_Add(module, "_constructors",
2227+
PyDictProxy_New(state->constructs));
22382228
}
22392229

22402230
static int

Modules/_json.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,22 +1756,12 @@ static int
17561756
_json_exec(PyObject *module)
17571757
{
17581758
PyObject *PyScannerType = PyType_FromSpec(&PyScannerType_spec);
1759-
if (PyScannerType == NULL) {
1760-
return -1;
1761-
}
1762-
int rc = PyModule_AddObjectRef(module, "make_scanner", PyScannerType);
1763-
Py_DECREF(PyScannerType);
1764-
if (rc < 0) {
1759+
if (PyModule_Add(module, "make_scanner", PyScannerType) < 0) {
17651760
return -1;
17661761
}
17671762

17681763
PyObject *PyEncoderType = PyType_FromSpec(&PyEncoderType_spec);
1769-
if (PyEncoderType == NULL) {
1770-
return -1;
1771-
}
1772-
rc = PyModule_AddObjectRef(module, "make_encoder", PyEncoderType);
1773-
Py_DECREF(PyEncoderType);
1774-
if (rc < 0) {
1764+
if (PyModule_Add(module, "make_encoder", PyEncoderType) < 0) {
17751765
return -1;
17761766
}
17771767

Modules/_sre/sre.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3195,12 +3195,7 @@ do { \
31953195

31963196
#define ADD_ULONG_CONSTANT(module, name, value) \
31973197
do { \
3198-
PyObject *o = PyLong_FromUnsignedLong(value); \
3199-
if (!o) \
3200-
goto error; \
3201-
int res = PyModule_AddObjectRef(module, name, o); \
3202-
Py_DECREF(o); \
3203-
if (res < 0) { \
3198+
if (PyModule_Add(module, name, PyLong_FromUnsignedLong(value)) < 0) { \
32043199
goto error; \
32053200
} \
32063201
} while (0)

Modules/_ssl.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5773,13 +5773,7 @@ static int
57735773
sslmodule_add_option(PyObject *m, const char *name, uint64_t value)
57745774
{
57755775
Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(value));
5776-
PyObject *obj = PyLong_FromUnsignedLongLong(value);
5777-
if (obj == NULL) {
5778-
return -1;
5779-
}
5780-
int res = PyModule_AddObjectRef(m, name, obj);
5781-
Py_DECREF(obj);
5782-
return res;
5776+
return PyModule_Add(m, name, PyLong_FromUnsignedLongLong(value));
57835777
}
57845778

57855779

Modules/_testcapi/mem.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,11 @@ _PyTestCapi_Init_Mem(PyObject *mod)
692692

693693
PyObject *v;
694694
#ifdef WITH_PYMALLOC
695-
v = Py_NewRef(Py_True);
695+
v = Py_True;
696696
#else
697-
v = Py_NewRef(Py_False);
697+
v = Py_False;
698698
#endif
699-
int rc = PyModule_AddObjectRef(mod, "WITH_PYMALLOC", v);
700-
Py_DECREF(v);
701-
if (rc < 0) {
699+
if (PyModule_AddObjectRef(mod, "WITH_PYMALLOC", v) < 0) {
702700
return -1;
703701
}
704702

Modules/_testcapi/watchers.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,7 @@ static PyFunction_WatchCallback func_watcher_callbacks[NUM_TEST_FUNC_WATCHERS] =
516516
static int
517517
add_func_event(PyObject *module, const char *name, PyFunction_WatchEvent event)
518518
{
519-
PyObject *value = PyLong_FromLong(event);
520-
if (value == NULL) {
521-
return -1;
522-
}
523-
int ok = PyModule_AddObjectRef(module, name, value);
524-
Py_DECREF(value);
525-
return ok;
519+
return PyModule_Add(module, name, PyLong_FromLong(event));
526520
}
527521

528522
static PyObject *

Modules/_testsinglephase.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,7 @@ init_module(PyObject *module, module_state *state)
137137
}
138138

139139
double d = _PyTime_AsSecondsDouble(state->initialized);
140-
PyObject *initialized = PyFloat_FromDouble(d);
141-
if (initialized == NULL) {
142-
return -1;
143-
}
144-
int rc = PyModule_AddObjectRef(module, "_module_initialized", initialized);
145-
Py_DECREF(initialized);
146-
if (rc < 0) {
140+
if (PyModule_Add(module, "_module_initialized", PyFloat_FromDouble(d)) < 0) {
147141
return -1;
148142
}
149143

Modules/pyexpat.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,16 +1812,13 @@ add_errors_module(PyObject *mod)
18121812
goto error;
18131813
}
18141814

1815-
int rc = PyModule_AddObjectRef(errors_module, "codes", codes_dict);
1816-
Py_CLEAR(codes_dict);
1817-
if (rc < 0) {
1818-
goto error;
1815+
if (PyModule_Add(errors_module, "codes", codes_dict) < 0) {
1816+
Py_DECREF(rev_codes_dict);
1817+
return -1;
18191818
}
18201819

1821-
rc = PyModule_AddObjectRef(errors_module, "messages", rev_codes_dict);
1822-
Py_CLEAR(rev_codes_dict);
1823-
if (rc < 0) {
1824-
goto error;
1820+
if (PyModule_Add(errors_module, "messages", rev_codes_dict) < 0) {
1821+
return -1;
18251822
}
18261823

18271824
return 0;

Modules/socketmodule.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7425,9 +7425,7 @@ socket_exec(PyObject *m)
74257425
sock_free_api(capi);
74267426
goto error;
74277427
}
7428-
int rc = PyModule_AddObjectRef(m, PySocket_CAPI_NAME, capsule);
7429-
Py_DECREF(capsule);
7430-
if (rc < 0) {
7428+
if (PyModule_Add(m, PySocket_CAPI_NAME, capsule) < 0) {
74317429
goto error;
74327430
}
74337431

@@ -8818,13 +8816,7 @@ socket_exec(PyObject *m)
88188816
};
88198817
int i;
88208818
for (i = 0; i < Py_ARRAY_LENGTH(codes); ++i) {
8821-
PyObject *tmp = PyLong_FromUnsignedLong(codes[i]);
8822-
if (tmp == NULL) {
8823-
goto error;
8824-
}
8825-
int rc = PyModule_AddObjectRef(m, names[i], tmp);
8826-
Py_DECREF(tmp);
8827-
if (rc < 0) {
8819+
if (PyModule_Add(m, names[i], PyLong_FromUnsignedLong(codes[i])) < 0) {
88288820
goto error;
88298821
}
88308822
}

Modules/unicodedata.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,13 +1496,7 @@ unicodedata_exec(PyObject *module)
14961496
}
14971497

14981498
/* Export C API */
1499-
PyObject *capsule = unicodedata_create_capi();
1500-
if (capsule == NULL) {
1501-
return -1;
1502-
}
1503-
int rc = PyModule_AddObjectRef(module, "_ucnhash_CAPI", capsule);
1504-
Py_DECREF(capsule);
1505-
if (rc < 0) {
1499+
if (PyModule_Add(module, "_ucnhash_CAPI", unicodedata_create_capi()) < 0) {
15061500
return -1;
15071501
}
15081502
return 0;

PC/msvcrtmodule.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -565,15 +565,9 @@ static struct PyMethodDef msvcrt_functions[] = {
565565
};
566566

567567
static int
568-
insertptr(PyObject *mod, char *name, void *value)
568+
insertptr(PyObject *mod, const char *name, void *value)
569569
{
570-
PyObject *v = PyLong_FromVoidPtr(value);
571-
if (v == NULL) {
572-
return -1;
573-
}
574-
int rc = PyModule_AddObjectRef(mod, name, v);
575-
Py_DECREF(v);
576-
return rc;
570+
return PyModule_Add(mod, name, PyLong_FromVoidPtr(value));
577571
}
578572

579573
#define INSERTINT(MOD, NAME, VAL) do { \
@@ -646,12 +640,7 @@ exec_module(PyObject* m)
646640
_VC_CRT_MINOR_VERSION,
647641
_VC_CRT_BUILD_VERSION,
648642
_VC_CRT_RBUILD_VERSION);
649-
if (version == NULL) {
650-
return -1;
651-
}
652-
int st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
653-
Py_DECREF(version);
654-
if (st < 0) {
643+
if (PyModule_Add(m, "CRT_ASSEMBLY_VERSION", version) < 0) {
655644
return -1;
656645
}
657646
#endif

PC/winreg.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,15 +2079,9 @@ static struct PyMethodDef winreg_methods[] = {
20792079
} while (0)
20802080

20812081
static int
2082-
inskey(PyObject *mod, char *name, HKEY key)
2082+
inskey(PyObject *mod, const char *name, HKEY key)
20832083
{
2084-
PyObject *v = PyLong_FromVoidPtr(key);
2085-
if (v == NULL) {
2086-
return -1;
2087-
}
2088-
int rc = PyModule_AddObjectRef(mod, name, v);
2089-
Py_DECREF(v);
2090-
return rc;
2084+
return PyModule_Add(mod, name, PyLong_FromVoidPtr(key));
20912085
}
20922086

20932087
#define ADD_KEY(VAL) do { \

Python/import.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,14 +3844,9 @@ imp_module_exec(PyObject *module)
38443844
{
38453845
const wchar_t *mode = _Py_GetConfig()->check_hash_pycs_mode;
38463846
PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1);
3847-
if (pyc_mode == NULL) {
3847+
if (PyModule_Add(module, "check_hash_based_pycs", pyc_mode) < 0) {
38483848
return -1;
38493849
}
3850-
if (PyModule_AddObjectRef(module, "check_hash_based_pycs", pyc_mode) < 0) {
3851-
Py_DECREF(pyc_mode);
3852-
return -1;
3853-
}
3854-
Py_DECREF(pyc_mode);
38553850

38563851
return 0;
38573852
}

0 commit comments

Comments
 (0)
0