Browse Source

moved exceptions and constants from top-level to the class they are relevant for

git-svn-id: svn://bind10.isc.org/svn/bind10/experiments/python-binding@2081 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
ca26ccabf2
2 changed files with 68 additions and 82 deletions
  1. 58 72
      src/lib/dns/python/name_python.cc
  2. 10 10
      src/lib/dns/python/tests/name_python_test.py

+ 58 - 72
src/lib/dns/python/name_python.cc

@@ -44,6 +44,7 @@ static PyObject* po_NameRelation;
 // Initialization and addition of these go in the module init at the
 // end
 //
+/*
 static PyObject* po_MAX_WIRE;
 static PyObject* po_MAX_LABELS;
 static PyObject* po_MAX_LABELLEN;
@@ -51,7 +52,7 @@ static PyObject* po_MAX_COMPRESS_POINTER;
 static PyObject* po_COMPRESS_POINTER_MARK8;
 static PyObject* po_COMPRESS_POINTER_MARK16;
 static PyObject* po_ROOT_NAME;
-
+*/
 
 
 //
@@ -565,77 +566,6 @@ Name_isWildCard(s_Name* self)
 bool
 initModulePart_Name(PyObject* mod)
 {
-    // Add the exceptions to the module
-    po_EmptyLabel = PyErr_NewException("libdns_python.EmptyLabel", NULL, NULL);
-    Py_INCREF(po_EmptyLabel);
-    PyModule_AddObject(mod, "EmptyLabel", po_EmptyLabel);
-
-    po_TooLongName = PyErr_NewException("libdns_python.TooLongName", NULL, NULL);
-    Py_INCREF(po_TooLongName);
-    PyModule_AddObject(mod, "TooLongName", po_TooLongName);
-
-    po_TooLongLabel = PyErr_NewException("libdns_python.TooLongLabel", NULL, NULL);
-    Py_INCREF(po_TooLongLabel);
-    PyModule_AddObject(mod, "TooLongLabel", po_TooLongLabel);
-
-    po_BadLabelType = PyErr_NewException("libdns_python.BadLabelType", NULL, NULL);
-    Py_INCREF(po_BadLabelType);
-    PyModule_AddObject(mod, "BadLabelType", po_BadLabelType);
-
-    po_BadEscape = PyErr_NewException("libdns_python.BadEscape", NULL, NULL);
-    Py_INCREF(po_BadEscape);
-    PyModule_AddObject(mod, "BadEscape", po_BadEscape);
-
-    po_IncompleteName = PyErr_NewException("libdns_python.IncompleteName", NULL, NULL);
-    Py_INCREF(po_IncompleteName);
-    PyModule_AddObject(mod, "IncompleteName", po_IncompleteName);
-
-    po_InvalidBufferPosition = PyErr_NewException("libdns_python.InvalidBufferPosition", NULL, NULL);
-    Py_INCREF(po_InvalidBufferPosition);
-    PyModule_AddObject(mod, "InvalidBufferPosition", po_InvalidBufferPosition);
-
-    po_DNSMessageFORMERR = PyErr_NewException("libdns_python.DNSMessageFORMERR", NULL, NULL);
-    Py_INCREF(po_DNSMessageFORMERR);
-    PyModule_AddObject(mod, "DNSMessageFORMERR", po_DNSMessageFORMERR);
-
-    po_IscException = PyErr_NewException("libdns_python.IscException", NULL, NULL);
-    Py_INCREF(po_IncompleteName);
-    PyModule_AddObject(mod, "IscException", po_IscException);
-
-    // Add the enums to the module
-    po_NameRelation = Py_BuildValue("{i:s,i:s,i:s,i:s}",
-                                    0, "SUPERDOMAIN",
-                                    1, "SUBDOMAIN",
-                                    2, "EQUAL",
-                                    3, "COMMONANCESTOR");
-    Py_INCREF(po_NameRelation);
-    PyModule_AddObject(mod, "NameRelation", po_NameRelation);
-
-    // Add the constants to the module
-    po_MAX_WIRE = Py_BuildValue("I", 255U);
-    Py_INCREF(po_MAX_WIRE);
-    PyModule_AddObject(mod, "MAX_WIRE", po_MAX_WIRE);
-    po_MAX_LABELS = Py_BuildValue("I", 128U);
-    Py_INCREF(po_MAX_LABELS);
-    PyModule_AddObject(mod, "MAX_LABELS", po_MAX_LABELS);
-    po_MAX_LABELLEN = Py_BuildValue("I", 63U);
-    Py_INCREF(po_MAX_LABELLEN);
-    PyModule_AddObject(mod, "MAX_LABELLEN", po_MAX_LABELLEN);
-    po_MAX_COMPRESS_POINTER = Py_BuildValue("I", 0x3fffU);
-    Py_INCREF(po_MAX_COMPRESS_POINTER);
-    PyModule_AddObject(mod, "MAX_COMPRESS_POINTER", po_MAX_COMPRESS_POINTER);
-    po_COMPRESS_POINTER_MARK8 = Py_BuildValue("I", 0xc0U);
-    Py_INCREF(po_COMPRESS_POINTER_MARK8);
-    PyModule_AddObject(mod, "COMPRESS_POINTER_MARK8", po_COMPRESS_POINTER_MARK8);
-    po_COMPRESS_POINTER_MARK16 = Py_BuildValue("I", 0xc000U);
-    Py_INCREF(po_COMPRESS_POINTER_MARK16);
-    PyModule_AddObject(mod, "COMPRESS_POINTER_MARK16", po_COMPRESS_POINTER_MARK16);
-    s_Name* root_name = PyObject_New(s_Name, &name_type);
-    root_name->name = new Name(".");
-    po_ROOT_NAME = (PyObject*) root_name;
-    Py_INCREF(po_ROOT_NAME);
-    PyModule_AddObject(mod, "ROOT_NAME", po_ROOT_NAME);
-
     // Add the classes to the module
     // We initialize the static description object with PyType_Ready(),
     // then add it to the module
@@ -645,6 +575,15 @@ initModulePart_Name(PyObject* mod)
         return false;
     }
     Py_INCREF(&name_comparison_result_type);
