##// END OF EJS Templates
parsers: fix refcount leak, simplify init of index (issue3417)...
Bryan O'Sullivan -
r16572:8d44b5a2 stable
parent child Browse files
Show More
@@ -470,8 +470,10 b' static void _index_clearcaches(indexObje'
470 Py_ssize_t i;
470 Py_ssize_t i;
471
471
472 for (i = 0; i < self->raw_length; i++) {
472 for (i = 0; i < self->raw_length; i++) {
473 Py_XDECREF(self->cache[i]);
473 if (self->cache[i]) {
474 self->cache[i] = NULL;
474 Py_DECREF(self->cache[i]);
475 self->cache[i] = NULL;
476 }
475 }
477 }
476 free(self->cache);
478 free(self->cache);
477 self->cache = NULL;
479 self->cache = NULL;
@@ -957,9 +959,19 b' static long inline_scan(indexObject *sel'
957 return len;
959 return len;
958 }
960 }
959
961
960 static int index_real_init(indexObject *self, const char *data, int size,
962 static int index_init(indexObject *self, PyObject *args)
961 PyObject *inlined_obj, PyObject *data_obj)
962 {
963 {
964 PyObject *data_obj, *inlined_obj;
965 Py_ssize_t size;
966
967 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
968 return -1;
969 if (!PyString_Check(data_obj)) {
970 PyErr_SetString(PyExc_TypeError, "data is not a string");
971 return -1;
972 }
973 size = PyString_GET_SIZE(data_obj);
974
963 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
975 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
964 self->data = data_obj;
976 self->data = data_obj;
965 self->cache = NULL;
977 self->cache = NULL;
@@ -971,7 +983,6 b' static int index_real_init(indexObject *'
971 self->ntdepth = self->ntsplits = 0;
983 self->ntdepth = self->ntsplits = 0;
972 self->ntlookups = self->ntmisses = 0;
984 self->ntlookups = self->ntmisses = 0;
973 self->ntrev = -1;
985 self->ntrev = -1;
974 Py_INCREF(self->data);
975
986
976 if (self->inlined) {
987 if (self->inlined) {
977 long len = inline_scan(self, NULL);
988 long len = inline_scan(self, NULL);
@@ -987,27 +998,16 b' static int index_real_init(indexObject *'
987 self->raw_length = size / 64;
998 self->raw_length = size / 64;
988 self->length = self->raw_length + 1;
999 self->length = self->raw_length + 1;
989 }
1000 }
1001 Py_INCREF(self->data);
990
1002
991 return 0;
1003 return 0;
992 bail:
1004 bail:
993 return -1;
1005 return -1;
994 }
1006 }
995
1007
996 static int index_init(indexObject *self, PyObject *args, PyObject *kwds)
997 {
998 const char *data;
999 int size;
1000 PyObject *inlined_obj;
1001
1002 if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj))
1003 return -1;
1004
1005 return index_real_init(self, data, size, inlined_obj,
1006 PyTuple_GET_ITEM(args, 0));
1007 }
1008
1009 static PyObject *index_nodemap(indexObject *self)
1008 static PyObject *index_nodemap(indexObject *self)
1010 {
1009 {
1010 Py_INCREF(self);
1011 return (PyObject *)self;
1011 return (PyObject *)self;
1012 }
1012 }
1013
1013
@@ -1106,26 +1106,19 b' static PyTypeObject indexType = {'
1106 */
1106 */
1107 static PyObject *parse_index2(PyObject *self, PyObject *args)
1107 static PyObject *parse_index2(PyObject *self, PyObject *args)
1108 {
1108 {
1109 const char *data;
1109 PyObject *tuple = NULL, *cache = NULL;
1110 int size, ret;
1111 PyObject *inlined_obj, *tuple = NULL, *cache = NULL;
1112 indexObject *idx;
1110 indexObject *idx;
1113
1111 int ret;
1114 if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj))
1115 return NULL;
1116
1112
1117 idx = PyObject_New(indexObject, &indexType);
1113 idx = PyObject_New(indexObject, &indexType);
1118
1119 if (idx == NULL)
1114 if (idx == NULL)
1120 goto bail;
1115 goto bail;
1121
1116
1122 ret = index_real_init(idx, data, size, inlined_obj,
1117 ret = index_init(idx, args);
1123 PyTuple_GET_ITEM(args, 0));
1118 if (ret == -1)
1124 if (ret)
1125 goto bail;
1119 goto bail;
1126
1120
1127 if (idx->inlined) {
1121 if (idx->inlined) {
1128 Py_INCREF(idx->data);
1129 cache = Py_BuildValue("iO", 0, idx->data);
1122 cache = Py_BuildValue("iO", 0, idx->data);
1130 if (cache == NULL)
1123 if (cache == NULL)
1131 goto bail;
1124 goto bail;
@@ -1134,8 +1127,6 b' static PyObject *parse_index2(PyObject *'
1134 Py_INCREF(cache);
1127 Py_INCREF(cache);
1135 }
1128 }
1136
1129
1137 Py_INCREF(idx);
1138
1139 tuple = Py_BuildValue("NN", idx, cache);
1130 tuple = Py_BuildValue("NN", idx, cache);
1140 if (!tuple)
1131 if (!tuple)
1141 goto bail;
1132 goto bail;
General Comments 0
You need to be logged in to leave comments. Login now