aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/qtcreatorcdbext/pystdoutredirect.cpp
blob: d70668131aab36e9fb8802b1187edef184581f21 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "pystdoutredirect.h"

static std::string output;
static PyObject *stdoutRedirect = nullptr;

struct StdoutRedirect
{
    PyObject_HEAD
};

PyObject *stdoutRedirect_write(PyObject * /*self*/, PyObject *args)
{
    char *string;
    PyArg_ParseTuple(args, "s", &string);
    output += string;
    return Py_BuildValue("");
}

PyObject *stdoutRedirect_flush(PyObject * /*self*/, PyObject * /*args*/)
{
    return Py_BuildValue("");
}

static PyMethodDef StdoutRedirectMethods[] =
{
    {"write", stdoutRedirect_write, METH_VARARGS, "sys.stdout.write"},
    {"flush", stdoutRedirect_flush, METH_VARARGS, "sys.stdout.flush"},
    {0, 0, 0, 0} // sentinel
};

PyTypeObject *stdoutRedirect_pytype()
{
    static PyTypeObject cdbext_StdoutRedirectType =
    {
        PyVarObject_HEAD_INIT(NULL, 0)
        "cdbext.StdoutRedirect",    /* tp_name */
        sizeof(StdoutRedirect),     /* tp_basicsize */
        0,                          /* tp_itemsize */
        0,                          /* tp_dealloc */
        0,                          /* tp_print */
        0,                          /* tp_getattr */
        0,                          /* tp_setattr */
        0,                          /* tp_reserved */
        0,                          /* tp_repr */
        0,                          /* tp_as_number */
        0,                          /* tp_as_sequence */
        0,                          /* tp_as_mapping */
        0,                          /* tp_hash  */
        0,                          /* tp_call */
        0,                          /* tp_str */
        0,                          /* tp_getattro */
        0,                          /* tp_setattro */
        0,                          /* tp_as_buffer */
        Py_TPFLAGS_DEFAULT,         /* tp_flags */
        "stdout redirector",        /* tp_doc */
        0,                          /* tp_traverse */
        0,                          /* tp_clear */
        0,                          /* tp_richcompare */
        0,                          /* tp_weaklistoffset */
        0,                          /* tp_iter */
        0,                          /* tp_iternext */
        StdoutRedirectMethods,      /* tp_methods */
    };
    return &cdbext_StdoutRedirectType;
}

void startCapturePyStdout()
{
    if (stdoutRedirect != nullptr)
        endCapturePyStdout();
    stdoutRedirect = _PyObject_New(stdoutRedirect_pytype());
    if (PySys_SetObject("stdout", stdoutRedirect) != 0)
        PySys_SetObject("stdout", PySys_GetObject("__stdout__"));
    if (PySys_SetObject("stderr", stdoutRedirect) != 0)
        PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
    PyObject_CallMethod(stdoutRedirect, "write", "s", "text");
    output.clear();
}

std::string getPyStdout()
{
    return output;
}

void endCapturePyStdout()
{
    PySys_SetObject("stdout", PySys_GetObject("__stdout__"));
    PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
    Py_DecRef(stdoutRedirect);
    stdoutRedirect = nullptr;
}