-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_runner.h
More file actions
53 lines (46 loc) · 2.31 KB
/
Copy pathpython_runner.h
File metadata and controls
53 lines (46 loc) · 2.31 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
#pragma once
#include "embedded_runner.h"
#include <QMutex>
// ─────────────────────────────────────────────────────────────────────────────
// PythonRunner — runs Python 3 in-process via CPython's embedding API.
//
// Output capture:
// sys.stdout and sys.stderr are replaced with io.StringIO() objects before
// each execution. After PyRun_String() returns, getvalue() retrieves the
// captured text. The real sys.stdout/stderr are restored afterwards.
//
// Stdin injection:
// sys.stdin is replaced with io.StringIO(stdinInput) so input() / sys.stdin
// reads work correctly without a real terminal.
//
// Interpreter lifecycle:
// Py_Initialize() is called ONCE in the constructor (it is expensive and
// not meant to be called repeatedly). The interpreter stays alive for the
// lifetime of the PythonRunner object.
//
// IMPORTANT: only create ONE PythonRunner per process. CPython is not
// designed for multiple simultaneous interpreters without sub-interpreters.
//
// Thread safety:
// m_mutex serialises all Python calls. CPython's GIL does not protect you
// from calling into the interpreter from multiple Qt threads simultaneously
// without acquiring the GIL first.
// ─────────────────────────────────────────────────────────────────────────────
class PythonRunner : public EmbeddedRunner
{
public:
// Initialises the CPython interpreter. Call once per process.
explicit PythonRunner();
// Finalises the interpreter. Do not create another after this.
~PythonRunner() override;
Result execute(const QString &code,
const QString &stdinInput,
volatile bool *abort = nullptr,
const QMap<QString, QString> &additionalFiles = {}) override;
QString languageId() const override { return QStringLiteral("python"); }
QString languageName() const override { return QStringLiteral("Python"); }
bool isInitialised() const { return m_initialised; }
private:
bool m_initialised = false;
QMutex m_mutex;
};