Example #1
1
//-------------------------------------------------------------------------------------
int Script::run_simpleString(const char* command, std::string* retBufferPtr)
{
	if(command == NULL)
	{
		ERROR_MSG("Script::Run_SimpleString: command is NULL!\n");
		return 0;
	}

	ScriptStdOutErrHook* pStdouterrHook = new ScriptStdOutErrHook();

	if(retBufferPtr != NULL)
	{
		DebugHelper::getSingleton().resetScriptMsgType();
		if(!pStdouterrHook->install()){												
			ERROR_MSG("Script::Run_SimpleString: pyStdouterrHook_->install() is failed!\n");
			SCRIPT_ERROR_CHECK();
			delete pStdouterrHook;
			return -1;
		}
			
		pStdouterrHook->setHookBuffer(retBufferPtr);
		//PyRun_SimpleString(command);

		PyObject *m, *d, *v;
		m = PyImport_AddModule("__main__");
		if (m == NULL)
		{
			SCRIPT_ERROR_CHECK();
			pStdouterrHook->uninstall();
			delete pStdouterrHook;
			return -1;
		}

		d = PyModule_GetDict(m);

		v = PyRun_String(command, Py_single_input, d, d);
		if (v == NULL) 
		{
			PyErr_Print();
			pStdouterrHook->uninstall();
			delete pStdouterrHook;
			return -1;
		}

		Py_DECREF(v);
		SCRIPT_ERROR_CHECK();
		
		pStdouterrHook->uninstall();
		delete pStdouterrHook;
		return 0;
	}

	PyRun_SimpleString(command);

	SCRIPT_ERROR_CHECK();
	delete pStdouterrHook;
	return 0;
}
Example #2
0
//-------------------------------------------------------------------------------------
void PyProfile::addToStream(std::string profile, MemoryStream* s)
{
	PyProfile::PROFILES::iterator iter = profiles_.find(profile);
	if(iter == profiles_.end())
	{
		ERROR_MSG(fmt::format("PyProfile::getstats: profile({}) is not exists!\n", profile));
		return;
	}

	ScriptStdOutErrHook* pScriptStdOutErrHook = new ScriptStdOutErrHook();
	if(!pScriptStdOutErrHook->install())
	{												
		ERROR_MSG("PyProfile::addToStream: pyStdouterrHook_->install() is failed!\n");
		delete pScriptStdOutErrHook;
		SCRIPT_ERROR_CHECK();
		return;
	}

	std::string retBufferPtr;
	pScriptStdOutErrHook->setHookBuffer(&retBufferPtr);
	pScriptStdOutErrHook->setPrint(false);

	PyObject* pyRet = PyObject_CallMethod(iter->second.get(), const_cast<char*>("print_stats"),
		const_cast<char*>("s"), const_cast<char*>("time"));
	
	pScriptStdOutErrHook->setPrint(true);
	pScriptStdOutErrHook->uninstall();

	delete pScriptStdOutErrHook;
	SCRIPT_ERROR_CHECK();

	if(!pyRet)
	{
		return;
	}
	
	(*s) << retBufferPtr;

	// DEBUG_MSG(fmt::format("PyProfile::addToStream:{}", retBufferPtr));

	Py_DECREF(pyRet);
}