+
+    // Add the enums to the module
+    po_NameRelation = Py_BuildValue("{i:s,i:s,i:s,i:s}",
+                                    0, "SUPERDOMAIN",
+                                    1, "SUBDOMAIN",
+                                    2, "EQUAL",
+                                    3, "COMMONANCESTOR");
+    addClassVariable(name_comparison_result_type, "NameRelation", po_NameRelation);
+
     PyModule_AddObject(mod, "NameComparisonResult",
                        (PyObject*) &name_comparison_result_type);
     
@@ -652,6 +591,53 @@ initModulePart_Name(PyObject* mod)
         return false;
     }
     Py_INCREF(&name_type);
+
+    // Add the constants to the module
+    addClassVariable(name_type, "MAX_WIRE", Py_BuildValue("I", 255U));
+    addClassVariable(name_type, "MAX_LABELS", Py_BuildValue("I", 128U));
+    addClassVariable(name_type, "MAX_LABELLEN", Py_BuildValue("I", 63U));
+    addClassVariable(name_type, "MAX_COMPRESS_POINTER", Py_BuildValue("I", 0x3fffU));
+    addClassVariable(name_type, "COMPRESS_POINTER_MARK8", Py_BuildValue("I", 0xc0U));
+    addClassVariable(name_type, "COMPRESS_POINTER_MARK16", Py_BuildValue("I", 0xc000U));
+
+    s_Name* root_name = PyObject_New(s_Name, &name_type);
+    root_name->name = new Name(".");
+    PyObject* po_ROOT_NAME = (PyObject*) root_name;
+    Py_INCREF(po_ROOT_NAME);
+    addClassVariable(name_type, "ROOT_NAME", po_ROOT_NAME);
+
+    // Add the exceptions to the module
+    po_EmptyLabel = PyErr_NewException("libdns_python.Name.EmptyLabel", NULL, NULL);
+    addClassVariable(name_type, "EmptyLabel", po_EmptyLabel);
+
+    po_TooLongName = PyErr_NewException("libdns_python.Name.TooLongName", NULL, NULL);
+    addClassVariable(name_type, "TooLongName", po_TooLongName);
+
+    po_TooLongLabel = PyErr_NewException("libdns_python.Name.TooLongLabel", NULL, NULL);
+    addClassVariable(name_type, "TooLongLabel", po_TooLongLabel);
+
+    po_BadLabelType = PyErr_NewException("libdns_python.Name.BadLabelType", NULL, NULL);
+    addClassVariable(name_type, "BadLabelType", po_BadLabelType);
+
+    po_BadEscape = PyErr_NewException("libdns_python.Name.BadEscape", NULL, NULL);
+    addClassVariable(name_type, "BadEscape", po_BadEscape);
+
+    po_IncompleteName = PyErr_NewException("libdns_python.Name.IncompleteName", NULL, NULL);
+    addClassVariable(name_type, "IncompleteName", po_IncompleteName);
+
+    po_InvalidBufferPosition = PyErr_NewException("libdns_python.Name.InvalidBufferPosition", NULL, NULL);
+    addClassVariable(name_type, "InvalidBufferPosition", po_InvalidBufferPosition);
+
+    /* TODO; this one is a message-specific one, move to message? */
+    po_DNSMessageFORMERR = PyErr_NewException("libdns_python.DNSMessageFORMERR", NULL, NULL);
+    Py_INCREF(po_DNSMessageFORMERR);
+    PyModule_AddObject(mod, "DNSMessageFORMERR", po_DNSMessageFORMERR);
+
+    /* TODO: this one is module-level, move to libdns_python.cc */
+    po_IscException = PyErr_NewException("libdns_python.IscException", NULL, NULL);
+    Py_INCREF(po_IncompleteName);
+    PyModule_AddObject(mod, "IscException", po_IscException);
+
     PyModule_AddObject(mod, "Name",
                        (PyObject*) &name_type);
     

