[cig-commits] r12966 - in cs/cigma/trunk: . pysrc pysrc/tests src

luis at geodynamics.org luis at geodynamics.org
Wed Sep 24 06:22:08 PDT 2008


Author: luis
Date: 2008-09-24 06:22:08 -0700 (Wed, 24 Sep 2008)
New Revision: 12966

Added:
   cs/cigma/trunk/pysrc/Py_DataPath.cpp
   cs/cigma/trunk/pysrc/_cigma_module.cpp
Removed:
   cs/cigma/trunk/pysrc/pycigma.cpp
   cs/cigma/trunk/pysrc/pyhello.cpp
   cs/cigma/trunk/pysrc/tests/test_hello.py
Modified:
   cs/cigma/trunk/Makefile.am
   cs/cigma/trunk/pysrc/setup.py
   cs/cigma/trunk/src/DataPath.cpp
Log:
Build the C++ Python bindings for each class separately

In order to avoid any speed/memory problems with C++ templates,
we split up the module into various pieces and compile them
separately.

In this changeset, we also add a __repr__ method, add docstrings,
and removed the hello module.

Modified: cs/cigma/trunk/Makefile.am
===================================================================
--- cs/cigma/trunk/Makefile.am	2008-09-24 13:22:06 UTC (rev 12965)
+++ cs/cigma/trunk/Makefile.am	2008-09-24 13:22:08 UTC (rev 12966)
@@ -178,8 +178,10 @@
 _cigma_so_LDADD += libcigma.a
 
 _cigma_so_SOURCES = \
-	pysrc/pycigma.cpp
+	pysrc/Py_DataPath.cpp \
+	pysrc/_cigma_module.cpp
 
+
 EXTRA_DIST += pysrc/setup.py
 
 

Added: cs/cigma/trunk/pysrc/Py_DataPath.cpp
===================================================================
--- cs/cigma/trunk/pysrc/Py_DataPath.cpp	                        (rev 0)
+++ cs/cigma/trunk/pysrc/Py_DataPath.cpp	2008-09-24 13:22:08 UTC (rev 12966)
@@ -0,0 +1,43 @@
+//#include <cigma/DataPath.h> // XXX: create include directory for core cigma headers?
+
+#include "DataPath.h"
+#include <boost/python.hpp>
+#include <boost/cstdint.hpp>
+
+
+const char *DataPath_doc[] = {
+"An object encapsulating a dataset path.\n",
+"Included in the core bindings as a first example",
+
+"Constructs a path given a colon separated filename:datapath string"
+};
+
+
+template<class DataPath>
+boost::python::str py_repr_DataPath(const DataPath& dp)
+{
+    using namespace boost::python;
+    return str("<" + str(dp) + ">");
+}
+
+void Py_DataPath()
+{
+    using namespace boost::python;
+
+    class_<DataPath>("DataPath", DataPath_doc[0], no_init)
+        .def(init<std::string>(DataPath_doc[1]))
+        .def("exists", &DataPath::exists)
+        .def("empty", &DataPath::empty)
+        .def("extension", &DataPath::extension)
+        .add_property("filename", &DataPath::filename, &DataPath::set_filename)
+        .add_property("location", &DataPath::location, &DataPath::set_location)
+        .def("__repr__", &py_repr_DataPath<DataPath>)
+        .def(self_ns::str(self))    // __str__
+    ;
+}
+
+void export_DataPath()
+{
+    Py_DataPath();
+}
+

Added: cs/cigma/trunk/pysrc/_cigma_module.cpp
===================================================================
--- cs/cigma/trunk/pysrc/_cigma_module.cpp	                        (rev 0)
+++ cs/cigma/trunk/pysrc/_cigma_module.cpp	2008-09-24 13:22:08 UTC (rev 12966)
@@ -0,0 +1,12 @@
+#include <boost/python.hpp>
+#include <boost/cstdint.hpp>
+
+extern void export_DataPath();
+
+using namespace boost::python;
+
+BOOST_PYTHON_MODULE(_cigma)
+{
+    export_DataPath();
+}
+

