-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize python bindings from pythonix
Copies part of the tree from Pythonix at Mic92/pythonix@fbc8490 into the ./python subdirectory. These Python bindings don't build yet with the current Nix version, which is why they're not built yet in this commit. Co-Authored-By: Jörg Thalheim <joerg@thalheim.io>
- Loading branch information
Showing
13 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2017 Jörg Thalheim | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
project('python-nix', 'cpp', | ||
version : '0.1.8', | ||
license : 'LGPL-2.0', | ||
) | ||
|
||
python_mod = import('python3') | ||
python_dep = dependency('python3', required : true) | ||
nix_expr_dep = dependency('nix-expr', required: true) | ||
|
||
python = python_mod.find_python() | ||
test('python test', python, args : files('tests.py')) | ||
|
||
subdir('src') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "internal/eval.hh" | ||
#include "internal/errors.hh" | ||
#include "internal/nix-to-python.hh" | ||
#include "internal/python-to-nix.hh" | ||
#include <Python.h> | ||
|
||
#include <cxxabi.h> | ||
#include <store-api.hh> | ||
|
||
namespace pythonnix { | ||
|
||
const char *currentExceptionTypeName() { | ||
int status; | ||
auto res = abi::__cxa_demangle(abi::__cxa_current_exception_type()->name(), 0, | ||
0, &status); | ||
return res ? res : "(null)"; | ||
} | ||
|
||
static PyObject *_eval(const char *expression, PyObject *vars) { | ||
nix::Strings storePath; | ||
nix::EvalState state(storePath, nix::openStore()); | ||
|
||
nix::Env *env = nullptr; | ||
auto staticEnv = pythonToNixEnv(state, vars, &env); | ||
if (!staticEnv) { | ||
return nullptr; | ||
} | ||
|
||
auto e = state.parseExprFromString(expression, ".", *staticEnv); | ||
nix::Value v; | ||
e->eval(state, *env, v); | ||
|
||
state.forceValueDeep(v); | ||
|
||
nix::PathSet context; | ||
return nixToPythonObject(state, v, context); | ||
} | ||
|
||
PyObject *eval(PyObject *self, PyObject *args, PyObject *keywds) { | ||
const char *expression = nullptr; | ||
PyObject *vars = nullptr; | ||
|
||
const char *kwlist[] = {"expression", "vars", nullptr}; | ||
|
||
if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|O!", | ||
const_cast<char **>(kwlist), &expression, | ||
&PyDict_Type, &vars)) { | ||
return nullptr; | ||
} | ||
|
||
try { | ||
return _eval(expression, vars); | ||
} catch (nix::Error &e) { | ||
return PyErr_Format(NixError, "%s", e.what()); | ||
} catch (...) { | ||
return PyErr_Format(NixError, "unexpected C++ exception: '%s'", | ||
currentExceptionTypeName()); | ||
} | ||
} | ||
} // namespace pythonnix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#include <Python.h> | ||
|
||
namespace pythonnix { | ||
|
||
extern PyObject *NixError; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#include <Python.h> | ||
|
||
namespace pythonnix { | ||
|
||
PyObject *eval(PyObject *self, PyObject *args, PyObject *kwdict); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <Python.h> | ||
|
||
#include <nix/config.h> | ||
|
||
#include <eval.hh> | ||
|
||
namespace pythonnix { | ||
|
||
PyObject *nixToPythonObject(nix::EvalState &state, nix::Value &v, | ||
nix::PathSet &context); | ||
} // namespace pythonnix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <Python.h> | ||
#include <memory> | ||
|
||
namespace pythonnix { | ||
|
||
struct PyObjectDeleter { | ||
void operator()(PyObject *const obj) { Py_DECREF(obj); } | ||
}; | ||
|
||
typedef std::unique_ptr<PyObject, PyObjectDeleter> PyObjPtr; | ||
} // namespace pythonnix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <Python.h> | ||
#include <nix/config.h> | ||
|
||
#include <eval.hh> | ||
#include <optional> | ||
|
||
namespace pythonnix { | ||
|
||
nix::Value *pythonToNixValue(nix::EvalState &state, PyObject *obj); | ||
|
||
std::optional<nix::StaticEnv> pythonToNixEnv(nix::EvalState &state, | ||
PyObject *vars, nix::Env **env); | ||
} // namespace pythonnix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
src = [ | ||
'nix-to-python.cc', | ||
'python-to-nix.cc', | ||
'eval.cc', | ||
'python-module.cc', | ||
] | ||
|
||
python_mod.extension_module('nix', src, | ||
dependencies : [python_dep, nix_expr_dep], | ||
install: true, | ||
install_dir: python_mod.sysconfig_path('platlib'), | ||
cpp_args: ['-std=c++17', '-fvisibility=hidden']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <Python.h> | ||
|
||
#include "internal/errors.hh" | ||
#include "internal/nix-to-python.hh" | ||
#include "internal/ptr.hh" | ||
|
||
namespace pythonnix { | ||
|
||
PyObject *nixToPythonObject(nix::EvalState &state, nix::Value &v, | ||
nix::PathSet &context) { | ||
switch (v.type()) { | ||
case nix::nInt: | ||
return PyLong_FromLong(v.integer); | ||
|
||
case nix::nBool: | ||
if (v.boolean) { | ||
Py_RETURN_TRUE; | ||
} else { | ||
Py_RETURN_FALSE; | ||
} | ||
case nix::nString: | ||
copyContext(v, context); | ||
return PyUnicode_FromString(v.string.s); | ||
|
||
case nix::nPath: | ||
return PyUnicode_FromString(state.copyPathToStore(context, v.path).c_str()); | ||
|
||
case nix::nNull: | ||
Py_RETURN_NONE; | ||
|
||
case nix::nAttrs: { | ||
auto i = v.attrs->find(state.sOutPath); | ||
if (i == v.attrs->end()) { | ||
PyObjPtr dict(PyDict_New()); | ||
if (!dict) { | ||
return (PyObject *)nullptr; | ||
} | ||
|
||
nix::StringSet names; | ||
|
||
for (auto &j : *v.attrs) { | ||
names.insert(j.name); | ||
} | ||
for (auto &j : names) { | ||
nix::Attr &a(*v.attrs->find(state.symbols.create(j))); | ||
|
||
auto value = nixToPythonObject(state, *a.value, context); | ||
if (!value) { | ||
return nullptr; | ||
} | ||
PyDict_SetItemString(dict.get(), j.c_str(), value); | ||
} | ||
return dict.release(); | ||
} else { | ||
return nixToPythonObject(state, *i->value, context); | ||
} | ||
} | ||
|
||
case nix::nList: { | ||
PyObjPtr list(PyList_New(v.listSize())); | ||
if (!list) { | ||
return (PyObject *)nullptr; | ||
} | ||
|
||
for (unsigned int n = 0; n < v.listSize(); ++n) { | ||
auto value = nixToPythonObject(state, *v.listElems()[n], context); | ||
if (!value) { | ||
return nullptr; | ||
} | ||
PyList_SET_ITEM(list.get(), n, value); | ||
} | ||
return list.release(); | ||
} | ||
|
||
case nix::nExternal: | ||
return PyUnicode_FromString("<unevaluated>"); | ||
|
||
case nix::nThunk: | ||
return PyUnicode_FromString("<thunk>"); | ||
|
||
case nix::nFunction: | ||
return PyUnicode_FromString("<function>"); | ||
|
||
case nix::nFloat: | ||
return PyFloat_FromDouble(v.fpoint); | ||
|
||
default: | ||
PyErr_Format(NixError, "cannot convert nix type '%s' to a python object", | ||
showType(v).c_str()); | ||
return nullptr; | ||
} | ||
} | ||
} // namespace pythonnix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <Python.h> | ||
|
||
#include "internal/eval.hh" | ||
#include "internal/ptr.hh" | ||
|
||
#include <nix/config.h> | ||
|
||
#include <eval.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) { | ||
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 |
Oops, something went wrong.