diff --git a/bblfsh/pyuast.cc b/bblfsh/pyuast.cc index 16ba4a2..ea89328 100644 --- a/bblfsh/pyuast.cc +++ b/bblfsh/pyuast.cc @@ -39,12 +39,12 @@ typedef struct { PyObject_HEAD ContextExt *ctx; NodeHandle handle; -} NodeExt; +} PyNodeExt; -static PyObject *NodeExt_load(NodeExt *self, PyObject *Py_UNUSED(ignored)); +static PyObject *PyNodeExt_load(PyNodeExt *self, PyObject *Py_UNUSED(ignored)); -static PyMethodDef NodeExt_methods[] = { - {"load", (PyCFunction) NodeExt_load, METH_NOARGS, +static PyMethodDef PyNodeExt_methods[] = { + {"load", (PyCFunction) PyNodeExt_load, METH_NOARGS, "Load external node to Python" }, {nullptr} // Sentinel @@ -52,10 +52,10 @@ static PyMethodDef NodeExt_methods[] = { extern "C" { - static PyTypeObject NodeExtType = { + static PyTypeObject PyNodeExtType = { PyVarObject_HEAD_INIT(nullptr, 0) "pyuast.NodeExt", // tp_name - sizeof(NodeExt), // tp_basicsize + sizeof(PyNodeExt), // tp_basicsize 0, // tp_itemsize 0, // tp_dealloc 0, // tp_print @@ -80,7 +80,7 @@ extern "C" 0, // tp_weaklistoffset 0, // tp_iter: __iter()__ method 0, // tp_iternext: next() method - NodeExt_methods, // tp_methods + PyNodeExt_methods, // tp_methods 0, // tp_members 0, // tp_getset 0, // tp_base @@ -181,12 +181,12 @@ class ContextExt { private: uast::Context *ctx; - // toPy allocates a new NodeExt with a specified handle. + // toPy allocates a new PyNodeExt with a specified handle. // Returns a new reference. PyObject* toPy(NodeHandle node) { if (node == 0) Py_RETURN_NONE; - NodeExt *pyObj = PyObject_New(NodeExt, &NodeExtType); + PyNodeExt *pyObj = PyObject_New(PyNodeExt, &PyNodeExtType); if (!pyObj) return nullptr; pyObj->ctx = this; @@ -194,19 +194,19 @@ class ContextExt { return (PyObject*)pyObj; } - // toHandle casts an object to NodeExt and returns its handle. + // toHandle casts an object to PyNodeExt and returns its handle. // Borrows the reference. NodeHandle toHandle(PyObject* obj) { if (!obj || obj == Py_None) return 0; - if (!PyObject_TypeCheck(obj, &NodeExtType)) { + if (!PyObject_TypeCheck(obj, &PyNodeExtType)) { const char* err = "unknown node type"; PyErr_SetString(PyExc_NotImplementedError, err); ctx->SetError(err); return 0; } - auto node = (NodeExt*)obj; + auto node = (PyNodeExt*)obj; return node->handle; } @@ -828,7 +828,7 @@ class Context { uast::Buffer data = ctx->Encode(toNode(node), format); return asPyBuffer(data); // TODO: this probably won't deallocate the buffer } - PyObject* LoadFrom(NodeExt *src) { + PyObject* LoadFrom(PyNodeExt *src) { auto sctx = src->ctx->ctx; NodeHandle snode = src->handle; @@ -837,7 +837,7 @@ class Context { } }; -static PyObject *NodeExt_load(NodeExt *self, PyObject *Py_UNUSED(ignored)) { +static PyObject *PyNodeExt_load(PyNodeExt *self, PyObject *Py_UNUSED(ignored)) { auto ctx = new Context(); PyObject* node = ctx->LoadFrom(self); delete(ctx); @@ -858,18 +858,18 @@ static void PyUastIter_dealloc(PyObject *self) { typedef struct { PyObject_HEAD Context *p; -} PyUast; +} PyContext; -static void PyUast_dealloc(PyObject *self) { +static void PyContext_dealloc(PyObject *self) { delete(((PyUast *)self)->p); // TODO: delete self? } -static PyObject *PyUast_root(PyUast *self, PyObject *Py_UNUSED(ignored)) { +static PyObject *PyContext_root(PyContext *self, PyObject *Py_UNUSED(ignored)) { return self->p->RootNode(); } -static PyObject *PyUast_filter(PyUast *self, PyObject *args) { +static PyObject *PyContext_filter(PyContext *self, PyObject *args) { PyObject *node = nullptr; char *query = nullptr; if (!PyArg_ParseTuple(args, "Os", &node, &query)) @@ -877,7 +877,7 @@ static PyObject *PyUast_filter(PyUast *self, PyObject *args) { return self->p->Filter(node, query); } -static PyObject *PyUast_encode(PyUast *self, PyObject *args) { +static PyObject *PyContext_encode(PyContext *self, PyObject *args) { PyObject *node = nullptr; UastFormat format = UAST_BINARY; // TODO: make it a kwarg and enum if (!PyArg_ParseTuple(args, "Oi", &node, &format)) @@ -885,14 +885,14 @@ static PyObject *PyUast_encode(PyUast *self, PyObject *args) { return self->p->Encode(node, format); } -static PyMethodDef PyUast_methods[] = { - {"root", (PyCFunction) PyUast_root, METH_NOARGS, +static PyMethodDef PyContext_methods[] = { + {"root", (PyCFunction) PyContext_root, METH_NOARGS, "Return the root node attached to this query context" }, - {"filter", (PyCFunction) PyUast_filter, METH_VARARGS, + {"filter", (PyCFunction) PyContext_filter, METH_VARARGS, "Filter a provided UAST with XPath" }, - {"encode", (PyCFunction) PyUast_encode, METH_VARARGS, + {"encode", (PyCFunction) PyContext_encode, METH_VARARGS, "Encodes a UAST into a buffer" }, {nullptr} // Sentinel @@ -900,12 +900,12 @@ static PyMethodDef PyUast_methods[] = { extern "C" { - static PyTypeObject PyUastType = { + static PyTypeObject PyContextType = { PyVarObject_HEAD_INIT(nullptr, 0) "pyuast.Context", // tp_name - sizeof(PyUast), // tp_basicsize + sizeof(PyContext), // tp_basicsize 0, // tp_itemsize - PyUast_dealloc, // tp_dealloc + PyContext_dealloc, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr @@ -928,7 +928,7 @@ extern "C" 0, // tp_weaklistoffset 0, // tp_iter: __iter()__ method 0, // tp_iternext: next() method - PyUast_methods, // tp_methods + PyContext_methods, // tp_methods 0, // tp_members 0, // tp_getset 0, // tp_base @@ -954,9 +954,9 @@ static PyObject *PyUastIter_new(PyObject *self, PyObject *args) { return nullptr; // the node can either be external or any other Python object - if (PyObject_TypeCheck(obj, &NodeExtType)) { + if (PyObject_TypeCheck(obj, &PyNodeExtType)) { // external node -> external iterator - auto node = (NodeExt*)obj; + auto node = (PyNodeExt*)obj; return node->ctx->Iterate(obj, (TreeOrder)order); } // Python object -> create a new context and attach it to an iterator @@ -964,7 +964,7 @@ static PyObject *PyUastIter_new(PyObject *self, PyObject *args) { return ctx->Iterate(obj, (TreeOrder)order, true); } -static PyObject *PyUastDecode(PyObject *self, PyObject *args) { +static PyObject *PyContextExt_decode(PyObject *self, PyObject *args) { PyObject *obj = nullptr; UastFormat format = UAST_BINARY; // TODO: make it a kwarg @@ -991,12 +991,12 @@ static PyObject *PyUastDecode(PyObject *self, PyObject *args) { return (PyObject*)pyU; } -static PyObject *PyUast_new(PyObject *self, PyObject *args) { +static PyObject *PyContext_new(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "")) { return nullptr; } - PyUast *pyU = PyObject_New(PyUast, &PyUastType); + PyContext *pyU = PyObject_New(PyContext, &PyContextType); if (!pyU) { return nullptr; } @@ -1006,8 +1006,8 @@ static PyObject *PyUast_new(PyObject *self, PyObject *args) { static PyMethodDef extension_methods[] = { {"iterator", PyUastIter_new, METH_VARARGS, "Get an iterator over a node"}, - {"decode", PyUastDecode, METH_VARARGS, "Decode UAST from a byte array"}, - {"uast", PyUast_new, METH_VARARGS, "Creates a new UAST context"}, + {"decode", PyContextDecode, METH_VARARGS, "Decode UAST from a byte array"}, + {"uast", PyContext_new, METH_VARARGS, "Creates a new UAST context"}, {nullptr, nullptr, 0, nullptr} }; @@ -1027,10 +1027,10 @@ PyMODINIT_FUNC PyInit_pyuast(void) { if (PyType_Ready(&PyContextExtType) < 0) return nullptr; - if (PyType_Ready(&NodeExtType) < 0) return nullptr; + if (PyType_Ready(&PyNodeExtType) < 0) return nullptr; if (PyType_Ready(&PyUastIterExtType) < 0) return nullptr; - if (PyType_Ready(&PyUastType) < 0) return nullptr; + if (PyType_Ready(&PyContextType) < 0) return nullptr; if (PyType_Ready(&PyUastIterType) < 0) return nullptr; return PyModule_Create(&module_def); }