void uwsgi_python_hijack(void) { // the pyshell will be execute only in the first worker #ifndef UWSGI_PYPY FILE *pyfile; if (up.pyrun) { uwsgi.workers[uwsgi.mywid].hijacked = 1; UWSGI_GET_GIL; pyfile = fopen(up.pyrun, "r"); if (!pyfile) { uwsgi_error_open(up.pyrun); exit(1); } PyRun_SimpleFile(pyfile, up.pyrun); // could be never executed exit(0); } #endif if (up.pyshell_oneshot && uwsgi.workers[uwsgi.mywid].hijacked_count > 0) { uwsgi.workers[uwsgi.mywid].hijacked = 0; return; } if (up.pyshell && uwsgi.mywid == 1) { uwsgi.workers[uwsgi.mywid].hijacked = 1; uwsgi.workers[uwsgi.mywid].hijacked_count++; // re-map stdin to stdout and stderr if we are logging to a file if (uwsgi.logfile) { if (dup2(0, 1) < 0) { uwsgi_error("dup2()"); } if (dup2(0, 2) < 0) { uwsgi_error("dup2()"); } } UWSGI_GET_GIL; PyImport_ImportModule("readline"); #ifndef UWSGI_PYPY int ret = PyRun_InteractiveLoop(stdin, "uwsgi"); if (up.pyshell_oneshot) { exit(UWSGI_DE_HIJACKED_CODE); } if (ret == 0) { exit(UWSGI_QUIET_CODE); } #endif exit(0); } }
int PyRun_AnyFileEx(FILE *fp, char *filename, int closeit) { if (filename == NULL) filename = "???"; if (Py_FdIsInteractive(fp, filename)) { int err = PyRun_InteractiveLoop(fp, filename); if (closeit) fclose(fp); return err; } else return PyRun_SimpleFileEx(fp, filename, closeit); }
/* extra ops */ int PP_Run_Command_Line(const char *prompt) { int res; /* interact with python, in "__main__" */ Py_Initialize(); /* in the program's "stdio" window */ if (prompt != NULL) #if defined (FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_MACOSX) printf("[%s <Use Ctrl-D (i.e. EOF) to exit.>]\n", prompt); #elif defined (FC_OS_WIN32) printf("[%s <Use Ctrl-Z plus Return to exit.>]\n", prompt); #endif res = PyRun_InteractiveLoop(stdin, "<stdin>"); return res; }
static void python_interactive_command (char *arg, int from_tty) { struct cleanup *cleanup; int err; cleanup = make_cleanup_restore_integer (&interpreter_async); interpreter_async = 0; arg = skip_spaces (arg); ensure_python_env (get_current_arch (), current_language); if (arg && *arg) { int len = strlen (arg); char *script = xmalloc (len + 2); strcpy (script, arg); script[len] = '\n'; script[len + 1] = '\0'; err = eval_python_command (script); xfree (script); } else { err = PyRun_InteractiveLoop (instream, "<stdin>"); dont_repeat (); } if (err) { gdbpy_print_stack (); error (_("Error while executing Python code.")); } do_cleanups (cleanup); }
int main(void) { std::cout << "*** Start" << std::endl; std::cout << "*** List of registered classes: " << cmnClassRegister::ToString() << std::endl; myDerivedClass derived1; derived1.FixedSizeVector().SetAll(10); derived1.DynamicVector().SetSize(5); derived1.DynamicVector().SetAll(-10); std::cout << "*** Fixed size vector of derived1 before python: " << derived1.FixedSizeVector() << std::endl; myReDerivedClass rederived1; rederived1.FixedSizeVector().SetAll(1000); cmnObjectRegister::Register("derived1", &derived1); cmnObjectRegister::Register("rederived1", &rederived1); std::cout << "*** List from C: " << cmnObjectRegister::ToString() << std::endl; cmnGenericObject* ptr = cmnObjectRegister::FindObject("derived1"); std::cout << "*** Addresses of derived1: " << &derived1 << " " << ptr << std::endl; ptr = cmnObjectRegister::FindObject("rederived1"); std::cout << "*** Addresses of rederived1: " << &rederived1 << " " << ptr << std::endl; std::cout << "*** List from C: " << cmnObjectRegister::ToString() << std::endl; std::string cisstRoot; if (!cmnPath::GetCisstRoot(cisstRoot)) { std::cout << "Can't find environment variable CISST_ROOT" << std::endl; exit(0); } Py_Initialize(); PyRun_SimpleString("import sys; sys.path.append(\".\");"); std::string command = std::string("sys.path.append(\"") + cisstRoot + "/lib\");"; std::cout << "*** Path for cisst libraries: " << cisstRoot << std::endl; PyRun_SimpleString(const_cast<char*>(command.c_str())); #if (CISST_OS == CISST_WINDOWS) #ifdef _DEBUG command = std::string("sys.path.append(\"") + cisstRoot + "/lib/debug\");"; std::cout << "*** Path for cisst libraries: " << cisstRoot << "/lib/debug" << std::endl; #else command = std::string("sys.path.append(\"") + cisstRoot + "/lib/release\");"; std::cout << "*** Path for cisst libraries: " << cisstRoot << "/lib/release" << std::endl; #endif PyRun_SimpleString(const_cast<char*>(command.c_str())); #endif std::cout << "*** Import cisstCommonPython and cisstVectorPython" << std::endl; if (PyRun_SimpleString("import cisstCommonPython; from cisstCommonPython import *") != 0) { std::cout << "Failed importing cisstCommonPython!" << std::endl; exit(0); } if (PyRun_SimpleString("import cisstVectorPython; from cisstVectorPython import *") != 0) { std::cout << "Failed importing cisstVectorPython!" << std::endl; exit(0); } std::cout << "*** Import myDerivedClassPython and myReDerivedClassPython" << std::endl; if (PyRun_SimpleString("import myDerivedClassPython; from myDerivedClassPython import *") != 0) { std::cout << "Failed importing myDerivedClassPython!" << std::endl; exit(0); } if (PyRun_SimpleString("import myReDerivedClassPython; from myReDerivedClassPython import *") != 0) { std::cout << "Failed importing myReDerivedClassPython!" << std::endl; exit(0); } std::cout << "*** Run some Python commands from C" << std::endl; PyRun_SimpleString("derived1 = cmnObjectRegister.FindObject(\"derived1\"); print derived1.__class__; print derived1.FixedSizeVector()"); PyRun_SimpleString("rederived1 = cmnObjectRegister.FindObject(\"rederived1\"); print rederived1.__class__; print rederived1.Services().GetName()"); PyRun_SimpleString("rederived2 = myReDerivedClass(); cmnObjectRegister.Register(\"rederived2\", rederived2);"); std::cout << "*** Load a Python script and execute it" << std::endl; bool FileFound = true; FILE * fp = NULL; cmnPath path; path.AddRelativeToCisstRoot("/bin"); std::string fullName = path.Find("pythonEmbedded.py"); if (fullName == "") { std::cout << "*** Can't find file \"pythonEmbedded.py\" in path " << path << std::endl; FileFound = false; } else { fp = fopen(fullName.c_str(), "r"); if (fp == NULL) { std::cout << "*** Can't open \"" << fullName << "\"" << std::endl; FileFound = false; } } if (FileFound) { #if (CISST_OS != CISST_WINDOWS) PyRun_SimpleFile(fp, "pythonEmbedded.py"); #endif PyRun_InteractiveLoop(fp, "pythonEmbedded.py"); fclose(fp); } std::cout << "*** List from C: " << cmnObjectRegister::ToString() << std::endl; std::cout << "*** Fixed size vector of derived1 after python: " << derived1.FixedSizeVector() << std::endl; Py_Finalize(); return 0; }