Skip to content

Commit

Permalink
python: Format using clang-format
Browse files Browse the repository at this point in the history
Using the configuration file from NixOS#6721 for less conflicts
  • Loading branch information
infinisil committed Feb 24, 2023
1 parent ed0329a commit d2e5e20
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 274 deletions.
80 changes: 40 additions & 40 deletions python/src/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,53 @@

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)";
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());
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 staticEnvPointer = std::make_shared<nix::StaticEnv>(*staticEnv);
nix::Env * env = nullptr;
auto staticEnv = pythonToNixEnv(state, vars, &env);
if (!staticEnv) {
return nullptr;
}
auto staticEnvPointer = std::make_shared<nix::StaticEnv>(*staticEnv);

auto e = state.parseExprFromString(expression, ".", staticEnvPointer);
nix::Value v;
e->eval(state, *env, v);
auto e = state.parseExprFromString(expression, ".", staticEnvPointer);
nix::Value v;
e->eval(state, *env, v);

state.forceValueDeep(v);
state.forceValueDeep(v);

nix::PathSet context;
return nixToPythonObject(state, v, context);
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());
}
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
2 changes: 1 addition & 1 deletion python/src/internal/errors.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

namespace pythonnix {

extern PyObject *NixError;
extern PyObject * NixError;
}
2 changes: 1 addition & 1 deletion python/src/internal/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

namespace pythonnix {

PyObject *eval(PyObject *self, PyObject *args, PyObject *kwdict);
PyObject * eval(PyObject * self, PyObject * args, PyObject * kwdict);
}
3 changes: 1 addition & 2 deletions python/src/internal/nix-to-python.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@

namespace pythonnix {

PyObject *nixToPythonObject(nix::EvalState &state, nix::Value &v,
nix::PathSet &context);
PyObject * nixToPythonObject(nix::EvalState & state, nix::Value & v, nix::PathSet & context);
} // namespace pythonnix
8 changes: 6 additions & 2 deletions python/src/internal/ptr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

namespace pythonnix {

struct PyObjectDeleter {
void operator()(PyObject *const obj) { Py_DECREF(obj); }
struct PyObjectDeleter
{
void operator()(PyObject * const obj)
{
Py_DECREF(obj);
}
};

typedef std::unique_ptr<PyObject, PyObjectDeleter> PyObjPtr;
Expand Down
5 changes: 2 additions & 3 deletions python/src/internal/python-to-nix.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace pythonnix {

nix::Value *pythonToNixValue(nix::EvalState &state, PyObject *obj);
nix::Value * pythonToNixValue(nix::EvalState & state, PyObject * obj);

std::optional<nix::StaticEnv> pythonToNixEnv(nix::EvalState &state,
PyObject *vars, nix::Env **env);
std::optional<nix::StaticEnv> pythonToNixEnv(nix::EvalState & state, PyObject * vars, nix::Env ** env);
} // namespace pythonnix
127 changes: 63 additions & 64 deletions python/src/nix-to-python.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,84 +6,83 @@

namespace pythonnix {

PyObject *nixToPythonObject(nix::EvalState &state, nix::Value &v,
nix::PathSet &context) {
switch (v.type()) {
case nix::nInt:
return PyLong_FromLong(v.integer);
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::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: {
auto p = state.copyPathToStore(context, v.path).to_string();
return PyUnicode_FromStringAndSize(p.data(), p.length());
}
case nix::nPath: {
auto p = state.copyPathToStore(context, v.path).to_string();
return PyUnicode_FromStringAndSize(p.data(), p.length());
}

case nix::nNull:
Py_RETURN_NONE;
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;
}
case nix::nAttrs: {
auto i = v.attrs->find(state.sOutPath);
if (i == v.attrs->end()) {
PyObjPtr dict(PyDict_New());
if (!dict) {
return (PyObject *) nullptr;
}

for (auto &j : *v.attrs) {
const std::string & name = state.symbols[j.name];
auto value = nixToPythonObject(state, *j.value, context);
if (!value) {
return nullptr;
for (auto & j : *v.attrs) {
const std::string & name = state.symbols[j.name];
auto value = nixToPythonObject(state, *j.value, context);
if (!value) {
return nullptr;
}
PyDict_SetItemString(dict.get(), name.c_str(), value);
}
return dict.release();
} else {
return nixToPythonObject(state, *i->value, context);
}
PyDict_SetItemString(dict.get(), name.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;
}
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);
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();
}
return list.release();
}

case nix::nExternal:
return PyUnicode_FromString("<unevaluated>");
case nix::nExternal:
return PyUnicode_FromString("<unevaluated>");

case nix::nThunk:
return PyUnicode_FromString("<thunk>");
case nix::nThunk:
return PyUnicode_FromString("<thunk>");

case nix::nFunction:
return PyUnicode_FromString("<function>");
case nix::nFunction:
return PyUnicode_FromString("<function>");

case nix::nFloat:
return PyFloat_FromDouble(v.fpoint);
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;
}
default:
PyErr_Format(NixError, "cannot convert nix type '%s' to a python object", showType(v).c_str());
return nullptr;
}
}
} // namespace pythonnix
72 changes: 36 additions & 36 deletions python/src/python-module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,49 @@ namespace pythonnix {

#define _public_ __attribute__((visibility("default")))

PyObject *NixError = nullptr;
PyObject * NixError = nullptr;

static PyMethodDef NixMethods[] = {{"eval", (PyCFunction)eval,
METH_VARARGS | METH_KEYWORDS,
"Eval nix expression"},
{NULL, NULL, 0, NULL}};
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 __build-remote";
// 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();
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 __build-remote";
// 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
Loading

0 comments on commit d2e5e20

Please sign in to comment.