aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/runtime_py3/test/subinterpreter/stdout_interceptor.cpp
diff options
context:
space:
mode:
authorsnermolaev <snermolaev@yandex-team.com>2025-04-15 08:08:40 +0300
committersnermolaev <snermolaev@yandex-team.com>2025-04-15 08:21:15 +0300
commit0ca7cfb39890921e0fc5a77e2972842717989f96 (patch)
tree90f759fbc021bbdbf3430593ff34bf8d082429de /library/python/runtime_py3/test/subinterpreter/stdout_interceptor.cpp
parent0e221bf1e6683a34b2ce4cd10a015b0669c836be (diff)
downloadydb-0ca7cfb39890921e0fc5a77e2972842717989f96.tar.gz
Revert "Subinterpretor compatible __res module" rXXXXXX
commit_hash:1d9f5675b9d3ddaa44db4472376a06ad3e811e2d
Diffstat (limited to 'library/python/runtime_py3/test/subinterpreter/stdout_interceptor.cpp')
-rw-r--r--library/python/runtime_py3/test/subinterpreter/stdout_interceptor.cpp77
1 files changed, 0 insertions, 77 deletions
diff --git a/library/python/runtime_py3/test/subinterpreter/stdout_interceptor.cpp b/library/python/runtime_py3/test/subinterpreter/stdout_interceptor.cpp
deleted file mode 100644
index 3cd4b69d012..00000000000
--- a/library/python/runtime_py3/test/subinterpreter/stdout_interceptor.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-#include "stdout_interceptor.h"
-
-#include <util/stream/output.h>
-
-namespace {
-
-struct TOStreamWrapper {
- PyObject_HEAD
- IOutputStream* Stm = nullptr;
-};
-
-PyObject* Write(TOStreamWrapper *self, PyObject *const *args, Py_ssize_t nargs) noexcept {
- try {
- Py_buffer view;
- for (Py_ssize_t i = 0; i < nargs; ++i) {
- PyObject* buf = args[i];
- if (PyUnicode_Check(args[i])) {
- buf = PyUnicode_AsUTF8String(buf);
- if (!buf) {
- return nullptr;
- }
- }
-
- if (PyObject_GetBuffer(buf, &view, PyBUF_SIMPLE | PyBUF_C_CONTIGUOUS) == -1) {
- return nullptr;
- }
- self->Stm->Write(reinterpret_cast<const char*>(view.buf), view.len);
- PyBuffer_Release(&view);
- }
-
- return Py_None;
- } catch(const std::exception& err) {
- PyErr_SetString(PyExc_IOError, err.what());
- } catch (...) {
- PyErr_SetString(PyExc_RuntimeError, "Unhandled C++ exception of unknown type");
- }
- return nullptr;
-}
-
-PyMethodDef TOStreamWrapperMethods[] = {
- {"write", reinterpret_cast<PyCFunction>(Write), METH_FASTCALL, PyDoc_STR("write buffer to wrapped C++ stream")},
- {}
-};
-
-PyTypeObject TOStreamWrapperType {
- .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "testwrap.OStream",
- .tp_basicsize = sizeof(TOStreamWrapper),
- .tp_itemsize = 0,
- .tp_flags = Py_TPFLAGS_DEFAULT,
- .tp_doc = PyDoc_STR("C++ IOStream wrapper"),
- .tp_methods = TOStreamWrapperMethods,
- .tp_new = PyType_GenericNew,
-};
-
-}
-
-TPyStdoutInterceptor::TPyStdoutInterceptor(IOutputStream& redirectionStream) noexcept
- : RealStdout_{PySys_GetObject("stdout")}
-{
- Py_INCREF(RealStdout_);
-
- PyObject* redirect = TOStreamWrapperType.tp_alloc(&TOStreamWrapperType, 0);
- reinterpret_cast<TOStreamWrapper*>(redirect)->Stm = &redirectionStream;
-
- PySys_SetObject("stdout", redirect);
- Py_DECREF(redirect);
-}
-
-TPyStdoutInterceptor::~TPyStdoutInterceptor() noexcept {
- PySys_SetObject("stdout", RealStdout_);
- Py_DECREF(RealStdout_);
-}
-
-bool TPyStdoutInterceptor::SetupInterceptionSupport() noexcept {
- return PyType_Ready(&TOStreamWrapperType) == 0;
-}