Parcourir la source

实现PythonEntry

Tang Hai il y a 14 ans
Parent
commit
13a0b2da43

+ 2 - 0
Src/Egametang/Base/Marcos.h

@@ -3,4 +3,6 @@
 
 #define foreach BOOST_FOREACH
 
+#define private public
+
 #endif // BASE_MARCOS_H

+ 37 - 8
Src/Egametang/Python/CMakeLists.txt

@@ -1,13 +1,42 @@
-add_executable(PythonInitTest PythonInitTest.cc)
+set(PythonSrc 
+	PythonInit.cc 
+	PythonEntry.cc
+)
+add_library(Python ${PythonSrc})
 
 include_directories(/usr/include/python2.6)
 
-target_link_libraries(PythonInitTest
-	boost_python
-	python2.6
-	glog
-	gtest
-	gmock
+add_executable(PythonInitTest PythonInitTest.cc)
+add_executable(PythonEntryTest PythonEntryTest.cc)
+
+set(Excutes 
+	PythonInitTest 
+	PythonEntryTest
 )
 
-add_test(PythonInitTest PythonInitTest)
+foreach(Excute ${Excutes})
+	target_link_libraries(${Excute}
+		boost_python
+		python2.6
+		glog
+		gtest
+		gmock
+	)
+	add_test(${Excute} ${Excute})
+endforeach()
+
+#target_link_libraries(PythonInitTest
+#	boost_python
+#	python2.6
+#	glog
+#	gtest
+#	gmock
+#)
+#
+#target_link_libraries(PythonEntryTest
+#	boost_python
+#	python2.6
+#	glog
+#	gtest
+#	gmock
+#)

+ 6 - 5
Src/Egametang/Python/PythonEntry.cc

@@ -1,6 +1,6 @@
 #include <boost/foreach.hpp>
 #include <boost/format.hpp>
-#include "base/Marcos.h"
+#include <boost/python.hpp>
 #include "Python/PythonEntry.h"
 
 namespace Egametang {
@@ -55,11 +55,12 @@ bool PythonEntry::GetExecString(const std::string& main_fun, std::string& exec_s
 	return true;
 }
 
-void PythonEntry::Exec(std::string& main_fun)
+void PythonEntry::Execute(std::string& main_fun)
 {
 	std::string exec_string;
 	if (!GetExecString(main_fun, exec_string))
 	{
+		LOG(WARNNING) << "no python exec string!";
 		return;
 	}
 
@@ -67,10 +68,10 @@ void PythonEntry::Exec(std::string& main_fun)
 	{
 		boost::python::exec(exec_string.c_str(), main_ns_);
 	}
-	catch
+	catch (...)
 	{
-		LOG(ERROR) << "run python exec error";
-		python_init.PrintError();
+		LOG(ERROR) << "python execute error";
+		python_init_.PrintError();
 	}
 }
 

+ 3 - 3
Src/Egametang/Python/PythonEntry.h

@@ -2,7 +2,7 @@
 #define PYTHON_PYTHON_ENTRY_H
 
 #include <boost/noncopyable.hpp>
-#include <boost/python.hpp>
+#include "Base/Marcos.h"
 #include "Python/PythonInit.h"
 
 namespace Egametang {
@@ -18,7 +18,7 @@ private:
 
 	boost::unordered_set<std::string> python_modules_;
 
-public:  // private
+private:
 	bool PythonEntry::GetExecString(const std::string& main_fun, std::string& exec_string);
 
 public:
@@ -31,7 +31,7 @@ public:
 	template <typename T>
 	void RegisterObjectPtr(std::string& name, T object_ptr);
 
-	void Exec();
+	void Execute(std::string& main_fun);
 };
 
 } // namespace Egametang

+ 97 - 0
Src/Egametang/Python/PythonEntryTest.cc

@@ -0,0 +1,97 @@
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+#include <glog/logging.h>
+#include "Python/PythonEntry.h"
+
+namespace Egametang {
+
+class PythonEntryTest: public testing::Test
+{
+protected:
+	PythonEntry python_entry_;
+
+public:
+	PythonEntryTest(): python_entry_()
+	{}
+};
+
+class PersonTest
+{
+private:
+	int guid_;
+	std::string name_;
+
+public:
+	PersonTest(): guid_(0)
+	{
+	}
+	void SetGuid(int guid)
+	{
+		guid_ = guid;
+	}
+
+	int Guid() const
+	{
+		return guid_;
+	}
+
+	void SetName(const std::string& name)
+	{
+		name_ = name;
+	}
+
+	std::string Name() const
+	{
+		return name_;
+	}
+};
+
+typedef boost::shared_ptr<PersonTest> PersonTestPtr;
+
+BOOST_PYTHON_MODULE(PersonTest)
+{
+	boost::python::class_<PersonTest>("Person")
+		.def("SetGuid", &PersonTest::SetGuid)
+		.def("Guid", &PersonTest::Guid)
+		.def("SetName", &PersonTest::SetName)
+		.def("Name", &PersonTest::Name)
+	;
+	boost::python::register_ptr_to_python<PersonTestPtr>();
+}
+
+TEST_F(PythonEntryTest, EnterPythonScript)
+{
+	try
+	{
+		initPersonTest();
+		python_entry_.ImportPath("../../../Src/Egametang/Python/");
+		python_entry_.ImportModule("PythonEntryTest");
+
+		PersonTestPtr person(new PersonTest);
+		python_entry_.RegisterObjectPtr("person", person);
+
+		ASSERT_EQ(0, person->Guid());
+
+		// 进到python脚本层设置person的值为2
+		python_entry_.Execute("PythonEntryTest.fun(person)");
+
+		ASSERT_EQ(2, person->Guid());
+		ASSERT_EQ(std::string("tanghai"), person->Name());
+	}
+	catch (boost::python::error_already_set& err)
+	{
+		python_init.PrintError();
+		throw err;
+	}
+}
+
+} // namespace Egametang
+
+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();
+}