+ 10 - 10
src/lib/dns/python/tests/name_python_test.py

@@ -55,8 +55,8 @@ class NameComparisonTest(unittest.TestCase):
         self.assertEqual(1, self.ncr15.get_common_labels())
 
     def test_get_relation(self):
-        self.assertEqual("COMMONANCESTOR", NameRelation[self.ncr12.get_relation()])
-        self.assertEqual("COMMONANCESTOR", NameRelation[self.ncr15.get_relation()])
+        self.assertEqual("COMMONANCESTOR", NameComparisonResult.NameRelation[self.ncr12.get_relation()])
+        self.assertEqual("COMMONANCESTOR", NameComparisonResult.NameRelation[self.ncr15.get_relation()])
 
 class NameTest(unittest.TestCase):
     def setUp(self):
@@ -67,19 +67,19 @@ class NameTest(unittest.TestCase):
         self.name5 = Name("*.example.com")
 
     def test_init(self):
-        self.assertRaises(EmptyLabel, Name, "example..com")
-        self.assertRaises(TooLongLabel, Name, "a"*64 + ".example.com")
-        self.assertRaises(BadLabelType, Name, "\[asdf.example.com")
-        self.assertRaises(BadEscape, Name, "\\999")
-        self.assertRaises(TooLongName, Name, "example."*32 + "com")
-        self.assertRaises(IncompleteName, Name, "\\")
+        self.assertRaises(Name.EmptyLabel, Name, "example..com")
+        self.assertRaises(Name.TooLongLabel, Name, "a"*64 + ".example.com")
+        self.assertRaises(Name.BadLabelType, Name, "\[asdf.example.com")
+        self.assertRaises(Name.BadEscape, Name, "\\999")
+        self.assertRaises(Name.TooLongName, Name, "example."*32 + "com")
+        self.assertRaises(Name.IncompleteName, Name, "\\")
         self.assertRaises(TypeError, Name, 1)
 
         b = bytearray()
         self.name1.to_wire(b)
         self.assertEqual(self.name1, Name(b))
         self.assertEqual(self.name1, Name(b, 0))
-        self.assertRaises(InvalidBufferPosition, Name, b, 100)
+        self.assertRaises(Name.InvalidBufferPosition, Name, b, 100)
         b = bytearray()
         b += b'\x07example'*32 + b'\x03com\x00'
         # no TooLong for from wire?
@@ -154,7 +154,7 @@ class NameTest(unittest.TestCase):
         self.assertEqual("example.com.", self.name1.concatenate(self.name2).to_text())
         self.assertEqual("example.com.example.com.", self.name1.concatenate(self.name1).to_text())
         self.assertRaises(TypeError, self.name1.concatenate, "wrong")
-        self.assertRaises(TooLongName, self.name1.concatenate, Name("example."*31))
+        self.assertRaises(Name.TooLongName, self.name1.concatenate, Name("example."*31))
         
 
     def test_downcase(self):