strutils - strutils.to_bytes ignores the case of units

Bug #1189635 reported by Ben Nemec
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
oslo-incubator
Fix Released
Undecided
Zhongyue Luo

Bug Description

The function strutils.to_bytes ignores the case of the unit, which means that it will return the same value for 100 MB as 100 Mb. This is not correct.

This is low priority since presumably the function works for what it is currently used for, but since it's in a general purpose library it shouldn't be assuming the caller means bytes when they might mean bits. As an added bonus, it would be nice if it supported forms like 100 MiB too, which is a different value still.

Zhongyue Luo (zyluo)
Changed in oslo:
assignee: nobody → Zhongyue Luo (zyluo)
Revision history for this message
Zhongyue Luo (zyluo) wrote :

What should the return value be for "79b"? 9 or 9.875

I guess the return value type should be float?

Revision history for this message
Ben Nemec (bnemec) wrote :

Hmm, that is an odd case for this. I'd be inclined to say return a float so we can give an accurate value. If there's an issue with doing it that way, I think it should probably always be rounded up to the next int. So 79b would return 10, to make sure that at least the bit value passed in can fit in the returned value.

Changed in oslo:
status: New → In Progress
Revision history for this message
Zhongyue Luo (zyluo) wrote :
summary: - strutils.to_bytes ignores the case of units
+ strutils - strutils.to_bytes ignores the case of units
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to oslo-incubator (master)

Reviewed: https://review.openstack.org/44652
Committed: https://git.openstack.org/cgit/openstack/oslo-incubator/commit/?id=bec3a5eb8157c0c552c6392ccf0da342ffb8da26
Submitter: Jenkins
Branch: master

commit bec3a5eb8157c0c552c6392ccf0da342ffb8da26
Author: Zhongyue Luo <email address hidden>
Date: Mon Sep 2 16:50:39 2013 +0800

    Implements SI/IEC unit system conversion to bytes

    The issues with utils.to_bytes are

    1) Ignores measure of unit and treats b and B both as Byte
    2) Cannot handle SI unit system which base is 1000 (IEC=1024)
    3) The name "to_bytes" didn't do a good job representing what the function does.
       Disscussed here,
           https://review.openstack.org/#/c/32093/2/openstack/common/strutils.py

    This patch implements "string_to_bytes". The name comes from
    "string_to_bool" in cinder/utils.py. Its merits are,

    1) Handles bit and byte units
    2) Handles SI and IEC unit systems
    3) Has a much more intuitive function name

    Closes-bug: #1189635
    Closes-bug: #1193765

    Change-Id: I5f1ce56f447179d1ef6ded7c956983151f5a8f18

Changed in oslo:
status: In Progress → Fix Committed
Changed in oslo:
milestone: none → icehouse-3
Thierry Carrez (ttx)
Changed in oslo:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in oslo:
milestone: icehouse-3 → 2014.1
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.