MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / runFile

Method runFile

src/Base/Interpreter.cpp:436–487  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

434}
435
436void InterpreterSingleton::runFile(const char* pxFileName, bool local)
437{
438#ifdef FC_OS_WIN32
439 FileInfo fi(pxFileName);
440 FILE* fp = _wfopen(fi.toStdWString().c_str(), L"r");
441#else
442 FILE* fp = fopen(pxFileName, "r");
443#endif
444 if (!fp) {
445 throw FileException("Unknown file", pxFileName);
446 }
447
448 PyGILStateLocker locker;
449 PyObject* module {};
450 PyObject* dict {};
451 module = PyImport_AddModule("__main__");
452 dict = PyModule_GetDict(module);
453 if (local) {
454 dict = PyDict_Copy(dict);
455 }
456 else {
457 Py_INCREF(dict); // avoid to further distinguish between local and global dict
458 }
459
460 if (!PyDict_GetItemString(dict, "__file__")) {
461 PyObject* pyObj = PyUnicode_FromString(pxFileName);
462 if (!pyObj) {
463 fclose(fp);
464 Py_DECREF(dict);
465 return;
466 }
467 if (PyDict_SetItemString(dict, "__file__", pyObj) < 0) {
468 Py_DECREF(pyObj);
469 fclose(fp);
470 Py_DECREF(dict);
471 return;
472 }
473 Py_DECREF(pyObj);
474 }
475
476 PyObject* result = PyRun_File(fp, pxFileName, Py_file_input, dict, dict);
477 fclose(fp);
478 Py_DECREF(dict);
479
480 if (!result) {
481 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
482 throw SystemExitException();
483 }
484 throw PyException();
485 }
486 Py_DECREF(result);
487}
488
489bool InterpreterSingleton::loadModule(const char* psModName)
490{

Callers 4

processFilesMethod · 0.80
runMethod · 0.80
applyMethod · 0.80
executeMethod · 0.80

Calls 4

SystemExitExceptionFunction · 0.85
PyExceptionFunction · 0.85
toStdWStringMethod · 0.80
c_strMethod · 0.45

Tested by

no test coverage detected