| 1197 | } |
| 1198 | |
| 1199 | void ScriptModule::RunCode(double time, std::string code) |
| 1200 | { |
| 1201 | //should only be called from main thread |
| 1202 | |
| 1203 | if (!sPythonInitialized) |
| 1204 | { |
| 1205 | TheSynth->LogEvent("trying to call ScriptModule::RunCode() before python is initialized", kLogEventType_Error); |
| 1206 | return; |
| 1207 | } |
| 1208 | |
| 1209 | if (sHasLoadedUntrustedScript) |
| 1210 | { |
| 1211 | TheSynth->LogEvent("can't run scripts until user has added all loaded scripts to the allow list", kLogEventType_Error); |
| 1212 | return; |
| 1213 | } |
| 1214 | |
| 1215 | sMostRecentRunTime = time; |
| 1216 | mNextLineToExecute = -1; |
| 1217 | ComputeSliders(0); |
| 1218 | sPriorExecutedModule = nullptr; |
| 1219 | |
| 1220 | try |
| 1221 | { |
| 1222 | FixUpCode(code); |
| 1223 | py::exec(code, py::globals()); |
| 1224 | |
| 1225 | mCodeEntry->SetError(false); |
| 1226 | mLastError = ""; |
| 1227 | } |
| 1228 | catch (pybind11::error_already_set& e) |
| 1229 | { |
| 1230 | ofLog() << "python execution exception (error_already_set): " << e.what(); |
| 1231 | |
| 1232 | if (mNextLineToExecute == -1) //this script hasn't executed yet |
| 1233 | sMostRecentLineExecutedModule = this; |
| 1234 | |
| 1235 | sMostRecentLineExecutedModule->mLastError = (std::string)py::str(e.type()) + ": " + (std::string)py::str(e.value()); |
| 1236 | |
| 1237 | int lineNumber = sMostRecentLineExecutedModule->mNextLineToExecute; |
| 1238 | if (lineNumber == -1) |
| 1239 | { |
| 1240 | std::string errorString = (std::string)py::str(e.value()); |
| 1241 | const std::string lineTextLabel = " line "; |
| 1242 | const char* lineTextPos = strstr(errorString.c_str(), lineTextLabel.c_str()); |
| 1243 | if (lineTextPos != nullptr) |
| 1244 | { |
| 1245 | try |
| 1246 | { |
| 1247 | size_t start = lineTextPos + lineTextLabel.length() - errorString.c_str(); |
| 1248 | size_t len = errorString.size() - 1 - start; |
| 1249 | std::string lineNumberText = errorString.substr(start, len); |
| 1250 | int rawLineNumber = stoi(lineNumberText); |
| 1251 | int realLineNumber = rawLineNumber - 1; |
| 1252 | |
| 1253 | std::vector<std::string> lines = ofSplitString(sMostRecentLineExecutedModule->mLastRunLiteralCode, "\n"); |
| 1254 | for (size_t i = 0; i < lines.size() && i < rawLineNumber; ++i) |
| 1255 | { |
| 1256 | if (ofIsStringInString(lines[i], "###instrumentation###")) |
no test coverage detected