-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathpython-module.cc
61 lines (46 loc) · 1.92 KB
/
python-module.cc
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
#include <Python.h>
#include "internal/eval.hh"
#include "internal/ptr.hh"
#include <nix/config.h>
#include <eval.hh>
#include <globals.hh>
#include <shared.hh>
namespace pythonnix {
#define _public_ __attribute__((visibility("default")))
PyObject *NixError = nullptr;
static PyMethodDef NixMethods[] = {{"eval", (PyCFunction)eval,
METH_VARARGS | METH_KEYWORDS,
"Eval nix expression"},
{NULL, NULL, 0, NULL}};
static struct PyModuleDef nixmodule = {
PyModuleDef_HEAD_INIT, "nix", "Nix expression bindings",
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
NixMethods};
extern "C" _public_ PyObject *PyInit_nix(void) {
// By default, Nix sets the build-hook to be "$(readlink /proc/self/exe) __build-remote", expecting the current binary to be Nix itself.
// But when we call the Nix library from Python this isn't the case, the current binary is Python then
// So we need to change this default, pointing it to the Nix binary instead
nix::settings.buildHook = nix::settings.nixBinDir + "/nix";
// And by setting buildHook before calling initNix, we can override the defaults without overriding the user-provided options from the config files
nix::initNix();
nix::initGC();
PyObjPtr m(PyModule_Create(&nixmodule));
if (!m) {
return nullptr;
}
NixError = PyErr_NewExceptionWithDoc(
"nix.NixError", /* char *name */
"Base exception class for the nix module.", /* char *doc */
NULL, /* PyObject *base */
NULL /* PyObject *dict */
);
if (!NixError) {
return nullptr;
}
if (PyModule_AddObject(m.get(), "NixError", NixError) == -1) {
return nullptr;
}
return m.release();
}
} // namespace pythonnix