toml icon indicating copy to clipboard operation
toml copied to clipboard

Comments without a parent throw an Exception

Open amunchet opened this issue 2 years ago • 4 comments

Example TOML:

# Start of TOML file - invalid comment
[[test]]
    round = true
>>> a = """
... # Start of TOML file - invalid comment
... [[test]]
...     round = true
... """
>>> toml.loads(a, decoder=toml.TomlPreserveCommentDecoder())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\amunchet\AppData\Local\Programs\Python\Python38-32\lib\site-packages\toml\decoder.py", line 374, in loads
    decoder.embed_comments(idx, currentlevel)
  File "C:\Users\amunchet\AppData\Local\Programs\Python\Python38-32\lib\site-packages\toml\decoder.py", line 1056, in embed_comments  
    currentlevel[key] = CommentValue(currentlevel[key], comment, beginline,
KeyError: ''

Edit - this is specifically with decoder=toml.TomlPreserveCommentDecoder()

I will open a PR to fix.

amunchet avatar Jul 08 '21 19:07 amunchet

Which version of the toml library are you using? I have CPython 3.9.4 and toml 0.10.2, and your TOML example loads without errors on my system.

>>> s=r'''# Start of TOML file - invalid comment
... [[test]]
...     round = true'''
>>> import toml
>>> d=toml.loads(s)
>>> d
{'test': [{'round': True}]}

eksortso avatar Jul 09 '21 15:07 eksortso

My case deals specifically with comments - include decoder=toml.TomlPreserveCommentDecoder() to receive the error. I edited my issue to clarify.

>>> toml.__version__
'0.10.2'

amunchet avatar Jul 09 '21 16:07 amunchet

(can confirm as well, with the PreserveComment enco/decoder)

wimax-grapl avatar Aug 04 '21 22:08 wimax-grapl

would also very much appreciate if the issue gets resolved soon (considering there seem already to be a PR pending)

pedohorse avatar Aug 28 '21 12:08 pedohorse