Skip to content

Commit b948553

Browse files
benjaminpmethane
authored andcommitted
simplify memory management of the conv dictionary
In ResultObject_Initialize, always borrow the conv dictionary from the caller. This simplifies the code, removes an allocation, and fixes ref leaks in error cases.
1 parent 73ccdcd commit b948553

File tree

1 file changed

+1
-13
lines changed

1 file changed

+1
-13
lines changed

Diff for: _mysql.c

+1-13
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,6 @@ _mysql_ResultObject_Initialize(
363363
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|iO", kwlist,
364364
&_mysql_ConnectionObject_Type, &conn, &use, &conv))
365365
return -1;
366-
if (!conv) {
367-
if (!(conv = PyDict_New()))
368-
return -1;
369-
}
370-
else
371-
Py_INCREF(conv);
372366

373367
self->conn = (PyObject *) conn;
374368
Py_INCREF(conn);
@@ -387,29 +381,25 @@ _mysql_ResultObject_Initialize(
387381
return -1;
388382
}
389383
self->converter = PyTuple_New(0);
390-
Py_DECREF(conv);
391384
return 0;
392385
}
393386
n = mysql_num_fields(result);
394387
self->nfields = n;
395388
if (!(self->converter = PyTuple_New(n))) {
396-
Py_DECREF(conv);
397389
return -1;
398390
}
399391
fields = mysql_fetch_fields(result);
400392
for (i=0; i<n; i++) {
401393
PyObject *tmp, *fun;
402394
tmp = PyInt_FromLong((long) fields[i].type);
403395
if (!tmp) {
404-
Py_DECREF(conv);
405396
return -1;
406397
}
407-
fun = PyObject_GetItem(conv, tmp);
398+
fun = conv ? PyObject_GetItem(conv, tmp) : NULL;
408399
Py_DECREF(tmp);
409400
if (!fun) {
410401
if (PyErr_Occurred()) {
411402
if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
412-
Py_DECREF(conv);
413403
return -1;
414404
}
415405
PyErr_Clear();
@@ -428,7 +418,6 @@ _mysql_ResultObject_Initialize(
428418
PyObject *t = PySequence_GetItem(fun, j);
429419
if (!t) {
430420
Py_DECREF(fun);
431-
Py_DECREF(conv);
432421
return -1;
433422
}
434423
if (PyTuple_Check(t) && PyTuple_GET_SIZE(t) == 2) {
@@ -463,7 +452,6 @@ _mysql_ResultObject_Initialize(
463452
PyTuple_SET_ITEM(self->converter, i, fun);
464453
}
465454

466-
Py_DECREF(conv);
467455
return 0;
468456
}
469457

0 commit comments

Comments
 (0)