Fix #430: list index out of range (#431)
Co-authored-by: Bohdan Vanieiev <bohdan.vanieiev@aptiv.com>
diff --git a/.gitignore b/.gitignore
index 9f0b57d..83c2e1d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -45,6 +45,7 @@
coverage.xml
*.cover
.hypothesis/
+.pytest_cache
# Translations
*.mo
@@ -112,3 +113,6 @@
auto-save-list
tramp
.\#*
+
+# vscode
+.vscode
diff --git a/tests/test_api.py b/tests/test_api.py
index 1acc26f..ceb7ef4 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -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'})
diff --git a/toml/decoder.py b/toml/decoder.py
index bf400e9..6f23ec1 100644
--- a/toml/decoder.py
+++ b/toml/decoder.py
@@ -204,7 +204,7 @@
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:
diff --git a/toml/encoder.py b/toml/encoder.py
index 00da6dd..b3a3c14 100644
--- a/toml/encoder.py
+++ b/toml/encoder.py
@@ -173,7 +173,7 @@
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