Skip to content

Commit

Permalink
Limit circular ref check to list, tuple, dict
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Dec 9, 2024
1 parent f9394b9 commit c116fe7
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion include/pybind11_json/pybind11_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ namespace pyjson
}
}

inline nl::json to_json(const py::handle& obj, std::set<const PyObject*>& refs)
inline void check_circular_ref(const py::handle& obj, std::set<const PyObject*>& refs)
{
auto insert_ret = refs.insert(obj.ptr());
if (!insert_ret.second) {
throw std::runtime_error("Circular reference detected");
}
}

inline nl::json to_json(const py::handle& obj, std::set<const PyObject*>& refs)
{
if (obj.ptr() == nullptr || obj.is_none())
{
return nullptr;
Expand Down Expand Up @@ -124,6 +127,7 @@ namespace pyjson
}
if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj))
{
check_circular_ref(obj, refs);
auto out = nl::json::array();
for (const py::handle value : obj)
{
Expand All @@ -133,6 +137,7 @@ namespace pyjson
}
if (py::isinstance<py::dict>(obj))
{
check_circular_ref(obj, refs);
auto out = nl::json::object();
for (const py::handle key : obj)
{
Expand Down

0 comments on commit c116fe7

Please sign in to comment.