Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid crash when converting dict with circular reference #74

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions include/pybind11_json/pybind11_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef PYBIND11_JSON_HPP
#define PYBIND11_JSON_HPP

#include <set>
#include <string>
#include <vector>

Expand Down Expand Up @@ -67,7 +68,7 @@ namespace pyjson
}
}

inline nl::json to_json(const py::handle& obj)
inline nl::json to_json(const py::handle& obj, std::set<const PyObject*>& refs)
{
if (obj.ptr() == nullptr || obj.is_none())
{
Expand Down Expand Up @@ -118,24 +119,48 @@ namespace pyjson
}
if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj))
{
auto insert_ret = refs.insert(obj.ptr());
if (!insert_ret.second) {
throw std::runtime_error("Circular reference detected");
}

auto out = nl::json::array();
for (const py::handle value : obj)
{
out.push_back(to_json(value));
out.push_back(to_json(value, refs));
}

refs.erase(insert_ret.first);

return out;
}
if (py::isinstance<py::dict>(obj))
{
auto insert_ret = refs.insert(obj.ptr());
if (!insert_ret.second) {
throw std::runtime_error("Circular reference detected");
}

auto out = nl::json::object();
for (const py::handle key : obj)
{
out[py::str(key).cast<std::string>()] = to_json(obj[key]);
out[py::str(key).cast<std::string>()] = to_json(obj[key], refs);
}

refs.erase(insert_ret.first);

return out;
}

throw std::runtime_error("to_json not implemented for this type of object: " + py::repr(obj).cast<std::string>());
}

inline nl::json to_json(const py::handle& obj)
{
std::set<const PyObject*> refs;
return to_json(obj, refs);
}

}

// nlohmann_json serializers
Expand Down
19 changes: 19 additions & 0 deletions test/test_pybind11_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,22 @@ TEST(pybind11_caster_fromjson, dict)
ASSERT_EQ(j["number"].cast<int>(), 1234);
ASSERT_EQ(j["hello"].cast<std::string>(), "world");
}

TEST(pybind11_caster_tojson, recursive_dict)
{
py::scoped_interpreter guard;
py::module m = create_module("test");

m.def("to_json", &test_fromtojson);

// Simulate calling this binding from Python with a dictionary as argument
py::dict obj_inner("number"_a=1234, "hello"_a="world");
py::dict obj;
obj["first"] = obj_inner;
obj["second"] = obj_inner;

ASSERT_NO_THROW(m.attr("to_json")(obj));

obj["second"]["recur"] = obj_inner;
ASSERT_ANY_THROW(m.attr("to_json")(obj));
}
Loading