Răsfoiți Sursa

PythonEntry OK

tanghai 14 ani în urmă
părinte
comite
ab04da6630

+ 5 - 20
Src/Egametang/Python/CMakeLists.txt

@@ -2,9 +2,9 @@ set(PythonSrc
 	PythonInit.cc 
 	PythonEntry.cc
 )
-add_library(Python ${PythonSrc})
+add_library(PythonEntry ${PythonSrc})
 
-include_directories(/usr/include/python2.6)
+include_directories(/usr/include/python2.5)
 
 add_executable(PythonInitTest PythonInitTest.cc)
 add_executable(PythonEntryTest PythonEntryTest.cc)
@@ -16,27 +16,12 @@ set(Excutes
 
 foreach(Excute ${Excutes})
 	target_link_libraries(${Excute}
+		PythonEntry
 		boost_python
-		python2.6
+		python2.5
 		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
-#)
+endforeach()

+ 12 - 18
Src/Egametang/Python/PythonEntry.cc

@@ -1,6 +1,7 @@
 #include <boost/foreach.hpp>
 #include <boost/format.hpp>
 #include <boost/python.hpp>
+#include <glog/logging.h>
 #include "Python/PythonEntry.h"
 
 namespace Egametang {
@@ -8,59 +9,52 @@ namespace Egametang {
 PythonEntry::PythonEntry():
 		python_init_()
 {
-	main_ns_ = import("__main__").attr("__dict__");
+	main_ns_ = boost::python::import("__main__").attr("__dict__");
 }
 
-void PythonEntry::ImportPath(std::string& path)
+void PythonEntry::ImportPath(std::string path)
 {
 	python_paths_.insert(path);
 }
 
-void PythonEntry::ImportModule(std::string& module)
+void PythonEntry::ImportModule(std::string module)
 {
 	python_modules_.insert(module);
 }
 
-template <typename T>
-void PythonEntry::RegisterObjectPtr(std::string& name, T object_ptr)
-{
-	main_ns_[name.c_str()] = object_ptr;
-}
-
 bool PythonEntry::GetExecString(const std::string& main_fun, std::string& exec_string)
 {
 	exec_string = "import sys\n";
-	boost::format format;
 	if (python_paths_.size() == 0)
 	{
-		LOG(WARNNING) << "no python path";
+		LOG(WARNING) << "no python path";
 		return false;
 	}
-	foreach(std::string& path, python_paths_)
+	foreach(std::string path, python_paths_)
 	{
-		exec_string += format("sys.path.append('%1%')\n") % path;
+		exec_string += boost::str(boost::format("sys.path.append('%1%')\n") % path);
 	}
 
 	if (python_modules_.size() == 0)
 	{
-		LOG(WARNNING) << "no python module";
+		LOG(WARNING) << "no python module";
 		return false;
 	}
-	foreach(std::string& module, python_modules_)
+	foreach(std::string module, python_modules_)
 	{
-		exec_string += format("import %1%\n") % module;
+		exec_string += boost::str(boost::format("import %1%\n") % module);
 	}
 	exec_string += main_fun;
 
 	return true;
 }
 
-void PythonEntry::Execute(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!";
+		LOG(WARNING) << "no python exec string!";
 		return;
 	}
 

+ 10 - 5
Src/Egametang/Python/PythonEntry.h

@@ -2,6 +2,8 @@
 #define PYTHON_PYTHON_ENTRY_H
 
 #include <boost/noncopyable.hpp>
+#include <boost/unordered_set.hpp>
+#include <boost/shared_ptr.hpp>
 #include "Base/Marcos.h"
 #include "Python/PythonInit.h"
 
@@ -19,19 +21,22 @@ private:
 	boost::unordered_set<std::string> python_modules_;
 
 private:
-	bool PythonEntry::GetExecString(const std::string& main_fun, std::string& exec_string);
+	bool GetExecString(const std::string& main_fun, std::string& exec_string);
 
 public:
 	PythonEntry();
 
-	void ImportPath(std::string& path);
+	void ImportPath(std::string path);
 
-	void ImportModule(std::string& module);
+	void ImportModule(std::string module);
 
 	template <typename T>
-	void RegisterObjectPtr(std::string& name, T object_ptr);
+	void RegisterObjectPtr(std::string name, boost::shared_ptr<T> object_ptr)
+	{
+		main_ns_[name.c_str()] = object_ptr;
+	}
 
-	void Execute(std::string& main_fun);
+	void Execute(std::string main_fun);
 };
 
 } // namespace Egametang

+ 11 - 18
Src/Egametang/Python/PythonEntryTest.cc

@@ -1,6 +1,7 @@
 #include <gtest/gtest.h>
 #include <gflags/gflags.h>
 #include <glog/logging.h>
+#include <boost/python.hpp>
 #include "Python/PythonEntry.h"
 
 namespace Egametang {
@@ -61,28 +62,20 @@ BOOST_PYTHON_MODULE(PersonTest)
 
 TEST_F(PythonEntryTest, EnterPythonScript)
 {
-	try
-	{
-		initPersonTest();
-		python_entry_.ImportPath("../../../Src/Egametang/Python/");
-		python_entry_.ImportModule("PythonEntryTest");
+	initPersonTest();
+	python_entry_.ImportPath("../../../Src/Egametang/Python/");
+	python_entry_.ImportModule("PythonEntryTest");
 
-		PersonTestPtr person(new PersonTest);
-		python_entry_.RegisterObjectPtr("person", person);
+	PersonTestPtr person(new PersonTest);
+	python_entry_.RegisterObjectPtr("person", person);
 
-		ASSERT_EQ(0, person->Guid());
+	ASSERT_EQ(0, person->Guid());
 
-		// 进到python脚本层设置person的值为2
-		python_entry_.Execute("PythonEntryTest.fun(person)");
+	// 进到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;
-	}
+	ASSERT_EQ(2, person->Guid());
+	ASSERT_EQ(std::string("tanghai"), person->Name());
 }
 
 } // namespace Egametang

+ 1 - 0
Src/Egametang/Python/PythonInitTest.cc

@@ -1,3 +1,4 @@
+#include <boost/python.hpp>
 #include <gtest/gtest.h>
 #include <gflags/gflags.h>
 #include <glog/logging.h>