From 90ecfbed4a670cabcb53a36bbfc1da05e7864abb Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Mon, 30 Jan 2023 23:12:49 +0100 Subject: [PATCH] Update to Nix 2.13.2 --- default.nix | 6 +++++- src/eval.cc | 5 ++++- src/nix-to-python.cc | 18 +++++++----------- src/python-to-nix.cc | 29 +++++++++++++++-------------- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/default.nix b/default.nix index 23a194b..fa27d1c 100644 --- a/default.nix +++ b/default.nix @@ -1,5 +1,9 @@ { - pkgs ? (import {}).pkgs + nixpkgs ? fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/1badc6db75d797f53c77d18d89c4eb8616d205cc.tar.gz"; + sha256 = "0rwrlfgwhb839r1vs08vbs80l99c2m7n7vvjb80kihvb3fy10wkb"; + }, + pkgs ? (import nixpkgs {}).pkgs }: with pkgs; diff --git a/src/eval.cc b/src/eval.cc index b3e5af1..8fe5818 100644 --- a/src/eval.cc +++ b/src/eval.cc @@ -25,8 +25,9 @@ static PyObject *_eval(const char *expression, PyObject *vars) { if (!staticEnv) { return nullptr; } + auto staticEnvPointer = std::make_shared(*staticEnv); - auto e = state.parseExprFromString(expression, ".", *staticEnv); + auto e = state.parseExprFromString(expression, ".", staticEnvPointer); nix::Value v; e->eval(state, *env, v); @@ -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!", diff --git a/src/nix-to-python.cc b/src/nix-to-python.cc index 399f8d6..63793f5 100644 --- a/src/nix-to-python.cc +++ b/src/nix-to-python.cc @@ -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; @@ -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 { diff --git a/src/python-to-nix.cc b/src/python-to-nix.cc index f79134d..f6326ee 100644 --- a/src/python-to-nix.cc +++ b/src/python-to-nix.cc @@ -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); @@ -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) { @@ -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) { @@ -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; } @@ -133,7 +134,7 @@ std::optional 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; @@ -150,7 +151,7 @@ std::optional 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; }