Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix yaml loading versions as floating point and losing trailing zeros #2909

Merged
merged 3 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion conda_build/metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function

from collections import OrderedDict
import contextlib
import copy
import hashlib
import json
Expand Down Expand Up @@ -32,6 +33,13 @@
sys.exit('Error: could not import yaml (required to read meta.yaml '
'files of conda recipes)')

try:
loader = yaml.CLoader
except:
loader = yaml.Loader

from yaml.resolver import Resolver

on_win = (sys.platform == 'win32')

# arches that don't follow exact names in the subdir need to be mapped here
Expand Down Expand Up @@ -202,7 +210,9 @@ def select_lines(data, namespace, variants_in_place):

def yamlize(data):
try:
return yaml.load(data)
with stringify_numbers():
loaded_data = yaml.load(data, Loader=loader)
return loaded_data
except yaml.error.YAMLError as e:
if '{{' in data:
try:
Expand Down Expand Up @@ -786,6 +796,18 @@ def trim_build_only_deps(metadata, requirements_used):
return requirements_used - to_remove


@contextlib.contextmanager
def stringify_numbers():
# ensure that numbers are not interpreted as ints or floats. That trips up versions
# with trailing zeros.
implicit_resolver_backup = Resolver.yaml_implicit_resolvers.copy()
for ch in list(u'-+0123456789'):
del Resolver.yaml_implicit_resolvers[ch]
yield
for ch in list(u'-+0123456789'):
Resolver.yaml_implicit_resolvers[ch] = implicit_resolver_backup[ch]


class MetaData(object):
def __init__(self, path, config=None, variant=None):

Expand Down
5 changes: 5 additions & 0 deletions tests/test-recipes/metadata/float_misinterpretation/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# test ensuring that version numbers that look like floating point numbers are kept as strings

package:
name: test_floating_point_version
version: 1.10
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if not "%PKG_VERSION%" == "1.10" (
echo PKG_VERSION is %PKG_VERSION%; should be 1.10
exit 1
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if [ "$PKG_VERSION" != "1.10" ]; then
echo "PKG_VERSION is $PKG_VERSION, but should be 1.10"
exit 1
fi