How to solve circular reference detected How to solve circular reference detected json json

How to solve circular reference detected


I tried to reproduce your problem, because I planned to switch from json config to toml config too. I have installed toml version 0.10.1.

There is a small problem in the data (# comments after the "postgresql" key), but that section can be deleted entirely.

I could minimize the example to:

data = {         "network_server":{                "downlink_data_delay":{},                "gateway":{"key3":{"key4":{}}                }           }   }import tomlprint(toml.dumps(data)) # ValueError: Circular reference detected

But the weird thing is the error goes away when you keep the structure, but slightly rename the keys!!

Just delete the first 'n' from the "network_server" and you'll get na output!

That must be a bug. Very sad.


Update: After looking at the source, the bug is quite obvious.

retval = ""if encoder is None:    encoder = TomlEncoder(o.__class__)addtoretval, sections = encoder.dump_sections(o, "")retval += addtoretvalouter_objs = [id(o)]while sections:    section_ids = [id(section) for section in sections]    for outer_obj in outer_objs:        if outer_obj in section_ids:            raise ValueError("Circular reference detected")    outer_objs += section_ids    newsections = encoder.get_empty_table()    for section in sections:        addtoretval, addtosections = encoder.dump_sections(            sections[section], section)        if addtoretval or (not addtoretval and not addtosections):            if retval and retval[-2:] != "\n\n":                retval += "\n"            retval += "[" + section + "]\n"            if addtoretval:                retval += addtoretval        for s in addtosections:            newsections[section + "." + s] = addtosections[s]    sections = newsectionsreturn retval

Or isn't it? OK, it tracks the ids of sections, but the sections are not preserved between loop iterations. When ids get reused, the program is confused.

Quick & dirty fix: patch the toml/encoder.py file, in the dumps function:

  1. add allsections=[] before the while loop

  2. add one line here:

         allsections += sections   # <---     sections = newsections return retval

The purpose is to prevent the garbage collection of unused sections, in order to keep their ids in use.

Feel free to report the issue to the author at github.