Skip to content

Commit 28707b1

Browse files
authored
Consistent use of ValueError when parsing settings. NFC (#21245)
This is in preparation for a change to avoid using json parsing for settings by default. It also adds more context to the errors since when the ValueError is caught higher up we print nice error messages.
1 parent 380a9dd commit 28707b1

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

emcc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,8 @@ def parse_string_value(text):
14571457
first = text[0]
14581458
if first == "'" or first == '"':
14591459
text = text.rstrip()
1460-
assert text[-1] == text[0] and len(text) > 1, 'unclosed opened quoted string. expected final character to be "%s" and length to be greater than 1 in "%s"' % (text[0], text)
1460+
if text[-1] != text[0] or len(text) < 2:
1461+
raise ValueError(f'unclosed quoted string. expected final character to be "{text[0]}" and length to be greater than 1 in "{text[0]}"')
14611462
return text[1:-1]
14621463
return text
14631464

@@ -1469,15 +1470,15 @@ def parse_string_list_members(text):
14691470
while True:
14701471
current = values[index].lstrip() # Cannot safely rstrip for cases like: "HERE-> ,"
14711472
if not len(current):
1472-
exit_with_error('string array should not contain an empty value')
1473+
raise ValueError('empty value in string list')
14731474
first = current[0]
14741475
if not (first == "'" or first == '"'):
14751476
result.append(current.rstrip())
14761477
else:
14771478
start = index
14781479
while True: # Continue until closing quote found
14791480
if index >= len(values):
1480-
exit_with_error("unclosed quoted string. expected final character to be '%s' in '%s'" % (first, values[start]))
1481+
raise ValueError(f"unclosed quoted string. expected final character to be '{first}' in '{values[start]}'")
14811482
new = values[index].rstrip()
14821483
if new and new[-1] == first:
14831484
if start == index:
@@ -1498,7 +1499,7 @@ def parse_string_list(text):
14981499
text = text.rstrip()
14991500
if text and text[0] == '[':
15001501
if text[-1] != ']':
1501-
exit_with_error('unclosed opened string list. expected final character to be "]" in "%s"' % (text))
1502+
raise ValueError('unterminated string list. expected final character to be "]"')
15021503
text = text[1:-1]
15031504
if text.strip() == "":
15041505
return []

test/test_other.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7663,19 +7663,22 @@ def test_dash_s_unclosed_quote(self):
76637663
# Unclosed quote
76647664
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-s', "TEST_KEY='MISSING_QUOTE"])
76657665
self.assertNotContained('AssertionError', err) # Do not mention that it is an assertion error
7666-
self.assertContained('unclosed opened quoted string. expected final character to be "\'"', err)
7666+
self.assertContained('error: error parsing "-s" setting', err)
7667+
self.assertContained('unclosed quoted string. expected final character to be "\'"', err)
76677668

76687669
def test_dash_s_single_quote(self):
76697670
# Only one quote
76707671
err = self.expect_fail([EMCC, test_file('hello_world.c'), "-sTEST_KEY='"])
76717672
self.assertNotContained('AssertionError', err) # Do not mention that it is an assertion error
7672-
self.assertContained('unclosed opened quoted string.', err)
7673+
self.assertContained('error: error parsing "-s" setting', err)
7674+
self.assertContained('unclosed quoted string.', err)
76737675

76747676
def test_dash_s_unclosed_list(self):
76757677
# Unclosed list
76767678
err = self.expect_fail([EMCC, test_file('hello_world.cpp'), "-sTEST_KEY=[Value1, Value2"])
76777679
self.assertNotContained('AssertionError', err) # Do not mention that it is an assertion error
7678-
self.assertContained('unclosed opened string list. expected final character to be "]"', err)
7680+
self.assertContained('error: error parsing "-s" setting', err)
7681+
self.assertContained('unterminated string list. expected final character to be "]"', err)
76797682

76807683
def test_dash_s_valid_list(self):
76817684
err = self.expect_fail([EMCC, test_file('hello_world.cpp'), "-sTEST_KEY=[Value1, \"Value2\"]"])

0 commit comments

Comments
 (0)