aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/symbols/module/module.cpp
blob: 92bc7f4d67d5adbcf9232fca142ed8d5b04dd617 (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <Python.h>

#include <library/python/symbols/registry/syms.h>

#include <util/generic/string.h>

#define CAP(x) SYM_2(x, x)

BEGIN_SYMS("_capability")
#if defined(_musl_)
CAP("musl")
#endif
#if defined(_linux_)
CAP("linux")
#endif
#if defined(_darwin_)
CAP("darwin")
#endif
CAP("_sentinel")
END_SYMS()

#undef CAP

using namespace NPrivate;

namespace {
    template <class T>
    struct TCB: public ICB {
        inline TCB(T& t)
            : CB(&t)
        {
        }

        void Apply(const char* mod, const char* name, void* sym) override {
            (*CB)(mod, name, sym);
        }

        T* CB;
    };

    template <class T>
    static inline TCB<T> MakeTCB(T& t) {
        return t;
    }
}

static void DictSetStringPtr(PyObject* dict, const char* name, void* value) {
    PyObject* p = PyLong_FromVoidPtr(value);
    PyDict_SetItemString(dict, name, p);
    Py_DECREF(p);
}

static PyObject* InitSyms(PyObject* m) {
    if (!m)
        return NULL;
    PyObject* d = PyDict_New();
    if (!d)
        return NULL;

    auto f = [&](const char* mod, const char* name, void* sym) {
        DictSetStringPtr(d, (TString(mod) + "|" + TString(name)).c_str(), sym);
    };

    auto cb = MakeTCB(f);

    ForEachSymbol(cb);

    if (PyObject_SetAttrString(m, "syms", d))
        m = NULL;
    Py_DECREF(d);
    return m;
}

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef module = {
    PyModuleDef_HEAD_INIT, "syms", NULL, -1, NULL, NULL, NULL, NULL, NULL};

extern "C" PyObject* PyInit_syms() {
    return InitSyms(PyModule_Create(&module));
}
#else
extern "C" void initsyms() {
    InitSyms(Py_InitModule("syms", NULL));
}
#endif