⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplepy.h

📁 Python是一种简单易学
💻 H
字号:

#ifndef _SIMPLEPY_H_
#define _SIMPLEPY_H_
// simplepy.h v1.0
// Purpose: facilities for Embedded Python.
// by hujinshan @2005年9月2日9:13:02

#include <string>
using std::string;
#include <Python.h>

//--------------------------------------------------------------------
// Purpose: ease the job to embed Python into C++ applications
// by hujinshan @2005年9月2日9:13:18
//--------------------------------------------------------------------
class CSimplepy // : private noncopyable
{
public:
	///constructor
	CSimplepy()
	{
		Py_Initialize();

		pstr=NULL, pmod=NULL, pdict=NULL;
		pfunc=NULL, pargs=NULL;
	}
    ///destructor
	virtual ~CSimplepy()	
	{	
		Py_Finalize();
	}
	///import the user module
	bool ImportModule(const char* mod_name)
	{
		try{
			pmod  = PyImport_ImportModule(const_cast<char*>(mod_name));
			if(pmod==NULL)
				return false;
			pdict = PyModule_GetDict(pmod);
		}
		catch(...)
		{
			return false;
		}
		if(pmod!=NULL && pdict!=NULL)
			return true;
		else
			return false;
	}

	///Executes the Python source code from command in the __main__ module. 
	///If __main__ does not already exist, it is created. 
	///Returns 0 on success or -1 if an exception was raised. 
	///If there was an error, there is no way to get the exception information.
	int Run_SimpleString(const char* str)
	{
		return PyRun_SimpleString(const_cast<char*>(str) );
	}

	///PyRun_String("message", Py_eval_input, pdict, pdict);
	///Execute Python source code from str in the context specified by the dictionaries globals.
	///The parameter start specifies the start token that should be used to parse the source code. 
	///Returns the result of executing the code as a Python object, or NULL if an exception was raised.
	string Run_String(const char* str)
	{
		char *cstr;
		pstr  = PyRun_String(str, Py_eval_input, pdict, pdict);
		if(pstr==NULL)
			throw ("when Run_String, there is an exception was raised by Python environment.");
		PyArg_Parse(pstr, "s", &cstr);
		return string(cstr);
	}

	///support olny one parameter for python function, I think it's just enough.
	string CallObject(const char* func_name, const char* parameter)
	{
		pfunc=NULL;
		pfunc = PyObject_GetAttrString(pmod, const_cast<char*>(func_name));
		if(pfunc==NULL)
			throw (string("do not found in Python module for: ")+func_name).c_str();

		char* cstr;
		pargs = Py_BuildValue("(s)", const_cast<char*>(parameter));
		pstr  = PyEval_CallObject(pfunc, pargs);
		if(pstr==NULL)
			throw ("when PyEval_CallObject, there is an exception was raised by Python environment");
		PyArg_Parse(pstr, "s", &cstr);		
		return string(cstr);
	}
	//PyObject *args;
	//args = Py_BuildValue("(si)", label, count);   /* make arg-list */
    //pres = PyEval_CallObject(Handler, args);      

protected:
	PyObject *pstr, *pmod, *pdict;
	PyObject *pfunc, *pargs;

};



#endif // _SIMPLEPY_H_

// end of file 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -