Tcl Library Source Code

View Ticket
Login
Ticket UUID: 8659a16544549c77b75eb624d109985576af7cb3
Title: Support for !!float tag
Type: Patch Version: 0.3.10
Submitter: peterlewerin Created on: 2017-10-20 08:29:57
Subsystem: yaml Assigned To: aku
Priority: 7 High Severity: Minor
Status: Closed Last Modified: 2017-10-31 20:25:04
Resolution: Accepted Closed By: aku
    Closed on: 2017-10-31 20:25:04
Description:
If YAML source contains the (standard) !!float tag, yaml fails with the message
 
The "!!float" handle wasn't declared.
 
In CHANGES, support for !!float is mentioned (0.2.2), but there is no composer for it. The above problem can easily be kludged away by stripping !!float tags, but it seems more correct to add support for them. Code and test diffs below.
 
Possible problem with this patch: the decision how to handle values that can't be converted to float using expr {double($value)} is above me. For the purposes of this patch my decision is to let the Tcl exception remain unhandled, so for the value 123_0 the error message is
 
expected floating-point number but got "123_0"
 
While this side-steps the error handling in yaml, it is still a clear and useful message and keeps the patch simple. The alternative is of course to catch it and rethrow using the module's error handling, but that is a bigger rewrite.
 
Code (yaml.tcl):
 
50d49
<             !!float ::yaml::_composeFloat
249,252d247
< }
< 
< proc ::yaml::_composeFloat {value} {
<     return [list !!float [expr {double($value)}]]
 
Test (yaml.test):
 
769,785d768
< test yaml-21.1 "explicit_tags float" -body {
<     set y {!!float 123}
<     yaml::yaml2dict $y
< } -result {123.0}
< 
< 
< test yaml-21.2 "explicit_tags float" -body {
<     set y {!!float 123.0}
<     yaml::yaml2dict $y
< } -result {123.0}
< 
< 
< test yaml-21.2 "explicit_tags float" -body {
<     set y {!!float 123_0}
<     yaml::yaml2dict $y
< } -result {expected floating-point number but got "123_0"} -returnCodes error
<
User Comments: aku added on 2017-10-31 20:25:04:
For the future, please use unified diffs, i.e `diff -u`.
These are easier to apply.

Fixed with commit [5006d62d3e].

Bumped version to 0.4.