本文整理汇总了C++中PyRun_SimpleString函数的典型用法代码示例。如果您正苦于以下问题:C++ PyRun_SimpleString函数的具体用法?C++ PyRun_SimpleString怎么用?C++ PyRun_SimpleString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyRun_SimpleString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: check_init
static void check_init() {
if ((initialized == 0) && ! Py_IsInitialized()) {
Py_Initialize();
#ifdef _WIN32
PyRun_SimpleString(pCleanPath);
#endif
initialized = 1;
}
}
示例2: registerStdout
// Run a thread to manage a console-based interpreter
void ConsoleClass::pyconsole(){
std::string input, subinput;
bool outputting;
// Register python output to std streams
registerStdout(&std::cout);
registerStderr(&std::cerr);
while (true)
{
outputting = true;
// This could be replaced with pythons sys.ps1
std::cout << ">>> ";
// Reset stdin to deal with ctrl-c
// This needs actual handling later
std::cin.clear();
std::getline(std::cin, input);
// Handling for structures (if, while, def, etc)
if (input[input.length()-1] == ':')
{
outputting = false;
while (true)
{
// This could be replaced with pythons sys.ps2
std::cout << "... ";
std::getline(std::cin, subinput);
// When we get an empty line we are ready to run the input
if (subinput.length() == 0) break;
input += "\n" + subinput;
}
}
// TODO process ; delimited statements?
// Don't run empty input
if (input.length() == 0) continue;
// Aesthetics
input += "\n";
// Send to anyone listening for evaluated scripts
python_evaluator.Write(">>>" + input);
// These strings mean we won't get return/output, there are probably more
if (input.find("import") != std::string::npos) outputting = false;
if (input.find("print") != std::string::npos) outputting = false;
if (input.find("=") != std::string::npos) outputting = false;
// This is probably wrong
if (outputting)
input = "print " + input;
PyRun_SimpleString((char*)input.c_str());
}
}
示例3: main
int main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
示例4: main
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
numargs = argc;
Py_InitModule("emb", EmbMethods);
PyRun_SimpleString("from time import time, ctime\n"
"print 'Today is', ctime(time())\n");
PyRun_SimpleString("import emb\n"
"print 'Number of arguments', emb.numargs()\n");
Py_Finalize();
return 0;
}
示例5: python_check_error
void
python_check_error(void)
{
if (PyErr_Occurred()) {
PyErr_Print();
PyRun_SimpleString("import sys\nsys.stdout.flush()");
PyErr_Clear();
}
}
示例6: script_init_mudmagic
PyMODINIT_FUNC script_init_mudmagic(void) {
PyObject * p_main;
PyImport_AddModule ("mudmagic");
Py_InitModule ("mudmagic", MudmagicMethods);
p_main = PyImport_AddModule ("__main__");
p_main_dict = PyModule_GetDict (p_main);
PyRun_SimpleString("from mudmagic import *\n");
}
示例7: PyRun_SimpleString
bool Pythonize::runString (char *str)
{
if (str == NULL || strlen (str) == 0) return false;
int res = PyRun_SimpleString (str);
return res == 0;
}
示例8: main
int main(int argc, char **argv) {
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
// or use PyRun_SimpleFile(FILE *fp, const char *filename) to run a program file
Py_Finalize();
return 0;
}
示例9: PyRun_SimpleString
void PyModule::printInfo() {
for (auto& elem : methods_) {
std::string msg = "print(\"";
msg += elem->getName() + ":\t";
msg += elem->getDesc();
msg += "\")\n";
PyRun_SimpleString(msg.c_str());
}
}
示例10: init_script
void init_script(const char *exe_path)
{
exe_dir=QFileInfo(exe_path).absoluteDir();
qDebug() << "prog name:" << exe_path;
Py_SetProgramName((char*)exe_path);
qDebug() << "init py...";
Py_Initialize();
qDebug() << "init py ok";
init_python_console();
init_sproxel_bindings();
pycon("exe path: %s", exe_path);
pycon("exe dir: %s", exe_dir.absolutePath().toLocal8Bit().data());
QString code="import sys\nsys.path.insert(0, \"";
code.append(exe_dir.absolutePath());
code.append("\")\nprint 'sys.path:', sys.path\n");
PyRun_SimpleString(code.toLocal8Bit().data());
PyObject *mod=PyImport_ImportModule("sproxel_utils");
if (!mod)
{
PyErr_Print();
QMessageBox::critical(NULL, "Sproxel Error", "Failed to import sproxel_utils");
}
else
{
// check required methods
bool gotErrors=false;
py_save_project=PyObject_GetAttrString(mod, "save_project");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_load_project=PyObject_GetAttrString(mod, "load_project");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_init_plugin_pathes=PyObject_GetAttrString(mod, "init_plugin_pathes");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_scan_plugins=PyObject_GetAttrString(mod, "scan_plugins");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_register_plugins=PyObject_GetAttrString(mod, "register_plugins");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_unregister_plugins=PyObject_GetAttrString(mod, "unregister_plugins");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
pycon("Scripted methods: %s", gotErrors ? "some missing" : "all OK");
if (gotErrors) QMessageBox::critical(NULL, "Sproxel Error", "Some scripted methods are missing.");
Py_DECREF(mod); mod=NULL;
}
}
示例11: Python_Init
static int
Python_Init(void)
{
if (!initialised)
{
#ifdef DYNAMIC_PYTHON
if (!python_enabled(TRUE))
{
EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
goto fail;
}
#endif
init_structs();
#if !defined(MACOS) || defined(MACOS_X_UNIX)
Py_Initialize();
#else
PyMac_Initialize();
#endif
/* initialise threads */
PyEval_InitThreads();
#ifdef DYNAMIC_PYTHON
get_exceptions();
#endif
if (PythonIO_Init())
goto fail;
if (PythonMod_Init())
goto fail;
/* Remove the element from sys.path that was added because of our
* argv[0] value in PythonMod_Init(). Previously we used an empty
* string, but dependinding on the OS we then get an empty entry or
* the current directory in sys.path. */
PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
/* the first python thread is vim's, release the lock */
Python_SaveThread();
initialised = 1;
}
return 0;
fail:
/* We call PythonIO_Flush() here to print any Python errors.
* This is OK, as it is possible to call this function even
* if PythonIO_Init() has not completed successfully (it will
* not do anything in this case).
*/
PythonIO_Flush();
return -1;
}
示例12: OllyPython_Init
BOOL OllyPython_Init(void)
{
char initfile[MAX_PATH];
char tmp[MAX_PATH+16];
BOOL result = 1;
if (initialized == 1)
{
return TRUE;
}
Addtolist(0, 0, "OllyPython");
result &= CheckFile("init.py");
result &= CheckFile("ollyapi.py");
result &= CheckFile("ollyutils.py");
if (!result)
{
Addtolist(0, -1, " Could not locate Python scripts");
return FALSE;
}
Py_Initialize();
if (!Py_IsInitialized())
{
Addtolist(0, -1, " Could not initialize Python");
return FALSE;
}
init_ollyapi();
GetModuleFileName(hinst, initfile, MAX_PATH);
PathRemoveFileSpec(initfile);
strncat(initfile, "\\python", 7);
snprintf(tmp, MAX_PATH+16, "OLLYPYTHON_PATH=\"%s\"", initfile);
PyRun_SimpleString(tmp);
strncat(initfile, "\\init.py", 8);
if (!ExecFile(initfile))
{
Addtolist(0, -1, " Could not run init.py");
return FALSE;
}
#ifdef ENABLE_PYTHON_PROFILING
PyEval_SetTrace(tracefunc, NULL);
#endif
initialized = 1;
return TRUE;
}
示例13: main
int main() {
PyObject *globals, *foo, *bar, *noargs, *ret;
Py_Initialize();
PyRun_SimpleString(
"def foo(): \n"
" return 2, 10 \n"
"def bar(): \n"
" return \n"
"def teste(a,b): \n"
" print a, b \n"
" return 42 \n"
);
globals = PyModule_GetDict(PyImport_AddModule("__main__"));
Py_INCREF(globals);
static PyMethodDef teste_def[] = {{ "teste", (PyCFunction) teste, METH_VARARGS, NULL }};
PyObject* teste_obj = Py_FindMethod(teste_def, NULL, "teste");
PyDict_SetItemString(globals, "teste", teste_obj);
PyObject *teste = PyDict_GetItemString(globals, "teste");
PyObject *obj_result = PyObject_CallFunction(teste, "si", "entrada", 2);
if (!obj_result) {
return;
}
long result = PyInt_AsLong(obj_result);
Py_DECREF(obj_result);
printf("teste returned %ld \n", result);
foo = PyDict_GetItemString(globals, "foo");
noargs = PyTuple_New(0);
ret = PyObject_Call(foo, noargs, NULL);
printf("foo returned (%d, %d)\n", PyInt_AsLong(PyTuple_GET_ITEM(ret, 0)),
PyInt_AsLong(PyTuple_GET_ITEM(ret, 1)));
bar = PyDict_GetItemString(globals, "bar");
Py_DECREF(ret);
ret = PyObject_Call(bar, noargs, NULL);
if (ret == Py_None) printf("bar returned Py_None\n");
Py_DECREF(ret);
Py_DECREF(noargs);
Py_DECREF(globals);
PyObject* s = PyString_FromString("hello world");
PyObject* split = PyObject_GetAttrString(s, "split");
ret = PyObject_CallFunction(split, "s", " ");
/*
ret = PyObject_CallMethod(s, "split", "s", " ");
*/
int i;
for (i = 0; i < PyList_GET_SIZE(ret); i++) {
PyObject* item = PyList_GetItem(ret, i);
printf("%d: '%s'\n", i, PyString_AsString(item));
}
Py_Finalize();
}
示例14: PyRun_SimpleString
void Python_group_script::execute(const std::string grid_name, const std::string group_name) const
{
PyRun_SimpleString(""
"import redirect\n"
"class CoutLogger:\n"
" def __init__(self):\n"
" self.buf = []\n"
" def write(self, data):\n"
" self.buf.append(data)\n"
" if data.endswith('\\n'):\n"
" redirect.sgems_cout(''.join(self.buf))\n"
" self.buf = []\n"
"\n"
"class CerrLogger:\n"
" def __init__(self):\n"
" self.buf = []\n"
" def write(self, data):\n"
" self.buf.append(data)\n"
" if data.endswith('\\n'):\n"
" redirect.sgems_cerr(''.join(self.buf))\n"
" self.buf = []\n"
"\n"
"import sys\n"
"sys.stdout = CoutLogger()\n"
"sys.stderr = CerrLogger()\n"
"");
FILE* fp = fopen(filename_.c_str(), "r");
if (!fp)
{
GsTLcerr << "can't open file " << filename_ << gstlIO::end;
return;
}
PyObject* module = PyImport_AddModule("__main__");
PyObject* dictionary = PyModule_GetDict(module);
PyObject* dictionary_copy = PyDict_Copy(dictionary);
PyRun_File(fp, filename_.c_str(), Py_file_input, dictionary_copy, dictionary_copy);
PyObject* function = PyDict_GetItemString(dictionary_copy, "sgems_execute_group_action");
if (PyCallable_Check(function))
{
PyObject* result = PyObject_CallFunction(function, "ss", grid_name.c_str(), group_name.c_str());
// if (NULL == result)
// {
// std::cout << "execution failed\n";
// }
Py_XDECREF(result);
}
Py_XDECREF(dictionary_copy);
fclose(fp);
}
示例15: test
void test() {
print_line("testing python");
PyRun_SimpleString("import engine\n");
PyRun_SimpleString("def test(self):\n\tprint(\"noway\")\n");
PyRun_SimpleString("a=engine.ObjectPtr()\n");
PyRun_SimpleString("a.noway(22,'hello')\n");
PyRun_SimpleString("a.normalize()\n");
PyRun_SimpleString("class Moch(engine.ObjectPtr):\n\tdef mooch(self):\n\t\tprint('muchi')\n");
PyRun_SimpleString("b=Moch();\n");
PyRun_SimpleString("b.mooch();\n");
PyRun_SimpleString("b.meis();\n");
}