Merge pull request #3 from DinoV/pythoncapi · pythoncapi/cpython@d57cc7b · GitHub
Skip to content

Commit d57cc7b

Browse files
authored
Merge pull request #3 from DinoV/pythoncapi
Updates for PyDict_GetItemRefString
2 parents 63527d3 + bbb76e5 commit d57cc7b

10 files changed

Lines changed: 72 additions & 40 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 37 additions & 18 deletions

Modules/_ctypes/callbacks.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ TryAddRef(StgDictObject *dict, CDataObject *obj)
108108
{
109109
IUnknown *punk;
110110

111-
if (NULL == PyDict_GetItemString((PyObject *)dict, "_needs_com_addref_"))
111+
PyObject *add_ref = PyDict_GetItemRefString((PyObject *)dict, "_needs_com_addref_");
112+
if (NULL == add_ref)
112113
return;
113-
114+
Py_DECREF(add_ref);
114115
punk = *(IUnknown **)obj->b_ptr;
115116
if (punk)
116117
punk->lpVtbl->AddRef(punk);

Modules/_decimal/_decimal.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,22 +3125,29 @@ dec_format(PyObject *dec, PyObject *args)
31253125
"optional argument must be a dict");
31263126
goto finish;
31273127
}
3128-
if ((dot = PyDict_GetItemString(override, "decimal_point"))) {
3129-
if ((dot = PyUnicode_AsUTF8String(dot)) == NULL) {
3128+
PyObject *tmp;
3129+
if ((dot = PyDict_GetItemRefString(override, "decimal_point"))) {
3130+
if ((tmp = PyUnicode_AsUTF8String(dot)) == NULL) {
31303131
goto finish;
31313132
}
3133+
Py_DECREF(dot);
3134+
dot = tmp;
31323135
spec.dot = PyBytes_AS_STRING(dot);
31333136
}
3134-
if ((sep = PyDict_GetItemString(override, "thousands_sep"))) {
3135-
if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) {
3137+
if ((sep = PyDict_GetItemRefString(override, "thousands_sep"))) {
3138+
if ((tmp = PyUnicode_AsUTF8String(sep)) == NULL) {
31363139
goto finish;
31373140
}
3141+
Py_DECREF(sep);
3142+
sep = tmp;
31383143
spec.sep = PyBytes_AS_STRING(sep);
31393144
}
3140-
if ((grouping = PyDict_GetItemString(override, "grouping"))) {
3141-
if ((grouping = PyUnicode_AsUTF8String(grouping)) == NULL) {
3145+
if ((grouping = PyDict_GetItemRefString(override, "grouping"))) {
3146+
if ((tmp = PyUnicode_AsUTF8String(grouping)) == NULL) {
31423147
goto finish;
31433148
}
3149+
Py_DECREF(grouping);
3150+
grouping = tmp;
31443151
spec.grouping = PyBytes_AS_STRING(grouping);
31453152
}
31463153
if (mpd_validate_lconv(&spec) < 0) {

Modules/_pickle.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,9 @@ _Pickle_InitState(PickleState *st)
218218
builtins = PyEval_GetBuiltins();
219219
if (builtins == NULL)
220220
goto error;
221-
st->getattr = PyDict_GetItemString(builtins, "getattr");
221+
st->getattr = PyDict_GetItemRefString(builtins, "getattr");
222222
if (st->getattr == NULL)
223223
goto error;
224-
Py_INCREF(st->getattr);
225224

226225
copyreg = PyImport_ImportModule("copyreg");
227226
if (!copyreg)

Modules/_sqlite/connection.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
14581458
goto finally;
14591459
}
14601460

1461-
pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1461+
pyfn_iterdump = PyDict_GetItemRefString(module_dict, "_iterdump");
14621462
if (!pyfn_iterdump) {
14631463
PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
14641464
goto finally;
@@ -1473,6 +1473,7 @@ pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
14731473
retval = PyObject_CallObject(pyfn_iterdump, args);
14741474

14751475
finally:
1476+
Py_XDECREF(pyfn_iterdump);
14761477
Py_XDECREF(args);
14771478
Py_XDECREF(module);
14781479
return retval;

Modules/_sqlite/statement.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
281281

282282
binding_name++; /* skip first char (the colon) */
283283
if (PyDict_CheckExact(parameters)) {
284-
current_param = PyDict_GetItemString(parameters, binding_name);
285-
Py_XINCREF(current_param);
284+
current_param = PyDict_GetItemRefString(parameters, binding_name);
286285
} else {
287286
current_param = PyMapping_GetItemString(parameters, binding_name);
288287
}

Modules/itertoolsmodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4351,16 +4351,20 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
43514351
Py_ssize_t i;
43524352
PyObject *ittuple; /* tuple of iterators */
43534353
PyObject *result;
4354-
PyObject *fillvalue = Py_None;
4354+
PyObject *fillvalue;
43554355
Py_ssize_t tuplesize;
43564356

43574357
if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) {
4358-
fillvalue = PyDict_GetItemString(kwds, "fillvalue");
4358+
fillvalue = PyDict_GetItemRefString(kwds, "fillvalue");
43594359
if (fillvalue == NULL || PyDict_GET_SIZE(kwds) > 1) {
4360+
Py_XDECREF(fillvalue);
43604361
PyErr_SetString(PyExc_TypeError,
43614362
"zip_longest() got an unexpected keyword argument");
43624363
return NULL;
43634364
}
4365+
} else {
4366+
fillvalue = Py_None;
4367+
Py_INCREF(fillvalue);
43644368
}
43654369

43664370
/* args must be a tuple */
@@ -4407,7 +4411,6 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
44074411
lz->tuplesize = tuplesize;
44084412
lz->numactive = tuplesize;
44094413
lz->result = result;
4410-
Py_INCREF(fillvalue);
44114414
lz->fillvalue = fillvalue;
44124415
return (PyObject *)lz;
44134416
}

Modules/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
501501
PyObject *sys_path;
502502
PyObject *sysdict = interp->sysdict;
503503
if (sysdict != NULL) {
504-
sys_path = PyDict_GetItemString(sysdict, "path");
504+
sys_path = PyDict_GetItemRefString(sysdict, "path");
505505
}
506506
else {
507507
sys_path = NULL;
@@ -512,8 +512,10 @@ pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0)
512512
}
513513

514514
if (PyList_Insert(sys_path, 0, path0)) {
515+
Py_DECREF(sys_path);
515516
goto error;
516517
}
518+
Py_DECREF(sys_path);
517519
return 0;
518520

519521
error:

Modules/signalmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,9 @@ PyInit__signal(void)
13251325
goto finally;
13261326
#endif
13271327

1328-
x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1328+
x = IntHandler = PyDict_GetItemRefString(d, "default_int_handler");
13291329
if (!x)
13301330
goto finally;
1331-
Py_INCREF(IntHandler);
13321331

13331332
_Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
13341333
for (i = 1; i < NSIG; i++) {

Modules/socketmodule.c

Lines changed: 5 additions & 3 deletions

0 commit comments

Comments
 (0)