Browse Source

Added Python3 support

* Added Python 3 support
* fixed a small bug (#76)
Chachay 8 years ago
parent
commit
d3c9763cad
2 changed files with 22 additions and 3 deletions
  1. 4 0
      .gitignore
  2. 18 3
      matplotlibcpp.h

+ 4 - 0
.gitignore

@@ -26,3 +26,7 @@
 *.exe
 *.out
 *.app
+
+# Logfiles
+*.tlog
+*.log

+ 18 - 3
matplotlibcpp.h

@@ -10,7 +10,16 @@
 #include <functional>
 #endif
 
+#ifdef PY_INCLUDE
+#include <Python.h>
+#else
 #include <python2.7/Python.h>
+#endif
+
+#if PY_MAJOR_VERSION >= 3
+#define PyString_FromString PyUnicode_FromString
+#endif
+
 
 namespace matplotlibcpp {
 
@@ -48,8 +57,14 @@ namespace matplotlibcpp {
 
 			private:
 			_interpreter() {
-				char name[] = "plotting"; // silence compiler warning about const strings
-				Py_SetProgramName(name);  // optional but recommended
+                
+                // optional but recommended
+#if PY_MAJOR_VERSION >= 3
+                wchar_t name[] = L"plotting";
+#else
+                char name[] = "plotting";
+#endif
+				Py_SetProgramName(name);
 				Py_Initialize();
 
 				PyObject* pyplotname = PyString_FromString("matplotlib.pyplot");
@@ -62,7 +77,7 @@ namespace matplotlibcpp {
 
 				PyObject* pylabmod = PyImport_Import(pylabname);
 				Py_DECREF(pylabname);
-				if(!pymod) { throw std::runtime_error("Error loading module pylab!"); }
+				if(!pylabmod) { throw std::runtime_error("Error loading module pylab!"); }
 
 				s_python_function_show = PyObject_GetAttrString(pymod, "show");
 				s_python_function_figure = PyObject_GetAttrString(pymod, "figure");