Skip to content

Commit

Permalink
Fix #430: list index out of range (#431)
Browse files Browse the repository at this point in the history
Co-authored-by: Bohdan Vanieiev <bohdan.vanieiev@aptiv.com>
  • Loading branch information
Warchant and Bohdan Vanieiev authored Oct 8, 2023
1 parent 7ffcfa3 commit 06f54ed
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache

# Translations
*.mo
Expand Down Expand Up @@ -112,3 +113,6 @@ venv.bak/
auto-save-list
tramp
.\#*

# vscode
.vscode
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
TEST_DICT = {"a": {"b": 1, "c": 2}}


def test_bug_430():
# https://github.com/uiri/toml/issues/430 - IndexError
with pytest.raises(toml.TomlDecodeError, match="Key name found without value."):
toml.loads('\x00\r')


def test_bug_148():
assert 'a = "\\u0064"\n' == toml.dumps({'a': '\\x64'})
assert 'a = "\\\\x64"\n' == toml.dumps({'a': '\\\\x64'})
Expand Down
2 changes: 1 addition & 1 deletion toml/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def loads(s, _dict=dict, decoder=None):
line_no = 1

for i, item in enumerate(sl):
if item == '\r' and sl[i + 1] == '\n':
if item == '\r' and len(sl) > (i + 1) and sl[i + 1] == '\n':
sl[i] = ' '
continue
if keyname:
Expand Down
2 changes: 1 addition & 1 deletion toml/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def dump_inline_table(self, section):

def dump_value(self, v):
# Lookup function corresponding to v's type
dump_fn = next(f for t, f in self.dump_funcs.items() if isinstance(v, t), None)
dump_fn = next((f for t, f in self.dump_funcs.items() if isinstance(v, t)), None)
if dump_fn is None and hasattr(v, '__iter__'):
dump_fn = self.dump_funcs[list]
# Evaluate function (if it exists) else return v
Expand Down

0 comments on commit 06f54ed

Please sign in to comment.