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

Comments without a parent throw an Exception #371

Open
amunchet opened this issue Jul 8, 2021 · 4 comments
Open

Comments without a parent throw an Exception #371

amunchet opened this issue Jul 8, 2021 · 4 comments
Labels
component: decoder Related to parsing in `toml.load` syntax: comments Related to comments type: bug A confirmed bug or unintended behavior

Comments

@amunchet
Copy link

amunchet commented Jul 8, 2021

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.

@eksortso
Copy link

eksortso commented Jul 9, 2021

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}]}

@amunchet
Copy link
Author

amunchet commented Jul 9, 2021

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'

@wimax-grapl
Copy link

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

@pedohorse
Copy link

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

@pradyunsg pradyunsg added type: bug A confirmed bug or unintended behavior component: decoder Related to parsing in `toml.load` syntax: comments Related to comments labels Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: decoder Related to parsing in `toml.load` syntax: comments Related to comments type: bug A confirmed bug or unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants