int main(int argc, char **argv) { #if (COW_MPI) { int rank; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank != 0) freopen("/dev/null", "w", stdout); printf("was compiled with MPI support\n"); } #endif Py_Initialize(); PySys_SetArgv(argc, argv); Py_SetProgramName("/Users/jzrake/Work/cow/cowpy"); init_cow(); if (argc > 1) { FILE *fp = fopen(argv[1], "r"); if (fp) { PyRun_SimpleFileExFlags(fp, argv[1], 1, NULL); fclose(fp); } else { printf("No such file %s\n", argv[1]); } } Py_Finalize(); printf("finished...\n"); #if (COW_MPI) MPI_Finalize(); #endif return 0; }
//--------------------------------------------------------- // Launch a Python interpreter, with control passed to it until it // returns during app termination. This routine is called in a // separate thread created by Tart::start(). // // Note that unhandled SystemExit will terminate the process unless // Py_InspectFlag is set, so we need to handle that in blackberry_tart.py. // void TartThread::run() { qDebug() << QThread::currentThreadId() << "TartThread: running"; // Make sure the GIL stuff is set up properly for when do_postMessage() // is called. This may be required in the case where the tart app // doesn't actually create tertiary threads. PyEval_InitThreads(); const char * path = Tart::instance()->getScriptPath(); qDebug() << "script path" << path; if (path) { FILE * mainfile = fopen(path, "r"); if (mainfile) { // We use the SimpleFileExFlags version so that we can pass closeit=1 // (second argument) and to set flags to non-NULL so that the // code can specify "from __future__ import" if required. PyCompilerFlags flags; // Exceptions result in PyErr_Print() being called, and it // will terminate the process if Py_InspectFlag is not set. // If we don't want that to happen, we could either swallow // SystemExit exceptions in blackberry_tart.py, or set the // flag. If we set the flag, however, even clean SystemExits // will result in a traceback being printed to sys.stderr. // Py_InspectFlag = 1; // ensure we return... int rc = PyRun_SimpleFileExFlags(mainfile, path, 1, &flags); // Note: docs say there is no way to get the exception info if // there is an error. // See http://docs.python.org/3.2/c-api/veryhigh.html#PyRun_SimpleStringFlags qDebug() << QThread::currentThreadId() << "PyRun returned" << rc; } else { qDebug() << path << "is missing!\n"; } } else qDebug() << "no Python script specified\n"; // will block until any non-daemon threads terminate qDebug() << "Python finalizing"; Py_Finalize(); // If we never got as far as actually running the tart event loop, // the main Qt thread will still be blocked on the semaphore // waiting for us to start, so release it. if (!ran_loop()) { qDebug() << QThread::currentThreadId() << "TartThread: loop never ran!"; m_sem->release(1); } qDebug() << QThread::currentThreadId() << "TartThread: exiting"; }
/* Parse input from a file and execute it */ int PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags) { if (filename == NULL) filename = "???"; if (Py_FdIsInteractive(fp, filename)) { int err = PyRun_InteractiveLoopFlags(fp, filename, flags); if (closeit) fclose(fp); return err; } else return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); }