Quellcode durchsuchen

增加boost python支持

Ubuntu vor 15 Jahren
Ursprung
Commit
c939bd2b9b
5 geänderte Dateien mit 92 neuen und 6 gelöschten Zeilen
  1. 5 5
      SConstruct
  2. 30 0
      Src/Python/PythonInit.h
  3. 41 0
      Src/Python/PythonInitTest.cc
  4. 14 0
      Src/Python/SConscript
  5. 2 1
      Src/SConscript

+ 5 - 5
SConstruct

@@ -33,15 +33,15 @@ env['NTEST'] = GetOption('ntest')
 
 
 env.Append(CPPPATH=Dir(env['MODE']).abspath)
 env.Append(CPPPATH=Dir(env['MODE']).abspath)
 
 
-def Test(env, target, source):
-	if env['NTEST']:
+def Test(test_env, target, source):
+	if test_env['NTEST']:
 		return
 		return
-	test_env = env.Clone()
-	test_env.Append(LIBS=[
+	local_env = test_env.Clone()
+	local_env.Append(LIBS=[
 		'gtest',
 		'gtest',
 		'gmock',
 		'gmock',
 	])
 	])
-	test_target = test_env.Program(target, source)
+	test_target = local_env.Program(target, source)
 	return test_target
 	return test_target
 
 
 env.AddMethod(Test, 'Test')
 env.AddMethod(Test, 'Test')

+ 30 - 0
Src/Python/PythonInit.h

@@ -0,0 +1,30 @@
+#ifndef PYTHON_PYTHON_INIT_H
+#define PYTHON_PYTHON_INIT_H
+
+#include <boost/noncopyable.hpp>
+#include <boost/python.hpp>
+
+namespace Hainan {
+
+class PythonInit: private boost::noncopyable
+{
+public:
+	PythonInit()
+	{
+		Py_InitializeEx(0);
+	}
+
+	~PythonInit()
+	{
+		Py_Finalize();
+	}
+
+	bool IsInitialized()
+	{
+		return Py_IsInitialized();
+	}
+};
+
+} // namespace Hainan
+
+#endif // PYTHON_PYTHON_INIT_H

+ 41 - 0
Src/Python/PythonInitTest.cc

@@ -0,0 +1,41 @@
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+#include <glog/logging.h>
+#include "Python/PythonInit.h"
+
+namespace Hainan {
+
+class PythonInitTest: public testing::Test
+{
+public:
+	PythonInitTest(): python_init()
+	{}
+
+protected:
+	PythonInit python_init;
+};
+
+TEST_F(PythonInitTest, Int)
+{
+	boost::python::object i(10);
+	i = 10 * i;
+	ASSERT_EQ(100, boost::python::extract<int>(i));
+}
+
+TEST_F(PythonInitTest, String)
+{
+	boost::python::object i("ab");
+	std::string str = boost::python::extract<std::string>(i * 5);
+	ASSERT_EQ("ababababab", str);
+}
+
+} // namespace Hainan
+
+int main(int argc, char* argv[])
+{
+	FLAGS_logtostderr = true;
+	testing::InitGoogleTest(&argc, argv);
+	google::ParseCommandLineFlags(&argc, &argv, true);
+	google::InitGoogleLogging(argv[0]);
+	return RUN_ALL_TESTS();
+}

+ 14 - 0
Src/Python/SConscript

@@ -0,0 +1,14 @@
+Import('env')
+
+python_env = env.Clone()
+
+python_env.Append(LIBS=[
+	"boost_python",
+	"python2.6",
+])
+
+python_env.Append(CPPPATH=[
+	"/usr/include/python2.6"
+])
+
+python_env.Test("PythonInitTest", "PythonInitTest.cc")

+ 2 - 1
Src/SConscript

@@ -1,7 +1,8 @@
 Import('env')
 Import('env')
 
 
 subdirs = [
 subdirs = [
-	'Thread',
+	"Thread",
+	"Python",
 ]
 ]
 
 
 for subdir in subdirs:
 for subdir in subdirs: