Skip to content

Commit c116fe7

Browse files
committed
Limit circular ref check to list, tuple, dict
1 parent f9394b9 commit c116fe7

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

include/pybind11_json/pybind11_json.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ namespace pyjson
6868
}
6969
}
7070

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

79+
inline nl::json to_json(const py::handle& obj, std::set<const PyObject*>& refs)
80+
{
7881
if (obj.ptr() == nullptr || obj.is_none())
7982
{
8083
return nullptr;
@@ -124,6 +127,7 @@ namespace pyjson
124127
}
125128
if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj))
126129
{
130+
check_circular_ref(obj, refs);
127131
auto out = nl::json::array();
128132
for (const py::handle value : obj)
129133
{
@@ -133,6 +137,7 @@ namespace pyjson
133137
}
134138
if (py::isinstance<py::dict>(obj))
135139
{
140+
check_circular_ref(obj, refs);
136141
auto out = nl::json::object();
137142
for (const py::handle key : obj)
138143
{

0 commit comments

Comments
 (0)