Repository navigation
Expand file tree
/
Copy pathPythonFileRunner.cpp
More file actions
86 lines (80 loc) · 2.87 KB
/
Copy pathPythonFileRunner.cpp
File metadata and controls
86 lines (80 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS /* _wfopen */
#endif
//=============================================================================
#include "PythonFileRunner.hpp"
#include "PythonEngine.hpp"
#include "characters_encoding.hpp"
#include "PythonLibraryWrapper.hpp"
#include "i18n.hpp"
#include <cstdio>
#include <algorithm>
//=============================================================================
namespace Nelson {
//=============================================================================
void
PythonFileRunner::runFile(const std::wstring& filename, const wstringVector& arguments)
{
outputMessage = L"";
errorMessage = L"";
PyGILState_STATE gstate = NLSPyGILState_Ensure();
PyObject* obj = NLSPy_BuildValue("s", wstring_to_utf8(filename).c_str());
FILE* file = NLS_Py_fopen(obj, "r");
NLSPy_DECREF(obj);
if (file == NULL) {
errorMessage = _W("Impossible to open file: ") + filename;
NLSPyGILState_Release(gstate);
resultReady = true;
resultCondition.notify_one();
return;
}
int ret = NLSPyRun_SimpleFileExFlags(file, wstring_to_utf8(filename).c_str(), 1, NULL);
outputMessage += getPythonStandardOutput();
std::wstring msg = getPythonStandardError();
if (ret != 0) {
errorMessage += msg;
NLSPyErr_Clear();
}
if (!msg.empty()) {
outputMessage += msg;
NLSPyErr_Clear();
}
NLSPyGILState_Release(gstate);
resultReady = true;
resultCondition.notify_one();
}
//=============================================================================
bool
PythonFileRunner::isResultReady()
{
std::lock_guard<std::mutex> lock(resultMutex);
return resultReady;
}
//=============================================================================
std::wstring
PythonFileRunner::getErrorMessage()
{
std::unique_lock<std::mutex> lock(resultMutex);
resultCondition.wait(lock, [this]() { return resultReady; });
return errorMessage;
}
//=============================================================================
std::wstring
PythonFileRunner::getStandardOutput()
{
std::unique_lock<std::mutex> lock(resultMutex);
resultCondition.wait(lock, [this]() { return resultReady; });
return outputMessage;
}
//=============================================================================
}
//=============================================================================