+ 0 - 0
Src/Egametang/Python/PythonInitTest.py → Src/Egametang/Python/PythonEntryTest.py


+ 31 - 0
Src/Egametang/Python/PythonInit.cc

@@ -0,0 +1,31 @@
+#include <boost/python.hpp>
+#include "Python/PythonInit.h"
+
+namespace Egametang {
+
+PythonInit::PythonInit()
+{
+	Py_InitializeEx(0);
+}
+
+PythonInit::~PythonInit()
+{
+	Py_Finalize();
+}
+
+bool PythonInit::IsInitialized()
+{
+	return Py_IsInitialized();
+}
+
+const char* PythonInit::Version()
+{
+	return Py_GetVersion();
+}
+
+void PythonInit::PrintError()
+{
+	PyErr_Print();
+}
+
+} // namespace Egametang

+ 9 - 25
Src/Egametang/Python/PythonInit.h

@@ -2,37 +2,21 @@
 #define PYTHON_PYTHON_INIT_H
 
 #include <boost/noncopyable.hpp>
-#include <boost/python.hpp>
 
 namespace Egametang {
 
 class PythonInit: private boost::noncopyable
 {
 public:
-	PythonInit()
-	{
-		Py_InitializeEx(0);
-	}
-
-	~PythonInit()
-	{
-		Py_Finalize();
-	}
-
-	bool IsInitialized()
-	{
-		return Py_IsInitialized();
-	}
-
-	const char* Version()
-	{
-		return Py_GetVersion();
-	}
-
-	void PrintError()
-	{
-		PyErr_Print();
-	}
+	PythonInit();
+
+	~PythonInit();
+
+	bool IsInitialized();
+
+	const char* Version();
+
+	void PrintError();
 };
 
 } // namespace Egametang

+ 2 - 73
Src/Egametang/Python/PythonInitTest.cc

@@ -10,11 +10,11 @@ using namespace boost::python;
 class PythonInitTest: public testing::Test
 {
 public:
-	PythonInitTest(): python_init()
+	PythonInitTest(): python_init_()
 	{}
 
 protected:
-	PythonInit python_init;
+	PythonInit python_init_;
 };
 
 TEST_F(PythonInitTest, Int)
@@ -57,77 +57,6 @@ TEST_F(PythonInitTest, Dict)
 	ASSERT_EQ(2, boost::python::len(dict));
 }
 
-class PersonTest
-{
-private:
-	int guid_;
-	std::string name_;
-
-public:
-	PersonTest(): guid_(0)
-	{
-	}
-	void SetGuid(int guid)
-	{
-		guid_ = guid;
-	}
-
-	int Guid() const
-	{
-		return guid_;
-	}
-
-	void SetName(const std::string& name)
-	{
-		name_ = name;
-	}
-
-	std::string Name() const
-	{
-		return name_;
-	}
-};
-
-typedef boost::shared_ptr<PersonTest> PersonTestPtr;
-
-BOOST_PYTHON_MODULE(PersonTest)
-{
-	boost::python::class_<PersonTest>("Person")
-		.def("SetGuid", &PersonTest::SetGuid)
-		.def("Guid", &PersonTest::Guid)
-		.def("SetName", &PersonTest::SetName)
-		.def("Name", &PersonTest::Name)
-	;
-	boost::python::register_ptr_to_python<PersonTestPtr>();
-}
-
-TEST_F(PythonInitTest, EnterPythonScript)
-{
-	try
-	{
-		initPersonTest();
-		boost::python::object main_module = boost::python::import("__main__");
-		boost::python::object main_namespace = main_module.attr("__dict__");
-		PersonTestPtr person(new PersonTest);
-		main_namespace["person"] = person;
-		std::string str = "import sys\n"
-				"sys.path.append('../../../Src/Egametang/Python/')\n"
-				"import PythonInitTest\n"
-				"PythonInitTest.fun(person)\n";
-		ASSERT_EQ(0, person->Guid());
-
-		// 进到python脚本层设置person的值为2
-		boost::python::exec(str.c_str(), main_namespace);
-		ASSERT_EQ(2, person->Guid());
-		ASSERT_EQ(std::string("tanghai"), person->Name());
-	}
-	catch (boost::python::error_already_set& err)
-	{
-		python_init.PrintError();
-		throw err;
-	}
-}
-
 } // namespace Egametang
 
 int main(int argc, char* argv[])