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

updated to use regex to split on commas in array literals (#322) #327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def test_bug_196():
assert round_trip_bug_dict == bug_dict
assert round_trip_bug_dict['x'] == bug_dict['x']

def test_bug_322():
toml_string = """
a = [',', '']
"""
decoded = toml.loads(toml_string)['a']
assert decoded == [',', '']

def test_valid_tests():
valid_dir = "toml-test/tests/valid/"
Expand Down
7 changes: 6 additions & 1 deletion toml/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,19 @@ def _load_array_isstrarray(self, a):
return True
return False

def _split_array(self, a):
import re
PATTERN = re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
return PATTERN.split(a)[1::2]

def load_array(self, a):
atype = None
retval = []
a = a.strip()
if '[' not in a[1:-1] or "" != a[1:-1].split('[')[0].strip():
strarray = self._load_array_isstrarray(a)
if not a[1:-1].strip().startswith('{'):
a = a[1:-1].split(',')
a = self._split_array(a[1:-1])
else:
# a is an inline object, we must find the matching parenthesis
# to define groups
Expand Down