aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/python/bindings/py_resource.cpp
blob: ebb096029ade06c15411a59ea2264ffee03f251a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "py_resource.h"
#include "py_cast.h"
#include "py_errors.h"
#include "py_gil.h"
#include "py_utils.h"

#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/public/udf/udf_type_inspection.h>

using namespace NKikimr;

namespace NPython {
namespace {

void DestroyResourceCapsule(PyObject* obj) {
    if (auto* ptr = PyCapsule_GetPointer(obj, ResourceCapsuleName)) {
        delete reinterpret_cast<NUdf::TUnboxedValue*>(ptr);
    }
}

/////////////////////////////////////////////////////////////////////////////
// TResource
/////////////////////////////////////////////////////////////////////////////
class TResource final: public NUdf::TBoxedValue
{
public:
    TResource(PyObject* value, const NUdf::TStringRef& tag)
        : Value_(value, TPyObjectPtr::ADD_REF), Tag_(tag)
    {
    }

    ~TResource() {
        TPyGilLocker lock;
        Value_.Reset();
    }

private:
    NUdf::TStringRef GetResourceTag() const override {
        return Tag_;
    }

    void* GetResource() final {
        return Value_.Get();
    }

    TPyObjectPtr Value_;
    const NUdf::TStringRef Tag_;
};

} // namespace

const char ResourceCapsuleName[] = "YqlResourceCapsule";

TPyObjectPtr ToPyResource(
        const TPyCastContext::TPtr& ctx,
        const NUdf::TType* type,
        const NUdf::TUnboxedValuePod& value)
{
// TODO NILE-43
#if false && UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 15)
    NUdf::TResourceTypeInspector inpector(*ctx->PyCtx->TypeInfoHelper, type);
    auto tag = inpector.GetTag();
    if (tag == ctx->PyCtx->ResourceTag) {
        PyObject* p = reinterpret_cast<PyObject*>(value.GetResource());
        return TPyObjectPtr(p, TPyObjectPtr::ADD_REF);
    }
#else
    Y_UNUSED(type);
    if (value.GetResourceTag() == ctx->PyCtx->ResourceTag) {
        PyObject* p = reinterpret_cast<PyObject*>(value.GetResource());
        return TPyObjectPtr(p, TPyObjectPtr::ADD_REF);
    }
#endif
    auto resource = MakeHolder<NUdf::TUnboxedValue>(value);

    return PyCapsule_New(resource.Release(), ResourceCapsuleName, &DestroyResourceCapsule);
}

NUdf::TUnboxedValue FromPyResource(
        const TPyCastContext::TPtr& ctx,
        const NUdf::TType* type, PyObject* value)
{
// TODO NILE-43
#if false && UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 15)
    NUdf::TResourceTypeInspector inpector(*ctx->PyCtx->TypeInfoHelper, type);
    auto tag = inpector.GetTag();
    if (tag == ctx->PyCtx->ResourceTag) {
        return NUdf::TUnboxedValuePod(new TResource(value, ctx->PyCtx->ResourceTag));
    }

    if (PyCapsule_IsValid(value, ResourceCapsuleName)) {
        auto* resource = reinterpret_cast<NUdf::TUnboxedValue*>(PyCapsule_GetPointer(value, ResourceCapsuleName));
        auto valueTag = resource->GetResourceTag();
        if (valueTag != tag) {
            throw yexception() << "Mismatch of resource tag, expected: "
                << tag << ", got: " << valueTag;
        }

        return *resource;
    }

    throw yexception() << "Python object " << PyObjectRepr(value) \
        << " is not a valid resource with tag " << tag;
#else
    Y_UNUSED(type);
    if (PyCapsule_CheckExact(value)) {
        if (!PyCapsule_IsValid(value, ResourceCapsuleName)) {
            throw yexception() << "Python object " << PyObjectRepr(value) << " is not a valid resource capsule";
        }
        return *reinterpret_cast<NUdf::TUnboxedValue*>(PyCapsule_GetPointer(value, ResourceCapsuleName));
    }
    return NUdf::TUnboxedValuePod(new TResource(value, ctx->PyCtx->ResourceTag));
#endif
}

} // namspace NPython