-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathexport_add_doc_str.cpp
More file actions
33 lines (30 loc) · 1.36 KB
/
export_add_doc_str.cpp
File metadata and controls
33 lines (30 loc) · 1.36 KB
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
#include "lazyllm.hpp"
#include <iostream>
namespace py = pybind11;
void addDocStr(py::object obj, std::string docs) {
PyObject* ptr = obj.ptr();
if (Py_TYPE(ptr) == &PyCFunction_Type) {
auto f = reinterpret_cast<PyCFunctionObject*>(ptr);
f->m_ml->ml_doc = strdup(docs.c_str());
} else if (Py_TYPE(ptr) == &PyInstanceMethod_Type) {
auto im = reinterpret_cast<PyInstanceMethodObject*>(ptr);
if (Py_TYPE(im->func) == &PyCFunction_Type) {
auto f = reinterpret_cast<PyCFunctionObject*>(im->func);
f->m_ml->ml_doc = strdup(docs.c_str());
}
} else if (Py_TYPE(ptr) == &PyMethod_Type) {
auto m = reinterpret_cast<PyMethodObject*>(ptr);
if (Py_TYPE(m->im_func) == &PyCFunction_Type) {
auto f = reinterpret_cast<PyCFunctionObject*>(m->im_func);
f->m_ml->ml_doc = strdup(docs.c_str());
} else if (Py_TYPE(m->im_func) == &PyFunction_Type) {
auto f = reinterpret_cast<PyFunctionObject*>(m->im_func);
f->func_doc = PyUnicode_FromString(strdup(docs.c_str()));
}
} else {
std::cout << "Adding docstring failed with unexpected type:" << Py_TYPE(ptr)->tp_name << std::endl;
}
}
void exportAddDocStr(py::module& m) {
m.def("add_doc", &addDocStr, "Add docstring to a function or method", py::arg("obj"), py::arg("docs"));
}