aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/pybind/typedesc.cpp
diff options
context:
space:
mode:
authorprettyboy <prettyboy@yandex-team.com>2023-09-08 00:22:12 +0300
committerprettyboy <prettyboy@yandex-team.com>2023-09-08 00:46:04 +0300
commit3a6cd865171eed9b89bf536cd242285f8b583a91 (patch)
tree25e2756c125f7484fb118e0d5724212199662389 /library/cpp/pybind/typedesc.cpp
parent67f3f216950849664a29035458cfaa5d12a62846 (diff)
downloadydb-3a6cd865171eed9b89bf536cd242285f8b583a91.tar.gz
[build/plugins/ytest] Allow prebuilt linters for opensource
Без этого, ydb или не сможет запускать flake8 с помощью ya make. Или к ним поедет сборка flake8. Возможно последнее и не так плохо, но сейчас предлагается пока так
Diffstat (limited to 'library/cpp/pybind/typedesc.cpp')
-rw-r--r--library/cpp/pybind/typedesc.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/library/cpp/pybind/typedesc.cpp b/library/cpp/pybind/typedesc.cpp
new file mode 100644
index 0000000000..75f39fd126
--- /dev/null
+++ b/library/cpp/pybind/typedesc.cpp
@@ -0,0 +1,79 @@
+#include "typedesc.h"
+
+#include <util/generic/singleton.h>
+
+static void RegisterJSONBridgeImpl() {
+ PyRun_SimpleString("import json\n"
+ "class PyBindEncoder(json.JSONEncoder):\n"
+ " def default(self, obj):\n"
+ " if isinstance(obj, bytes):\n"
+ " try:\n"
+ " return obj.decode()\n"
+ " except UnicodeDecodeError:\n"
+ " return obj.hex()\n"
+ " dct = None\n"
+ " if hasattr(obj, '__getstate__'):\n"
+ " dct = obj.__getstate__()\n"
+ " elif hasattr(obj, '__dict__'):\n"
+ " dct = obj.__dict__\n"
+ " if dct is None:\n"
+ " return json.JSONEncoder.default(self, obj)\n"
+ " if hasattr(obj, '__class__'):\n"
+ " if hasattr(obj.__class__, '__name__'):\n"
+ " dct['__name__'] = obj.__class__.__name__\n"
+ " if hasattr(obj.__class__, '__module__'):\n"
+ " dct['__module__'] = obj.__class__.__module__\n"
+ " if hasattr(obj, 'GetPropertiesNames'):\n"
+ " dct['__properties__'] = obj.GetPropertiesNames()\n"
+ " return dct");
+
+ PyRun_SimpleString("def PyBindObjectHook(dct):\n"
+ " if '__name__' in dct:\n"
+ " name = dct['__name__']\n"
+ " module = dct['__module__']\n"
+ " del dct['__name__']\n"
+ " del dct['__module__']\n"
+ " cls = getattr(__import__(module), name)\n"
+ " if '__properties__' in dct:\n"
+ " props = dct['__properties__']\n"
+ " del dct['__properties__']\n"
+ " if len(props) == 0:\n"
+ " return dct\n"
+ " instance = cls(__properties__ = props)\n"
+ " else:\n"
+ " instance = cls()\n"
+ " if hasattr(instance, '__setstate__'):\n"
+ " instance.__setstate__(dct)\n"
+ " elif hasattr(instance, '__dict__'):\n"
+ " instance.__dict__ = dct\n"
+ " else:\n"
+ " return dct\n"
+ " return instance\n"
+ " return dct");
+
+ PyRun_SimpleString("def json_dump(*args, **kwargs):\n"
+ " kwargs['cls'] = PyBindEncoder\n"
+ " return json.dump(*args, **kwargs)\n"
+ "def json_dumps(*args, **kwargs):\n"
+ " kwargs['cls'] = PyBindEncoder\n"
+ " return json.dumps(*args, **kwargs)");
+
+ PyRun_SimpleString("def json_load(*args, **kwargs):\n"
+ " kwargs['object_hook'] = PyBindObjectHook\n"
+ " return json.load(*args, **kwargs)\n"
+ "def json_loads(*args, **kwargs):\n"
+ " kwargs['object_hook'] = PyBindObjectHook\n"
+ " return json.loads(*args, **kwargs)");
+}
+
+namespace {
+ struct TJSONBridge {
+ TJSONBridge() {
+ RegisterJSONBridgeImpl();
+ }
+ };
+}
+
+void NPyBind::RegisterJSONBridge() {
+ Singleton<TJSONBridge>();
+}