aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/pybind/embedding.cpp
blob: 78b608715efc16ba39bced15a4968b0a9d2ffb93 (plain) (blame)
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
#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include "embedding.h"

#include <util/generic/ptr.h>
#include <util/generic/yexception.h>

namespace NPyBind {
    TEmbedding::TEmbedding(char* argv0) {
#if PY_MAJOR_VERSION < 3
        Py_SetProgramName(argv0);
        Py_Initialize();
#else
        PyStatus status;

        PyConfig config;
        PyConfig_InitPythonConfig(&config);
        // Disable parsing command line arguments
        config.parse_argv = 0;

        status = PyConfig_SetBytesString(&config, &config.program_name, argv0);
        if (PyStatus_Exception(status)) {
            PyConfig_Clear(&config);
            Py_ExitStatusException(status);
        }

        status = Py_InitializeFromConfig(&config);
        if (PyStatus_Exception(status)) {
            PyConfig_Clear(&config);
            Py_ExitStatusException(status);
        }

        PyConfig_Clear(&config);
#endif
    }

    TEmbedding::~TEmbedding() {
        Py_Finalize();
    }
}