Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Closed
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
19 changes: 15 additions & 4 deletions bblfsh/pyuast.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ static const char *InternalType(const void *node) {
static const char *Token(const void *node) {
return String(node, "token");
}

static size_t ChildrenSize(const void *node) {
return Size(node, "children");
}
Expand Down Expand Up @@ -68,8 +67,13 @@ static const char *PropertyKeyAt(const void *node, int index) {
return NULL;
}

const char *retval = NULL;
PyObject *keys = PyMapping_Keys(properties);
return keys ? PyUnicode_AsUTF8(ItemAt(keys, index)) : NULL;
if (keys != NULL) {
retval = PyUnicode_AsUTF8(ItemAt(keys, index));
Py_DECREF(keys);
}
return retval;
}

static const char *PropertyValueAt(const void *node, int index) {
Expand All @@ -78,8 +82,13 @@ static const char *PropertyValueAt(const void *node, int index) {
return NULL;
}

const char *retval = NULL;
PyObject *values = PyMapping_Values(properties);
return values ? PyUnicode_AsUTF8(ItemAt(values, index)) : NULL;
if (values != NULL) {
retval = PyUnicode_AsUTF8(ItemAt(values, index));
Py_DECREF(values);
}
return retval;
}

static uint32_t PositionValue(const void* node, const char *prop, const char *field) {
Expand Down Expand Up @@ -275,7 +284,9 @@ static PyObject *PyFilter(PyObject *self, PyObject *args)
PyList_SET_ITEM(list, i, node);
}
NodesFree(nodes);
return PySeqIter_New(list);
PyObject *iter = PySeqIter_New(list);
Py_DECREF(list);
return iter;
}

static PyMethodDef extension_methods[] = {
Expand Down
14 changes: 14 additions & 0 deletions bblfsh/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,19 @@ def _validate_filter(self, resp):
self.assertEqual(next(results).token, "docker")


def testManyFilters(self):
root = self.client.parse(__file__).uast
root.properties['k1'] = 'v2'
root.properties['k2'] = 'v1'

import resource
before = resource.getrusage(resource.RUSAGE_SELF)
for _ in range(100):
filter(root, "//*[@roleIdentifier]")
after = resource.getrusage(resource.RUSAGE_SELF)

# Check that memory usage has not doubled after running the filter
self.assertLess(after[2] / before[2], 2.0)

if __name__ == "__main__":
unittest.main()