Skip to content

Commit

Permalink
Drop YAML 1.1 boolean and timestamp resolvers
Browse files Browse the repository at this point in the history
Fixes #148
  • Loading branch information
kislyuk committed Jul 3, 2022
1 parent a7fe04d commit a8eb2ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ See the `jq manual <https://stedolan.github.io/jq/manual/>`_ for more details on

Because YAML treats JSON as a dialect of YAML, you can use yq to convert JSON to YAML: ``yq -y . < in.json > out.yml``.

yq aims to use YAML 1.2 as the language specification, but due to limitations in PyYAML we still parse int and float
literals according to the YAML 1.1 specification.

Preserving tags and styles using the ``-Y`` (``--yaml-roundtrip``) option
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
12 changes: 6 additions & 6 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,9 @@ def test_multidocs(self):

def test_datetimes(self):
self.assertEqual(self.run_yq("- 2016-12-20T22:07:36Z\n", ["."]), "")
if yaml.__version__ < '5.3':
self.assertEqual(self.run_yq("- 2016-12-20T22:07:36Z\n", ["-y", "."]), "- '2016-12-20T22:07:36'\n")
else:
self.assertEqual(self.run_yq("- 2016-12-20T22:07:36Z\n", ["-y", "."]), "- '2016-12-20T22:07:36+00:00'\n")

self.assertEqual(self.run_yq("- 2016-12-20T22:07:36Z\n", ["-y", "."]), "- 2016-12-20T22:07:36Z\n")
self.assertEqual(self.run_yq("2016-12-20", ["."]), "")
self.assertEqual(self.run_yq("2016-12-20", ["-y", "."]), "'2016-12-20'\n")
self.assertEqual(self.run_yq("2016-12-20", ["-y", "."]), "2016-12-20\n...\n")

def test_unrecognized_tags(self):
self.assertEqual(self.run_yq("!!foo bar\n", ["."]), "")
Expand Down Expand Up @@ -264,5 +260,9 @@ def test_yaml_type_tags(self):
"example:\n Boston Red Sox: null\n Detroit Tigers: null\n New York Yankees: null\n"
)

def test_yaml_1_2(self):
self.assertEqual(self.run_yq("on: 1", ["-y", "."]), "on: 1\n")
self.assertEqual(self.run_yq("2022-02-22", ["-y", "."]), "2022-02-22\n...\n")

if __name__ == '__main__':
unittest.main()
14 changes: 14 additions & 0 deletions yq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ def exit_handler(arg=None):

def load_yaml_docs(in_stream, out_stream, jq, loader_class, max_expansion_factor, exit_func, prog):
loader = loader_class(in_stream)

# PyYAML does not yet support YAML 1.2 (https://github.com/yaml/pyyaml/pull/555).
# Use this hack to avoid interpreting yes|Yes|YES|no|No|NO|on|On|ON|off|Off|OFF as boolean values,
# and avoid interpreting any values as timestamps.
for start_char in list(loader.yaml_implicit_resolvers):
if start_char in {"y", "Y", "n", "N", "o", "O"}:
for i, matcher in enumerate(loader.yaml_implicit_resolvers[start_char]):
if matcher[0] == "tag:yaml.org,2002:bool":
del loader.yaml_implicit_resolvers[start_char][i]
if start_char in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}:
for i, matcher in enumerate(loader.yaml_implicit_resolvers[start_char]):
if matcher[0] == "tag:yaml.org,2002:timestamp":
del loader.yaml_implicit_resolvers[start_char][i]

last_loader_pos = 0
try:
while loader.check_node():
Expand Down

0 comments on commit a8eb2ed

Please sign in to comment.