Deleted: cs/cigma/trunk/pysrc/pycigma.cpp
===================================================================
--- cs/cigma/trunk/pysrc/pycigma.cpp	2008-09-24 13:22:06 UTC (rev 12965)
+++ cs/cigma/trunk/pysrc/pycigma.cpp	2008-09-24 13:22:08 UTC (rev 12966)
@@ -1,16 +0,0 @@
-#include "DataPath.h"
-#include <boost/python.hpp>
-using namespace boost::python;
-
-BOOST_PYTHON_MODULE(_cigma)
-{
-    class_<DataPath>("DataPath", no_init)
-        .def(init<std::string>())
-        .def(self_ns::str(self)) // __str__
-        .def("exists", &DataPath::exists)
-        .def("empty",  &DataPath::empty)
-        .def("extension", &DataPath::extension)
-        .add_property("filename", &DataPath::filename, &DataPath::set_filename)
-        .add_property("location", &DataPath::location, &DataPath::set_location)
-        ;
-}

Deleted: cs/cigma/trunk/pysrc/pyhello.cpp
===================================================================
--- cs/cigma/trunk/pysrc/pyhello.cpp	2008-09-24 13:22:06 UTC (rev 12965)
+++ cs/cigma/trunk/pysrc/pyhello.cpp	2008-09-24 13:22:08 UTC (rev 12966)
@@ -1,19 +0,0 @@
-#include <string>
-#include <boost/python.hpp>
-
-struct World
-{
-    void set(std::string msg) { this->msg = msg; }
-    std::string greet() { return msg; }
-    std::string msg;
-};
-
-using namespace boost::python;
-
-BOOST_PYTHON_MODULE(hello)
-{
-    class_<World>("World")
-        .def("greet", &World::greet)
-        .def("set", &World::set)
-    ;
-}

Modified: cs/cigma/trunk/pysrc/setup.py
===================================================================
--- cs/cigma/trunk/pysrc/setup.py	2008-09-24 13:22:06 UTC (rev 12965)
+++ cs/cigma/trunk/pysrc/setup.py	2008-09-24 13:22:08 UTC (rev 12966)
@@ -9,6 +9,8 @@
 #libs = os.environ["PYLIBS"].split()
 libs = ['boost_filesystem', 'boost_python']
 
+sources = ['_cigma_module.cpp', 'Py_DataPath.cpp']
+
 setup(name          = "_cigma",
       version       = "1.0",
       description   = "CIG Model Analysis Framework",
@@ -16,7 +18,7 @@
       author_email  = "luis at geodynamics.org",
       url           = "http://www.geodynamics.org/",
       ext_modules   = [
-        Extension("_cigma", ['pysrc/pycigma.cpp'],
+        Extension("_cigma", [('pysrc/%s' % f) for f in sources],
             define_macros=defines,
             libraries=libs,
             extra_objects=['libcigma.a'])

Deleted: cs/cigma/trunk/pysrc/tests/test_hello.py
===================================================================
--- cs/cigma/trunk/pysrc/tests/test_hello.py	2008-09-24 13:22:06 UTC (rev 12965)
+++ cs/cigma/trunk/pysrc/tests/test_hello.py	2008-09-24 13:22:08 UTC (rev 12966)
@@ -1,6 +0,0 @@
-#!/usr/bin/env python
-import hello
-planet = hello.World()
-planet.set('howdy')
-print planet.greet()
-

Modified: cs/cigma/trunk/src/DataPath.cpp
===================================================================
--- cs/cigma/trunk/src/DataPath.cpp	2008-09-24 13:22:06 UTC (rev 12965)
+++ cs/cigma/trunk/src/DataPath.cpp	2008-09-24 13:22:08 UTC (rev 12966)
@@ -105,7 +105,7 @@
 
 std::ostream &operator<<(std::ostream &os, const DataPath &dp)
 {
-    os << "<" << dp.filename() << ":" << dp.location() << ">";
+    os << dp.filename() << ":" << dp.location();
     return os;
 }
 



More information about the cig-commits mailing list