aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/pybind/ptr.h
diff options
context:
space:
mode:
authorsay <say@yandex-team.com>2023-02-14 17:24:43 +0300
committersay <say@yandex-team.com>2023-02-14 17:24:43 +0300
commite0094c4ad6964e11564777bc0d859c68d8aa9de2 (patch)
tree5d2ad1a4df88da1f74385888891a2a5f9fbbc3ef /library/cpp/pybind/ptr.h
parent65a08c9fdece8dba50da8beb4d7c81447211dd45 (diff)
downloadydb-e0094c4ad6964e11564777bc0d859c68d8aa9de2.tar.gz
Migrate black linter on custom_lint pipeline
Diffstat (limited to 'library/cpp/pybind/ptr.h')
-rw-r--r--library/cpp/pybind/ptr.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/library/cpp/pybind/ptr.h b/library/cpp/pybind/ptr.h
new file mode 100644
index 0000000000..e136736690
--- /dev/null
+++ b/library/cpp/pybind/ptr.h
@@ -0,0 +1,51 @@
+#pragma once
+
+#define PY_SSIZE_T_CLEAN
+#include <Python.h>
+#include <util/generic/ptr.h>
+
+namespace NPyBind {
+ template <class T>
+ class TPythonIntrusivePtrOps {
+ public:
+ static inline void Ref(T* t) noexcept {
+ Py_XINCREF(t);
+ }
+
+ static inline void UnRef(T* t) noexcept {
+ Py_XDECREF(t);
+ }
+
+ static inline void DecRef(T* t) noexcept {
+ Py_XDECREF(t);
+ }
+ };
+
+ class TPyObjectPtr: public TIntrusivePtr<PyObject, TPythonIntrusivePtrOps<PyObject>> {
+ private:
+ typedef TIntrusivePtr<PyObject, TPythonIntrusivePtrOps<PyObject>> TParent;
+ typedef TPythonIntrusivePtrOps<PyObject> TOps;
+
+ public:
+ inline TPyObjectPtr() noexcept {
+ }
+
+ inline explicit TPyObjectPtr(PyObject* obj) noexcept
+ : TParent(obj)
+ {
+ }
+
+ inline TPyObjectPtr(PyObject* obj, bool unref) noexcept
+ : TParent(obj)
+ {
+ if (unref)
+ TOps::UnRef(TParent::Get());
+ }
+
+ inline PyObject* RefGet() {
+ TOps::Ref(TParent::Get());
+ return TParent::Get();
+ }
+ };
+
+}