"training/src/datamodules/datasets/lm_dataset.py" did not exist on "9bc63d1e2dd3eee8cf0307036e077a752bd79fde"
_dynfuncmod.c 2.57 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
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
86
87
88
89
90
91
92
93
#include "_dynfunc.c"

/* Python-facing function to dynamically create a new C function object */
static PyObject*
make_function(PyObject *self, PyObject *args)
{
    PyObject *module, *fname, *fdoc, *fnaddrobj;
    void *fnaddr;
    EnvironmentObject *env;
    PyObject *keepalive;

    if (!PyArg_ParseTuple(args, "OOOOO!|O",
            &module, &fname, &fdoc, &fnaddrobj, &EnvironmentType, &env,
            &keepalive)) {
        return NULL;
    }

    fnaddr = PyLong_AsVoidPtr(fnaddrobj);
    if (fnaddr == NULL && PyErr_Occurred())
        return NULL;

    return pycfunction_new(module, fname, fdoc, fnaddr, env, keepalive);
}

static PyMethodDef ext_methods[] = {
#define declmethod(func) { #func , ( PyCFunction )func , METH_VARARGS , NULL }
    declmethod(make_function),
    { NULL },
#undef declmethod
};


static PyObject *
build_c_helpers_dict(void)
{
    PyObject *dct = PyDict_New();
    if (dct == NULL)
        goto error;

#define _declpointer(name, value) do {                 \
    PyObject *o = PyLong_FromVoidPtr(value);           \
    if (o == NULL) goto error;                         \
    if (PyDict_SetItemString(dct, name, o)) {          \
        Py_DECREF(o);                                  \
        goto error;                                    \
    }                                                  \
    Py_DECREF(o);                                      \
} while (0)

#define declmethod(func) _declpointer(#func, &Numba_##func)

#define declpointer(ptr) _declpointer(#ptr, &ptr)

    declmethod(make_generator);

#undef declmethod
    return dct;
error:
    Py_XDECREF(dct);
    return NULL;
}

MOD_INIT(_dynfunc) {
    PyObject *m, *impl_info;

    MOD_DEF(m, "_dynfunc", "No docs", ext_methods)
    if (m == NULL)
        return MOD_ERROR_VAL;

    if (init_dynfunc_module(m))
        return MOD_ERROR_VAL;

    impl_info = Py_BuildValue(
        "{snsnsn}",
        "offsetof_closure_body", offsetof(ClosureObject, env),
        "offsetof_env_body", offsetof(EnvironmentObject, globals),
        "offsetof_generator_state", offsetof(GeneratorObject, state)
        );
    if (impl_info == NULL)
        return MOD_ERROR_VAL;
    PyModule_AddObject(m, "_impl_info", impl_info);

    Py_INCREF(&ClosureType);
    PyModule_AddObject(m, "_Closure", (PyObject *) (&ClosureType));
    Py_INCREF(&EnvironmentType);
    PyModule_AddObject(m, "Environment", (PyObject *) (&EnvironmentType));
    Py_INCREF(&GeneratorType);
    PyModule_AddObject(m, "_Generator", (PyObject *) (&GeneratorType));

    PyModule_AddObject(m, "c_helpers", build_c_helpers_dict());

    return MOD_SUCCESS_VAL(m);
}