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

Empty table name after a non-empty one results in: UnboundLocalError: local variable 'table_name' referenced before assignment #117

Closed
koterpillar opened this issue Mar 31, 2021 · 2 comments · Fixed by #122

Comments

@koterpillar
Copy link

koterpillar commented Mar 31, 2021

The following file:

[one]

[]

crashes the parser:

In [1]: import tomlkit

In [2]: tomlkit.loads("[one]\n\n[]")
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-2-f7be5b211081> in <module>
----> 1 tomlkit.loads("[one]\n\n[]")

~/Projects/tomlkit/tomlkit/api.py in loads(string)
     32     Alias for parse().
     33     """
---> 34     return parse(string)
     35
     36

~/Projects/tomlkit/tomlkit/api.py in parse(string)
     49     Parses a string into a TOMLDocument.
     50     """
---> 51     return Parser(string).parse()
     52
     53

~/Projects/tomlkit/tomlkit/parser.py in parse(self)
    158
    159         while not self.end():
--> 160             key, value = self._parse_table()
    161             if isinstance(value, Table) and value.is_aot_element():
    162                 # This is just the first table in an AoT. Parse the rest of the array

~/Projects/tomlkit/tomlkit/parser.py in _parse_table(self, parent_name, parent)
   1160             else:
   1161                 if self._current == "[":
-> 1162                     is_aot_next, name_next = self._peek_table()
   1163
   1164                     if self._is_child(name, name_next):

~/Projects/tomlkit/tomlkit/parser.py in _peek_table(self)
   1221                 table_name = self.extract()
   1222
-> 1223             return is_aot, table_name
   1224
   1225     def _parse_aot(self, first, name_first):  # type: (Table, str) -> AoT

UnboundLocalError: local variable 'table_name' referenced before assignment

An empty table name by itself results in expected EmptyTableNameError.

@koterpillar
Copy link
Author

Tomlkit version: 0.7.0
Python version: 3.9.2

@frostming
Copy link
Contributor

I've created a fork of tomlkit v0.7.0 with the issue fixed, try it:

GitHub: https://github.com/frostming/atoml
PyPI: https://pypi.org/project/atoml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants