py_ctx.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include "py_ptr.h"
  3. #include <yql/essentials/public/udf/udf_types.h>
  4. #include <yql/essentials/public/udf/udf_type_builder.h>
  5. #include <yql/essentials/public/udf/udf_type_inspection.h>
  6. #include <yql/essentials/public/udf/udf_value_builder.h>
  7. #include <yql/essentials/public/udf/udf_string.h>
  8. #include <util/generic/ptr.h>
  9. #include <util/generic/intrlist.h>
  10. #include <unordered_map>
  11. namespace NPython {
  12. enum class EBytesDecodeMode {
  13. Never,
  14. Strict,
  15. };
  16. class IMemoryLock {
  17. public:
  18. virtual ~IMemoryLock() = default;
  19. virtual void Acquire() = 0;
  20. virtual void Release() = 0;
  21. };
  22. struct TPyCleanupListItemBase: public TIntrusiveListItem<TPyCleanupListItemBase> {
  23. virtual ~TPyCleanupListItemBase() = default;
  24. virtual void Cleanup() = 0;
  25. };
  26. template <typename TValueType>
  27. class TPyCleanupListItem: public TPyCleanupListItemBase {
  28. public:
  29. TPyCleanupListItem() = default;
  30. virtual ~TPyCleanupListItem() {
  31. Unlink();
  32. }
  33. void Cleanup() override {
  34. Value = {};
  35. }
  36. template <typename TCtx>
  37. void Set(const TIntrusivePtr<TCtx>& ctx, TValueType val) {
  38. Value = std::move(val);
  39. ctx->CleanupList.PushBack(this);
  40. }
  41. bool IsSet() const {
  42. return !!Value;
  43. }
  44. const TValueType& Get() const {
  45. if (!Value) {
  46. throw yexception() << "Trying to use python wrap object with destroyed yql value";
  47. }
  48. return Value;
  49. }
  50. private:
  51. TValueType Value;
  52. };
  53. struct TPyContext: public TSimpleRefCount<TPyContext> {
  54. const NKikimr::NUdf::ITypeInfoHelper::TPtr TypeInfoHelper;
  55. const NKikimr::NUdf::TStringRef ResourceTag;
  56. const NKikimr::NUdf::TSourcePosition Pos;
  57. TIntrusiveList<TPyCleanupListItemBase> CleanupList;
  58. TPyContext(NKikimr::NUdf::ITypeInfoHelper::TPtr helper, const NKikimr::NUdf::TStringRef& tag, const NKikimr::NUdf::TSourcePosition& pos)
  59. : TypeInfoHelper(std::move(helper))
  60. , ResourceTag(tag)
  61. , Pos(pos)
  62. {
  63. }
  64. void Cleanup() {
  65. for (auto& o: CleanupList) {
  66. o.Cleanup();
  67. }
  68. CleanupList.Clear();
  69. }
  70. ~TPyContext() = default;
  71. using TPtr = TIntrusivePtr<TPyContext>;
  72. };
  73. struct TPyCastContext: public TSimpleRefCount<TPyCastContext> {
  74. const NKikimr::NUdf::IValueBuilder *const ValueBuilder;
  75. const TPyContext::TPtr PyCtx;
  76. std::unordered_map<const NKikimr::NUdf::TType*, TPyObjectPtr> StructTypes;
  77. bool LazyInputObjects = true;
  78. TPyObjectPtr YsonConverterIn;
  79. TPyObjectPtr YsonConverterOut;
  80. EBytesDecodeMode BytesDecodeMode = EBytesDecodeMode::Never;
  81. TPyObjectPtr Decimal;
  82. std::unordered_map<ui32, TPyObjectPtr> TimezoneNames;
  83. THolder<IMemoryLock> MemoryLock;
  84. TPyCastContext(
  85. const NKikimr::NUdf::IValueBuilder* builder,
  86. TPyContext::TPtr pyCtx,
  87. THolder<IMemoryLock> memoryLock = {});
  88. ~TPyCastContext();
  89. const TPyObjectPtr& GetTimezoneName(ui32 id);
  90. const TPyObjectPtr& GetDecimal();
  91. using TPtr = TIntrusivePtr<TPyCastContext>;
  92. };
  93. using TPyCastContextPtr = TPyCastContext::TPtr;
  94. } // namspace NPython