Skip to content

Commit

Permalink
Respond to maintainer feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jmknoble committed Dec 11, 2024
1 parent 4cb3eed commit 3f84294
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
30 changes: 13 additions & 17 deletions tests/rules/test_quoted_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from tests.common import RuleTestCase
from yaml.reader import ReaderError as yaml_reader_ReaderError
import yaml.reader

from yamllint import config

Expand Down Expand Up @@ -479,24 +479,20 @@ def test_only_when_needed_extras(self):
def test_only_when_needed_special_characters(self):
conf = 'quoted-strings: {required: only-when-needed}\n'
self.check('---\n'
# double-quoted escaped special chars: ok
'k1: "\\u001b"\n',
conf)

def test_only_when_needed_special_characters_exceptions(self):
conf = 'quoted-strings: {required: only-when-needed}\n'
yamltext1 = ('---\n'
# double-quoted unescaped special chars: yuck"
'k1: "\u001b"\n')
yamltext2 = ('---\n'
# single-quoted unescaped special chars: yuck"
"k1: '\u001b'\n")
yamltext3 = ('---\n'
# unquoted unescaped special chars: yuck"
'k1: \u001b\n')
self.assertRaises(yaml_reader_ReaderError, self.check, yamltext1, conf)
self.assertRaises(yaml_reader_ReaderError, self.check, yamltext2, conf)
self.assertRaises(yaml_reader_ReaderError, self.check, yamltext3, conf)
self.assertRaises(yaml.reader.ReaderError, self.check,
'---\n'
'k1: "\u001b"\n',
conf)
self.assertRaises(yaml.reader.ReaderError, self.check,
'---\n'
"k1: '\u001b'\n",
conf)
self.assertRaises(yaml.reader.ReaderError, self.check,
'---\n'
'k1: \u001b\n',
conf)

def test_octal_values(self):
conf = 'quoted-strings: {required: true}\n'
Expand Down
13 changes: 6 additions & 7 deletions yamllint/rules/quoted_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,15 @@ def _quotes_are_needed(string, style, is_inside_a_flow):
if is_inside_a_flow and set(string) & {',', '[', ']', '{', '}'}:
return True

try:
loader = yaml.BaseLoader('key: ' + string)
except yaml.reader.ReaderError as e:
if e.reason == "special characters are not allowed" and style == '"':
if style == '"':
try:
yaml.reader.Reader('').check_printable('key: ' + string)
except yaml.reader.ReaderError:
# Special characters in a double-quoted string
# are assumed to be backslash-escaped
# are assumed to have been backslash-escaped
return True
else:
raise

loader = yaml.BaseLoader('key: ' + string)
# Remove the 5 first tokens corresponding to 'key: ' (StreamStartToken,
# BlockMappingStartToken, KeyToken, ScalarToken(value=key), ValueToken)
for _ in range(5):
Expand Down

0 comments on commit 3f84294

Please sign in to comment.