Parcourir la source

[trac915] added a new template: create@CPPCLASS@Object() for easier and
safer creation of python objects in C++- binding.

JINMEI Tatuya il y a 14 ans
Parent
commit
3e8ef1595d

+ 8 - 0
src/lib/util/python/wrapper_template.cc

@@ -288,6 +288,14 @@ initModulePart_@CPPCLASS@(PyObject* mod) {
 
     return (true);
 }
+
+PyObject*
+create@CPPCLASS@Object(const @CPPCLASS@& source) {
+    @CPPCLASS@Container container =
+        PyObject_New(s_@CPPCLASS@, &@cppclass@_type);
+    container.set(new @CPPCLASS@(source));
+    return (container.release());
+}
 } // namespace python
 } // namespace @MODULE@
 } // namespace isc

+ 15 - 0
src/lib/util/python/wrapper_template.h

@@ -34,6 +34,21 @@ extern PyTypeObject @cppclass@_type;
 
 bool initModulePart_@CPPCLASS@(PyObject* mod);
 
+// Note: this utility function works only when @CPPCLASS@ is a copy
+// constructable.
+// And, it would only be useful when python binding needs to create this
+// object frequently.  Otherwise, it would (or should) probably better to
+// remove the declaration and definition of this function.
+//
+/// This is A simple shortcut to create a python @CPPCLASS@ object (in the
+/// form of a pointer to PyObject) with minimal exception safety.
+/// On success, it returns a valid pointer to PyObject with a reference
+/// counter of 1; if something goes wrong it throws an exception (it never
+/// returns a NULL pointer).
+/// This function is expected to be called with in a try block
+/// followed by necessary setup for python exception.
+PyObject* create@CPPCLASS@Object(const @CPPCLASS@& source);
+
 } // namespace python
 } // namespace @MODULE@
 } // namespace isc