Skip to content

Commit eb1f24a

Browse files
committed
Merge pull request #880 from docker/813-parse-float-bytes
Handle 64-bit integer values in parse_bytes
2 parents 8c533bf + cd66f6c commit eb1f24a

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

docker/utils/utils.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ def datetime_to_timestamp(dt):
486486
return delta.seconds + delta.days * 24 * 3600
487487

488488

489+
def longint(n):
490+
if six.PY3:
491+
return int(n)
492+
return long(n)
493+
494+
489495
def parse_bytes(s):
490496
if len(s) == 0:
491497
s = 0
@@ -506,20 +512,21 @@ def parse_bytes(s):
506512

507513
if suffix in units.keys() or suffix.isdigit():
508514
try:
509-
digits = int(digits_part)
515+
digits = longint(digits_part)
510516
except ValueError:
511-
message = ('Failed converting the string value for'
512-
'memory ({0}) to a number.')
513-
formatted_message = message.format(digits_part)
514-
raise errors.DockerException(formatted_message)
517+
raise errors.DockerException(
518+
'Failed converting the string value for memory ({0}) to'
519+
' an integer.'.format(digits_part)
520+
)
515521

516-
s = digits * units[suffix]
522+
# Reconvert to long for the final result
523+
s = longint(digits * units[suffix])
517524
else:
518-
message = ('The specified value for memory'
519-
' ({0}) should specify the units. The postfix'
520-
' should be one of the `b` `k` `m` `g`'
521-
' characters')
522-
raise errors.DockerException(message.format(s))
525+
raise errors.DockerException(
526+
'The specified value for memory ({0}) should specify the'
527+
' units. The postfix should be one of the `b` `k` `m` `g`'
528+
' characters'.format(s)
529+
)
523530

524531
return s
525532

tests/unit/utils_test.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import os.path
77
import shutil
8+
import sys
89
import tarfile
910
import tempfile
1011

@@ -465,14 +466,28 @@ def test_hybrid_list(self):
465466
})
466467

467468

468-
class UtilsTest(base.BaseTestCase):
469-
longMessage = True
469+
class ParseBytesTest(base.BaseTestCase):
470+
def test_parse_bytes_valid(self):
471+
self.assertEqual(parse_bytes("512MB"), 536870912)
472+
self.assertEqual(parse_bytes("512M"), 536870912)
473+
self.assertEqual(parse_bytes("512m"), 536870912)
470474

471-
def test_parse_bytes(self):
472-
self.assertEqual(parse_bytes("512MB"), (536870912))
473-
self.assertEqual(parse_bytes("512M"), (536870912))
475+
def test_parse_bytes_invalid(self):
474476
self.assertRaises(DockerException, parse_bytes, "512MK")
475477
self.assertRaises(DockerException, parse_bytes, "512L")
478+
self.assertRaises(DockerException, parse_bytes, "127.0.0.1K")
479+
480+
def test_parse_bytes_float(self):
481+
self.assertRaises(DockerException, parse_bytes, "1.5k")
482+
483+
def test_parse_bytes_maxint(self):
484+
self.assertEqual(
485+
parse_bytes("{0}k".format(sys.maxsize)), sys.maxsize * 1024
486+
)
487+
488+
489+
class UtilsTest(base.BaseTestCase):
490+
longMessage = True
476491

477492
def test_convert_filters(self):
478493
tests = [

0 commit comments

Comments
 (0)