pyyaml icon indicating copy to clipboard operation
pyyaml copied to clipboard

Octal numbers processed as strings

Open ioggstream opened this issue 3 years ago • 4 comments

I expect

  • octal numbers processed as integers
yaml.safe_load('a: 0o1') == {"a": 1}

instead

yaml.safe_load('a: 0o1') == {"a": "0o1"}

ioggstream avatar Jan 26 '22 18:01 ioggstream

PyYAML only implements YAML 1.1 so far. In YAML 1.1, octal numbers just have a leading zero, e.g. 0755. In YAML 1.2 Core Schema, they start with 0o, e.g. 0o755. See https://perlpunk.github.io/yaml-test-schema/schemas.html

There are plans to support YAML 1.2 in PyYAML, and the first steps have been made. See https://github.com/yaml/pyyaml/issues/486 There are open questions about the API, the technical part is practically done.

perlpunk avatar Jan 26 '22 19:01 perlpunk

Ok, but shouldn't the following raise?

In [12]: yaml.safe_load("""
    ...: %YAML 1.2
    ...: ---
    ...: a: 0o12
    ...: """)
Out[12]: {'a': '0o12'}

ioggstream avatar Jan 26 '22 19:01 ioggstream

it should, but currently it allows many values for the directive without changing behaviour or reising.

perlpunk avatar Jan 26 '22 19:01 perlpunk