diff options
author | prettyboy <prettyboy@yandex-team.com> | 2023-09-08 00:22:12 +0300 |
---|---|---|
committer | prettyboy <prettyboy@yandex-team.com> | 2023-09-08 00:46:04 +0300 |
commit | 3a6cd865171eed9b89bf536cd242285f8b583a91 (patch) | |
tree | 25e2756c125f7484fb118e0d5724212199662389 /library/cpp/pybind/embedding.cpp | |
parent | 67f3f216950849664a29035458cfaa5d12a62846 (diff) | |
download | ydb-3a6cd865171eed9b89bf536cd242285f8b583a91.tar.gz |
[build/plugins/ytest] Allow prebuilt linters for opensource
Без этого, ydb или не сможет запускать flake8 с помощью ya make.
Или к ним поедет сборка flake8.
Возможно последнее и не так плохо, но сейчас предлагается пока так
Diffstat (limited to 'library/cpp/pybind/embedding.cpp')
-rw-r--r-- | library/cpp/pybind/embedding.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/library/cpp/pybind/embedding.cpp b/library/cpp/pybind/embedding.cpp new file mode 100644 index 00000000000..cf8941a92af --- /dev/null +++ b/library/cpp/pybind/embedding.cpp @@ -0,0 +1,63 @@ +#define PY_SSIZE_T_CLEAN +#include <Python.h> + +#include "embedding.h" + +#include <util/generic/ptr.h> +#include <util/generic/yexception.h> + +namespace NPyBind { +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 8 + class TDeleteRawMem { + public: + template <typename T> + static inline void Destroy(T* t) noexcept { + PyMem_RawFree(t); + } + }; + + template <typename T> + using TRawMemHolder = THolder<T, TDeleteRawMem>; + + static void SetProgramName(char* name) { + TRawMemHolder<wchar_t> wideName(Py_DecodeLocale(name, nullptr)); + Y_ENSURE(wideName); + Py_SetProgramName(wideName.Get()); + } +#endif + + TEmbedding::TEmbedding(char* argv0) { +#if PY_MAJOR_VERSION < 3 + Py_SetProgramName(argv0); + Py_Initialize(); +#elif PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8 + 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); +#elif PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 8 + SetProgramName(argv0); + Py_Initialize(); +#endif + } + + TEmbedding::~TEmbedding() { + Py_Finalize(); + } +} |