Skip to content

Commit

Permalink
Update to Nix 2.13.2
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed Jan 30, 2023
1 parent fbc8490 commit 90ecfbe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
6 changes: 5 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
pkgs ? (import <nixpkgs> {}).pkgs
nixpkgs ? fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/1badc6db75d797f53c77d18d89c4eb8616d205cc.tar.gz";
sha256 = "0rwrlfgwhb839r1vs08vbs80l99c2m7n7vvjb80kihvb3fy10wkb";
},
pkgs ? (import nixpkgs {}).pkgs
}:

with pkgs;
Expand Down
5 changes: 4 additions & 1 deletion src/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ static PyObject *_eval(const char *expression, PyObject *vars) {
if (!staticEnv) {
return nullptr;
}
auto staticEnvPointer = std::make_shared<nix::StaticEnv>(*staticEnv);

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

Expand All @@ -40,6 +41,8 @@ PyObject *eval(PyObject *self, PyObject *args, PyObject *keywds) {
const char *expression = nullptr;
PyObject *vars = nullptr;

nix::initLibStore();

const char *kwlist[] = {"expression", "vars", nullptr};

if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|O!",
Expand Down
18 changes: 7 additions & 11 deletions src/nix-to-python.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ PyObject *nixToPythonObject(nix::EvalState &state, nix::Value &v,
copyContext(v, context);
return PyUnicode_FromString(v.string.s);

case nix::nPath:
return PyUnicode_FromString(state.copyPathToStore(context, v.path).c_str());
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;
Expand All @@ -36,19 +38,13 @@ PyObject *nixToPythonObject(nix::EvalState &state, nix::Value &v,
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);
const std::string & name = state.symbols[j.name];
auto value = nixToPythonObject(state, *j.value, context);
if (!value) {
return nullptr;
}
PyDict_SetItemString(dict.get(), j.c_str(), value);
PyDict_SetItemString(dict.get(), name.c_str(), value);
}
return dict.release();
} else {
Expand Down
29 changes: 15 additions & 14 deletions src/python-to-nix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ nix::Value *pythonToNixValue(nix::EvalState &state, PyObject *obj) {
auto v = state.allocValue();

if (obj == Py_True && obj == Py_False) {
nix::mkBool(*v, obj == Py_True);
v->mkBool(obj == Py_True);
} else if (obj == Py_None) {
nix::mkNull(*v);
v->mkNull();
} else if (PyBytes_Check(obj)) {
auto str = checkNullByte(PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj));
if (!str) {
return nullptr;
}

nix::mkString(*v, str);
v->mkString(str);
} else if (PyUnicode_Check(obj)) {
Py_ssize_t size;
const char *utf8 = PyUnicode_AsUTF8AndSize(obj, &size);
Expand All @@ -88,13 +88,13 @@ nix::Value *pythonToNixValue(nix::EvalState &state, PyObject *obj) {
return nullptr;
}

nix::mkString(*v, utf8);
v->mkString(utf8);
} else if (PyFloat_Check(obj)) {
nix::mkFloat(*v, PyFloat_AS_DOUBLE(obj));
v->mkFloat(PyFloat_AS_DOUBLE(obj));
} else if (PyLong_Check(obj)) {
nix::mkInt(*v, PyLong_AsLong(obj));
v->mkInt(PyLong_AsLong(obj));
} else if (PyList_Check(obj)) {
state.mkList(*v, PyList_GET_SIZE(obj));
v->mkList(PyList_GET_SIZE(obj));
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(obj); i++) {
auto val = pythonToNixValue(state, PyList_GET_ITEM(obj, i));
if (!val) {
Expand All @@ -103,7 +103,7 @@ nix::Value *pythonToNixValue(nix::EvalState &state, PyObject *obj) {
v->listElems()[i] = val;
}
} else if (PyTuple_Check(obj)) {
state.mkList(*v, PyTuple_GET_SIZE(obj));
v->mkList(PyTuple_GET_SIZE(obj));
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(obj); i++) {
auto val = pythonToNixValue(state, PyTuple_GET_ITEM(obj, i));
if (!val) {
Expand All @@ -116,11 +116,12 @@ nix::Value *pythonToNixValue(nix::EvalState &state, PyObject *obj) {
if (!attrs) {
return nullptr;
}
state.mkAttrs(*v, attrs->size());
for (auto &attr : *attrs) {
v->attrs->push_back(nix::Attr(attr.first, attr.second));
auto attrsValue = attrs.value();
auto bindings = state.buildBindings(attrsValue.size());
for (auto &attr : attrsValue) {
bindings.insert(attr.first, attr.second);
}
v->attrs->sort();
v->mkAttrs(bindings);
}
return v;
}
Expand All @@ -133,7 +134,7 @@ std::optional<nix::StaticEnv> pythonToNixEnv(nix::EvalState &state,
*env = &state.allocEnv(vars ? PyDict_Size(vars) : 0);
(*env)->up = &state.baseEnv;

nix::StaticEnv staticEnv(false, &state.staticBaseEnv);
nix::StaticEnv staticEnv(false, state.staticBaseEnv.get());

if (!vars) {
return staticEnv;
Expand All @@ -150,7 +151,7 @@ std::optional<nix::StaticEnv> pythonToNixEnv(nix::EvalState &state,
if (!attrVal) {
return {};
}
staticEnv.vars[state.symbols.create(name)] = displ;
staticEnv.vars.emplace_back(state.symbols.create(name), displ);
(*env)->values[displ++] = attrVal;
}

Expand Down

0 comments on commit 90ecfbe

Please sign in to comment.