diff options
| author | robot-piglet <[email protected]> | 2025-07-24 21:46:28 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2025-07-24 21:58:13 +0300 |
| commit | 475c997f5ded6c9025b31345e894afb2fd00de27 (patch) | |
| tree | 1be88f3ae818b4b9c5d2d3350a44e88a6d57b250 /yql/essentials/udfs/common/python/bindings/py_test_engine.h | |
| parent | aac2e8a24f54055971d7291453001b440a0be98c (diff) | |
Intermediate changes
commit_hash:4b0cad6620396d4b5422b264f4aa162104d45e8d
Diffstat (limited to 'yql/essentials/udfs/common/python/bindings/py_test_engine.h')
| -rw-r--r-- | yql/essentials/udfs/common/python/bindings/py_test_engine.h | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/yql/essentials/udfs/common/python/bindings/py_test_engine.h b/yql/essentials/udfs/common/python/bindings/py_test_engine.h index ff0a61cbb8a..6809fc61cff 100644 --- a/yql/essentials/udfs/common/python/bindings/py_test_engine.h +++ b/yql/essentials/udfs/common/python/bindings/py_test_engine.h @@ -115,6 +115,32 @@ public: ToMiniKQLWithArg<TChecker>(type, argValue, script, std::move(checker)); } + template <typename FunctionType, + typename TMiniKQLValueBuilder, + typename TChecker> + void UnsafeCall(TMiniKQLValueBuilder&& builder, + const TStringBuf& script, + TChecker&& checker) + { + TPyObjectPtr function = CompilePythonFunction(script); + const auto functionType = GetTypeBuilder().SimpleSignatureType<FunctionType>(); + NUdf::TCallableTypeInspector inspector(*CastCtx_->PyCtx->TypeInfoHelper, functionType); + Y_ENSURE(inspector.GetArgsCount() == 1); + const TType* argType = static_cast<const TType*>(inspector.GetArgType(0)); + NUdf::TUnboxedValue value = builder(argType, GetValueBuilder()); + TPyObjectPtr pyArgs = ToPyArgs(CastCtx_, functionType, &value, inspector); + TPyObjectPtr resultObj = PyObject_CallObject(function.Get(), pyArgs.Get()); + + if (!resultObj) { + return checker(NUdf::TUnboxedValuePod::Invalid()); + } + + const auto returnType = inspector.GetReturnType(); + Y_ENSURE(CastCtx_->PyCtx->TypeInfoHelper->GetTypeKind(returnType) != NUdf::ETypeKind::Stream); + + checker(FromPyObject(CastCtx_, returnType, resultObj.Get())); + } + template <typename TMiniKQLValueBuilder> TPyObjectPtr ToPython( NUdf::TType* udfType, @@ -189,9 +215,7 @@ public: } private: - TPyObjectPtr RunPythonFunction( - const TStringBuf& script, PyObject* args = nullptr) - { + TPyObjectPtr CompilePythonFunction(const TStringBuf& script) { TString filename(TStringBuf("embedded:test.py")); TPyObjectPtr code(Py_CompileString(script.data(), filename.data(), Py_file_input)); if (!code) { @@ -211,6 +235,13 @@ private: PyErr_Print(); UNIT_FAIL("function 'Test' is not found in module"); } + return function; + } + + TPyObjectPtr RunPythonFunction( + const TStringBuf& script, PyObject* args = nullptr) + { + TPyObjectPtr function(CompilePythonFunction(script)); return PyObject_CallObject(function.Get(), args); } |
