#pragma once #include "py_ptr.h" #include #include #include #include #include #include #include #include namespace NPython { enum class EBytesDecodeMode { Never, Strict, }; class IMemoryLock { public: virtual ~IMemoryLock() = default; virtual void Acquire() = 0; virtual void Release() = 0; }; struct TPyCleanupListItemBase: public TIntrusiveListItem { virtual ~TPyCleanupListItemBase() = default; virtual void Cleanup() = 0; }; template class TPyCleanupListItem: public TPyCleanupListItemBase { public: TPyCleanupListItem() = default; virtual ~TPyCleanupListItem() { Unlink(); } void Cleanup() override { Value = {}; } template void Set(const TIntrusivePtr& ctx, TValueType val) { Value = std::move(val); ctx->CleanupList.PushBack(this); } bool IsSet() const { return !!Value; } const TValueType& Get() const { if (!Value) { throw yexception() << "Trying to use python wrap object with destroyed yql value"; } return Value; } private: TValueType Value; }; struct TPyContext: public TSimpleRefCount { const NKikimr::NUdf::ITypeInfoHelper::TPtr TypeInfoHelper; const NKikimr::NUdf::TStringRef ResourceTag; const NKikimr::NUdf::TSourcePosition Pos; TIntrusiveList CleanupList; TPyContext(NKikimr::NUdf::ITypeInfoHelper::TPtr helper, const NKikimr::NUdf::TStringRef& tag, const NKikimr::NUdf::TSourcePosition& pos) : TypeInfoHelper(std::move(helper)) , ResourceTag(tag) , Pos(pos) { } void Cleanup() { for (auto& o: CleanupList) { o.Cleanup(); } CleanupList.Clear(); } ~TPyContext() = default; using TPtr = TIntrusivePtr; }; struct TPyCastContext: public TSimpleRefCount { const NKikimr::NUdf::IValueBuilder *const ValueBuilder; const TPyContext::TPtr PyCtx; std::unordered_map StructTypes; bool LazyInputObjects = true; TPyObjectPtr YsonConverterIn; TPyObjectPtr YsonConverterOut; EBytesDecodeMode BytesDecodeMode = EBytesDecodeMode::Never; TPyObjectPtr Decimal; std::unordered_map TimezoneNames; THolder MemoryLock; TPyCastContext( const NKikimr::NUdf::IValueBuilder* builder, TPyContext::TPtr pyCtx, THolder memoryLock = {}); ~TPyCastContext(); const TPyObjectPtr& GetTimezoneName(ui32 id); const TPyObjectPtr& GetDecimal(); using TPtr = TIntrusivePtr; }; using TPyCastContextPtr = TPyCastContext::TPtr; } // namspace NPython