summaryrefslogtreecommitdiffstats
path: root/library/cpp/pybind/ptr.h
diff options
context:
space:
mode:
authorrobot-piglet <[email protected]>2025-08-28 14:27:58 +0300
committerrobot-piglet <[email protected]>2025-08-28 14:57:06 +0300
commit81d828c32c8d5477cb2f0ce5da06a1a8d9392ca3 (patch)
tree3081d566f0d5158d76e9093261344f6406fd09f7 /library/cpp/pybind/ptr.h
parent77ea11423f959e51795cc3ef36a48d808b4ffb98 (diff)
Intermediate changes
commit_hash:d5b1af16dbe9030537a04c27eb410c88c2f496cd
Diffstat (limited to 'library/cpp/pybind/ptr.h')
-rw-r--r--library/cpp/pybind/ptr.h56
1 files changed, 56 insertions, 0 deletions
diff --git a/library/cpp/pybind/ptr.h b/library/cpp/pybind/ptr.h
new file mode 100644
index 00000000000..f1b95eca00b
--- /dev/null
+++ b/library/cpp/pybind/ptr.h
@@ -0,0 +1,56 @@
+#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 {
+#ifdef Py_DEBUG
+ if (!Py_IsInitialized()) {
+ return;
+ }
+#endif
+ 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();
+ }
+ };
+
+}