aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/pybind/pod.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/pod.h
parent65a08c9fdece8dba50da8beb4d7c81447211dd45 (diff)
downloadydb-e0094c4ad6964e11564777bc0d859c68d8aa9de2.tar.gz
Migrate black linter on custom_lint pipeline
Diffstat (limited to 'library/cpp/pybind/pod.h')
-rw-r--r--library/cpp/pybind/pod.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/library/cpp/pybind/pod.h b/library/cpp/pybind/pod.h
new file mode 100644
index 00000000000..90165fdbec6
--- /dev/null
+++ b/library/cpp/pybind/pod.h
@@ -0,0 +1,53 @@
+#pragma once
+
+#define PY_SSIZE_T_CLEAN
+#include <Python.h>
+#include "attr.h"
+#include "typedesc.h"
+
+namespace NPyBind {
+ struct TPOD {
+ TPyObjectPtr Dict;
+
+ TPOD()
+ : Dict(PyDict_New(), true)
+ {
+ }
+ bool SetAttr(const char* name, PyObject* value) {
+ return PyDict_SetItemString(Dict.Get(), name, value) == 0;
+ }
+ PyObject* GetAttr(const char* name) const {
+ PyObject* res = PyDict_GetItemString(Dict.Get(), name);
+ Py_XINCREF(res);
+ return res;
+ }
+ };
+
+ class TPODTraits: public NPyBind::TPythonType<TPOD, TPOD, TPODTraits> {
+ private:
+ typedef TPythonType<TPOD, TPOD, TPODTraits> MyParent;
+ friend class TPythonType<TPOD, TPOD, TPODTraits>;
+ TPODTraits();
+
+ public:
+ static TPOD* GetObject(TPOD& obj) {
+ return &obj;
+ }
+ };
+
+ template <>
+ inline bool FromPyObject<TPOD*>(PyObject* obj, TPOD*& res) {
+ res = TPODTraits::CastToObject(obj);
+ if (res == nullptr)
+ return false;
+ return true;
+ }
+ template <>
+ inline bool FromPyObject<const TPOD*>(PyObject* obj, const TPOD*& res) {
+ res = TPODTraits::CastToObject(obj);
+ if (res == nullptr)
+ return false;
+ return true;
+ }
+
+}