Commit e12a45ef authored by Jelte Jansen's avatar Jelte Jansen
Browse files

review mentioned i wasn't following coding guidelines, updating them

also updated a few typo's in comments


git-svn-id: svn://bind10.isc.org/svn/bind10/experiments/python-binding@2234 e5f2f494-b856-4b98-b285-d166d9295462
parent 02907445
......@@ -70,8 +70,7 @@ static PyModuleDef libdns_python = {
};
PyMODINIT_FUNC
PyInit_libdns_python(void)
{
PyInit_libdns_python(void) {
PyObject *mod = PyModule_Create(&libdns_python);
if (mod == NULL) {
return NULL;
......
......@@ -17,8 +17,7 @@
#include <Python.h>
int
readDataFromSequence(uint8_t *data, size_t len, PyObject* sequence)
{
readDataFromSequence(uint8_t *data, size_t len, PyObject* sequence) {
PyObject* el = NULL;
for (size_t i = 0; i < len; i++) {
el = PySequence_GetItem(sequence, 0);
......@@ -46,7 +45,8 @@ readDataFromSequence(uint8_t *data, size_t len, PyObject* sequence)
}
void addClassVariable(PyTypeObject& c, const char* name, PyObject* obj)
void addClassVariable(PyTypeObject& c, const char* name,
PyObject* obj)
{
PyDict_SetItemString(c.tp_dict, name, obj);
}
This diff is collapsed.
......@@ -118,16 +118,14 @@ static PyTypeObject messagerenderer_type = {
};
static int
MessageRenderer_init(s_MessageRenderer* self)
{
MessageRenderer_init(s_MessageRenderer* self) {
self->outputbuffer = new OutputBuffer(4096);
self->messagerenderer = new MessageRenderer(*self->outputbuffer);
return 0;
}
static void
MessageRenderer_destroy(s_MessageRenderer* self)
{
MessageRenderer_destroy(s_MessageRenderer* self) {
delete self->messagerenderer;
delete self->outputbuffer;
self->messagerenderer = NULL;
......@@ -135,20 +133,19 @@ MessageRenderer_destroy(s_MessageRenderer* self)
}
static PyObject*
MessageRenderer_getData(s_MessageRenderer* self)
{
return Py_BuildValue("y#", self->messagerenderer->getData(), self->messagerenderer->getLength());
MessageRenderer_getData(s_MessageRenderer* self) {
return Py_BuildValue("y#",
self->messagerenderer->getData(),
self->messagerenderer->getLength());
}
static PyObject*
MessageRenderer_getLength(s_MessageRenderer* self)
{
MessageRenderer_getLength(s_MessageRenderer* self) {
return Py_BuildValue("I", self->messagerenderer->getLength());
}
static PyObject*
MessageRenderer_isTruncated(s_MessageRenderer* self)
{
MessageRenderer_isTruncated(s_MessageRenderer* self) {
if (self->messagerenderer->isTruncated()) {
Py_RETURN_TRUE;
} else {
......@@ -157,20 +154,19 @@ MessageRenderer_isTruncated(s_MessageRenderer* self)
}
static PyObject*
MessageRenderer_getLengthLimit(s_MessageRenderer* self)
{
MessageRenderer_getLengthLimit(s_MessageRenderer* self) {
return Py_BuildValue("I", self->messagerenderer->getLengthLimit());
}
static PyObject*
MessageRenderer_setTruncated(s_MessageRenderer* self)
{
MessageRenderer_setTruncated(s_MessageRenderer* self) {
self->messagerenderer->setTruncated();
Py_RETURN_NONE;
}
static PyObject*
MessageRenderer_setLengthLimit(s_MessageRenderer* self, PyObject* args)
MessageRenderer_setLengthLimit(s_MessageRenderer* self,
PyObject* args)
{
size_t lengthlimit;
if (!PyArg_ParseTuple(args, "I", &lengthlimit)) {
......@@ -185,8 +181,7 @@ MessageRenderer_setLengthLimit(s_MessageRenderer* self, PyObject* args)
// Module Initialization, all statics are initialized here
bool
initModulePart_MessageRenderer(PyObject* mod)
{
initModulePart_MessageRenderer(PyObject* mod) {
// Add the exceptions to the module
// Add the enums to the module
......
......@@ -130,7 +130,9 @@ static PyTypeObject name_comparison_result_type = {
// TODO: is there also a way to just not define it?
static int
NameComparisonResult_init(s_NameComparisonResult* self UNUSED_PARAM, PyObject* args UNUSED_PARAM) {
NameComparisonResult_init(s_NameComparisonResult* self UNUSED_PARAM,
PyObject* args UNUSED_PARAM)
{
PyErr_SetString(PyExc_NotImplementedError,
"NameComparisonResult can't be built directly");
return -1;
......@@ -281,8 +283,7 @@ static PyTypeObject name_type = {
static int
Name_init(s_Name* self, PyObject* args)
{
Name_init(s_Name* self, PyObject* args) {
const char* s;
PyObject* downcase = Py_False;
......@@ -360,16 +361,14 @@ Name_init(s_Name* self, PyObject* args)
}
static void
Name_destroy(s_Name* self)
{
Name_destroy(s_Name* self) {
delete self->name;
self->name = NULL;
Py_TYPE(self)->tp_free(self);
}
static PyObject*
Name_at(s_Name* self, PyObject* args)
{
Name_at(s_Name* self, PyObject* args) {
size_t pos;
if (!PyArg_ParseTuple(args, "i", &pos)) {
return NULL;
......@@ -384,33 +383,28 @@ Name_at(s_Name* self, PyObject* args)
}
static PyObject*
Name_getLength(s_Name* self)
{
Name_getLength(s_Name* self) {
return Py_BuildValue("i", self->name->getLength());
}
static PyObject*
Name_getLabelCount(s_Name* self)
{
Name_getLabelCount(s_Name* self) {
return Py_BuildValue("i", self->name->getLabelCount());
}
static PyObject*
Name_toText(s_Name* self)
{
Name_toText(s_Name* self) {
return Py_BuildValue("s", self->name->toText().c_str());
}
static PyObject*
Name_str(PyObject* self)
{
Name_str(PyObject* self) {
// Simply call the to_text method we already defined
return PyObject_CallMethod(self, (char*)"to_text", (char*)"");
}
static PyObject*
Name_toWire(s_Name* self, PyObject* args)
{
Name_toWire(s_Name* self, PyObject* args) {
PyObject* bytes;
s_MessageRenderer* mr;
......@@ -470,8 +464,7 @@ Name_equals(s_Name* self, PyObject* args) {
}
static PyObject*
Name_split(s_Name* self, PyObject* args)
{
Name_split(s_Name* self, PyObject* args) {
unsigned int first, n;
s_Name* ret = NULL;
......@@ -511,8 +504,7 @@ Name_split(s_Name* self, PyObject* args)
#include <iostream>
static PyObject*
Name_richcmp(s_Name* n1, s_Name* n2, int op)
{
Name_richcmp(s_Name* n1, s_Name* n2, int op) {
bool c;
// Check for null and if the types match. If different type,
......@@ -553,8 +545,7 @@ Name_richcmp(s_Name* n1, s_Name* n2, int op)
}
static PyObject*
Name_reverse(s_Name* self)
{
Name_reverse(s_Name* self) {
s_Name* ret = PyObject_New(s_Name, &name_type);
if (ret != NULL) {
......@@ -568,8 +559,7 @@ Name_reverse(s_Name* self)
}
static PyObject*
Name_concatenate(s_Name* self, PyObject* args)
{
Name_concatenate(s_Name* self, PyObject* args) {
s_Name* other;
if (!PyArg_ParseTuple(args, "O!", &name_type, (PyObject**) &other))
......@@ -592,16 +582,14 @@ Name_concatenate(s_Name* self, PyObject* args)
}
static PyObject*
Name_downcase(s_Name* self)
{
Name_downcase(s_Name* self) {
self->name->downcase();
Py_INCREF(self);
return (PyObject*) self;
}
static PyObject*
Name_isWildCard(s_Name* self)
{
Name_isWildCard(s_Name* self) {
if (self->name->isWildcard()) {
Py_RETURN_TRUE;
} else {
......@@ -613,8 +601,7 @@ Name_isWildCard(s_Name* self)
// Module Initialization, all statics are initialized here
bool
initModulePart_Name(PyObject* mod)
{
initModulePart_Name(PyObject* mod) {
// Add the classes to the module
// We initialize the static description object with PyType_Ready(),
// then add it to the module
......
......@@ -128,8 +128,7 @@ static PyTypeObject question_type = {
};
static int
Question_init(s_Question* self, PyObject* args)
{
Question_init(s_Question* self, PyObject* args) {
// The constructor argument can be a string ("IN"), an integer (1),
// or a sequence of numbers between 0 and 255 (wire code)
......@@ -183,15 +182,13 @@ Question_init(s_Question* self, PyObject* args)
}
static void
Question_destroy(s_Question* self)
{
Question_destroy(s_Question* self) {
self->question.reset();
Py_TYPE(self)->tp_free(self);
}
static PyObject*
Question_getName(s_Question* self)
{
Question_getName(s_Question* self) {
s_Name* name;
// is this the best way to do this?
......@@ -209,8 +206,7 @@ Question_getName(s_Question* self)
}
static PyObject*
Question_getType(s_Question* self)
{
Question_getType(s_Question* self) {
s_RRType* rrtype;
rrtype = (s_RRType*)rrtype_type.tp_alloc(&rrtype_type, 0);
......@@ -227,8 +223,7 @@ Question_getType(s_Question* self)
}
static PyObject*
Question_getClass(s_Question* self)
{
Question_getClass(s_Question* self) {
s_RRClass* rrclass;
rrclass = (s_RRClass*)rrclass_type.tp_alloc(&rrclass_type, 0);
......@@ -246,22 +241,19 @@ Question_getClass(s_Question* self)
static PyObject*
Question_toText(s_Question* self)
{
Question_toText(s_Question* self) {
// Py_BuildValue makes python objects from native data
return Py_BuildValue("s", self->question->toText().c_str());
}
static PyObject*
Question_str(PyObject* self)
{
Question_str(PyObject* self) {
// Simply call the to_text method we already defined
return PyObject_CallMethod(self, (char*)"to_text", (char*)"");
}
static PyObject*
Question_toWire(s_Question* self, PyObject* args)
{
Question_toWire(s_Question* self, PyObject* args) {
PyObject* bytes;
s_MessageRenderer* mr;
......@@ -270,13 +262,15 @@ Question_toWire(s_Question* self, PyObject* args)
OutputBuffer buffer(255);
self->question->toWire(buffer);
PyObject* n = PyBytes_FromStringAndSize((const char*) buffer.getData(), buffer.getLength());
PyObject* n = PyBytes_FromStringAndSize((const char*) buffer.getData(),
buffer.getLength());
PyObject* result = PySequence_InPlaceConcat(bytes_o, n);
// We need to release the object we temporarily created here
// to prevent memory leak
Py_DECREF(n);
return result;
} else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type, (PyObject**) &mr)) {
} else if (PyArg_ParseTuple(args, "O!", &messagerenderer_type,
(PyObject**) &mr)) {
self->question->toWire(*mr->messagerenderer);
// If we return NULL it is seen as an error, so use this for
// None returns
......@@ -293,8 +287,7 @@ Question_toWire(s_Question* self, PyObject* args)
// Module Initialization, all statics are initialized here
bool
initModulePart_Question(PyObject* mod)
{
initModulePart_Question(PyObject* mod) {
// Add the exceptions to the module
// We initialize the static description object with PyType_Ready(),
......
......@@ -137,8 +137,7 @@ static PyTypeObject rdata_type = {
};
static int
Rdata_init(s_Rdata* self, PyObject* args)
{
Rdata_init(s_Rdata* self, PyObject* args) {
s_RRType* rrtype;
s_RRClass* rrclass;
const char* s;
......@@ -156,8 +155,7 @@ Rdata_init(s_Rdata* self, PyObject* args)
}
static void
Rdata_destroy(s_Rdata* self)
{
Rdata_destroy(s_Rdata* self) {
// Clear the shared_ptr so that its reference count is zero
// before we call tp_free() (there is no direct release())
self->rdata.reset();
......@@ -165,22 +163,19 @@ Rdata_destroy(s_Rdata* self)
}
static PyObject*
Rdata_toText(s_Rdata* self)
{
Rdata_toText(s_Rdata* self) {
// Py_BuildValue makes python objects from native data
return Py_BuildValue("s", self->rdata->toText().c_str());
}
static PyObject*
Rdata_str(PyObject* self)
{
Rdata_str(PyObject* self) {
// Simply call the to_text method we already defined
return PyObject_CallMethod(self, (char*)"to_text", (char*)"");
}
static PyObject*
Rdata_toWire(s_Rdata* self, PyObject* args)
{
Rdata_toWire(s_Rdata* self, PyObject* args) {
PyObject* bytes;
s_MessageRenderer* mr;
......@@ -212,8 +207,7 @@ Rdata_toWire(s_Rdata* self, PyObject* args)
// Module Initialization, all statics are initialized here
bool
initModulePart_Rdata(PyObject* mod)
{
initModulePart_Rdata(PyObject* mod) {
// We initialize the static description object with PyType_Ready(),
// then add it to the module. This is not just a check! (leaving
// this out results in segmentation faults)
......
......@@ -37,7 +37,7 @@ static PyObject* po_IncompleteRRClass;
// RRClass
//
// The s_* Class simply coverst one instantiation of the object
// The s_* Class simply covers one instantiation of the object
typedef struct {
PyObject_HEAD
RRClass* rrclass;
......@@ -154,8 +154,7 @@ static PyTypeObject rrclass_type = {
};
static int
RRClass_init(s_RRClass* self, PyObject* args)
{
RRClass_init(s_RRClass* self, PyObject* args) {
const char* s;
unsigned int i;
PyObject* bytes = NULL;
......@@ -203,8 +202,7 @@ RRClass_init(s_RRClass* self, PyObject* args)
}
static void
RRClass_destroy(s_RRClass* self)
{
RRClass_destroy(s_RRClass* self) {
if (self->rrclass != NULL)
delete self->rrclass;
self->rrclass = NULL;
......@@ -212,22 +210,19 @@ RRClass_destroy(s_RRClass* self)
}
static PyObject*
RRClass_toText(s_RRClass* self)
{
RRClass_toText(s_RRClass* self) {
// Py_BuildValue makes python objects from native data
return Py_BuildValue("s", self->rrclass->toText().c_str());
}
static PyObject*
RRClass_str(PyObject* self)
{
RRClass_str(PyObject* self) {
// Simply call the to_text method we already defined
return PyObject_CallMethod(self, (char*)"to_text", (char*)"");
}
static PyObject*
RRClass_toWire(s_RRClass* self, PyObject* args)
{
RRClass_toWire(s_RRClass* self, PyObject* args) {
PyObject* bytes;
s_MessageRenderer* mr;
......@@ -255,14 +250,12 @@ RRClass_toWire(s_RRClass* self, PyObject* args)
}
static PyObject*
RRClass_getCode(s_RRClass* self)
{
RRClass_getCode(s_RRClass* self) {
return Py_BuildValue("I", self->rrclass->getCode());
}
static PyObject*
RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op)
{
RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op) {
bool c;
// Check for null and if the types match. If different type,
......@@ -303,8 +296,7 @@ RRClass_richcmp(s_RRClass* self, s_RRClass* other, int op)
Py_RETURN_FALSE;
}
static PyObject* RRClass_IN(s_RRClass *self UNUSED_PARAM)
{
static PyObject* RRClass_IN(s_RRClass *self UNUSED_PARAM) {
s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
if (ret != NULL) {
ret->rrclass = new RRClass(RRClass::IN());
......@@ -316,8 +308,7 @@ static PyObject* RRClass_IN(s_RRClass *self UNUSED_PARAM)
return (PyObject*) ret;
}
static PyObject* RRClass_CH(s_RRClass *self UNUSED_PARAM)
{
static PyObject* RRClass_CH(s_RRClass *self UNUSED_PARAM) {
s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
if (ret != NULL) {
ret->rrclass = new RRClass(RRClass::CH());
......@@ -329,8 +320,7 @@ static PyObject* RRClass_CH(s_RRClass *self UNUSED_PARAM)
return (PyObject*) ret;
}
static PyObject* RRClass_HS(s_RRClass *self UNUSED_PARAM)
{
static PyObject* RRClass_HS(s_RRClass *self UNUSED_PARAM) {
s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
if (ret != NULL) {
ret->rrclass = new RRClass(RRClass::HS());
......@@ -342,8 +332,7 @@ static PyObject* RRClass_HS(s_RRClass *self UNUSED_PARAM)
return (PyObject*) ret;
}
static PyObject* RRClass_NONE(s_RRClass *self UNUSED_PARAM)
{
static PyObject* RRClass_NONE(s_RRClass *self UNUSED_PARAM) {
s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
if (ret != NULL) {
ret->rrclass = new RRClass(RRClass::NONE());
......@@ -355,8 +344,7 @@ static PyObject* RRClass_NONE(s_RRClass *self UNUSED_PARAM)
return (PyObject*) ret;
}
static PyObject* RRClass_ANY(s_RRClass *self UNUSED_PARAM)
{
static PyObject* RRClass_ANY(s_RRClass *self UNUSED_PARAM) {
s_RRClass* ret = PyObject_New(s_RRClass, &rrclass_type);
if (ret != NULL) {
ret->rrclass = new RRClass(RRClass::ANY());
......@@ -372,8 +360,7 @@ static PyObject* RRClass_ANY(s_RRClass *self UNUSED_PARAM)
// Module Initialization, all statics are initialized here
bool
initModulePart_RRClass(PyObject* mod)
{
initModulePart_RRClass(PyObject* mod) {
// Add the exceptions to the module
po_InvalidRRClass = PyErr_NewException("libdns_python.InvalidRRClass", NULL, NULL);
Py_INCREF(po_InvalidRRClass);
......
......@@ -170,8 +170,7 @@ static PyTypeObject rrset_type = {
};
static int
RRset_init(s_RRset* self, PyObject* args UNUSED_PARAM)
{
RRset_init(s_RRset* self, PyObject* args UNUSED_PARAM) {
s_Name* name;
s_RRClass* rrclass;
s_RRType* rrtype;
......@@ -192,8 +191,7 @@ RRset_init(s_RRset* self, PyObject* args UNUSED_PARAM)
}
static void
RRset_destroy(s_RRset* self)
{
RRset_destroy(s_RRset* self) {
// Clear the shared_ptr so that its reference count is zero
// before we call tp_free() (there is no direct release())
self->rrset.reset();
......@@ -201,14 +199,12 @@ RRset_destroy(s_RRset* self)
}
static PyObject*
RRset_getRdataCount(s_RRset* self)
{
RRset_getRdataCount(s_RRset* self) {
return Py_BuildValue("I", self->rrset->getRdataCount());
}
static PyObject*
RRset_getName(s_RRset* self)
{
RRset_getName(s_RRset* self) {
s_Name* name;
// is this the best way to do this?
......@@ -226,8 +222,7 @@ RRset_getName(s_RRset* self)
}
static PyObject*
RRset_getClass(s_RRset* self)
{
RRset_getClass(s_RRset* self) {
s_RRClass* rrclass;
rrclass = (s_RRClass*)rrclass_type.tp_alloc(&rrclass_type, 0);
......@@ -244,8 +239,7 @@ RRset_getClass(s_RRset* self)
}
static PyObject*
RRset_getType(s_RRset* self)
{
RRset_getType(s_RRset* self) {
s_RRType* rrtype;
rrtype = (s_RRType*)rrtype_type.tp_alloc(&rrtype_type, 0);
......@@ -262,8 +256,7 @@ RRset_getType(s_RRset* self)
}
static PyObject*
RRset_getTTL(s_RRset* self)
{
RRset_getTTL(s_RRset* self) {
s_RRTTL* rrttl;
rrttl = (s_RRTTL*)rrttl_type.tp_alloc(&rrttl_type, 0);
......@@ -280,8 +273,7 @@ RRset_getTTL(s_RRset* self)
}
static PyObject*
RRset_setName(s_RRset* self, PyObject* args)
{
RRset_setName(s_RRset* self, PyObject* args) {
s_Name* name;
if (!PyArg_ParseTuple(args, "O!", &name_type, &name)) {
return NULL;
......@@ -291,8 +283,7 @@ RRset_setName(s_RRset* self, PyObject* args)
}
static PyObject*
RRset_setTTL(s_RRset* self, PyObject* args)
{
RRset_setTTL(s_RRset* self, PyObject* args) {
s_RRTTL* rrttl;
if (!PyArg_ParseTuple(args, "O!", &rrttl_type, &rrttl)) {
return NULL;
......@@ -302,8 +293,7 @@ RRset_setTTL(s_RRset* self, PyObject* args)
}