module.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <Python.h>
  2. #include <library/python/symbols/registry/syms.h>
  3. #include <util/generic/string.h>
  4. #define CAP(x) SYM_2(x, x)
  5. BEGIN_SYMS("_capability")
  6. #if defined(_musl_)
  7. CAP("musl")
  8. #endif
  9. #if defined(_linux_)
  10. CAP("linux")
  11. #endif
  12. #if defined(_darwin_)
  13. CAP("darwin")
  14. #endif
  15. CAP("_sentinel")
  16. END_SYMS()
  17. #undef CAP
  18. using namespace NPrivate;
  19. namespace {
  20. template <class T>
  21. struct TCB: public ICB {
  22. inline TCB(T& t)
  23. : CB(&t)
  24. {
  25. }
  26. void Apply(const char* mod, const char* name, void* sym) override {
  27. (*CB)(mod, name, sym);
  28. }
  29. T* CB;
  30. };
  31. template <class T>
  32. static inline TCB<T> MakeTCB(T& t) {
  33. return t;
  34. }
  35. }
  36. static void DictSetStringPtr(PyObject* dict, const char* name, void* value) {
  37. PyObject* p = PyLong_FromVoidPtr(value);
  38. PyDict_SetItemString(dict, name, p);
  39. Py_DECREF(p);
  40. }
  41. static PyObject* InitSyms(PyObject* m) {
  42. if (!m)
  43. return NULL;
  44. PyObject* d = PyDict_New();
  45. if (!d)
  46. return NULL;
  47. auto f = [&](const char* mod, const char* name, void* sym) {
  48. DictSetStringPtr(d, (TString(mod) + "|" + TString(name)).c_str(), sym);
  49. };
  50. auto cb = MakeTCB(f);
  51. ForEachSymbol(cb);
  52. if (PyObject_SetAttrString(m, "syms", d))
  53. m = NULL;
  54. Py_DECREF(d);
  55. return m;
  56. }
  57. #if PY_MAJOR_VERSION >= 3
  58. static struct PyModuleDef module = {
  59. PyModuleDef_HEAD_INIT, "syms", NULL, -1, NULL, NULL, NULL, NULL, NULL};
  60. extern "C" PyObject* PyInit_syms() {
  61. return InitSyms(PyModule_Create(&module));
  62. }
  63. #else
  64. extern "C" void initsyms() {
  65. InitSyms(Py_InitModule("syms", NULL));
  66. }
  67. #endif