Skip to content

Commit 2c522fb

Browse files
authored
Fix memory conversion to bytes (#2645)
* Fix memory conversion to bytes Co-authored-by: Ulysses Souza <[email protected]> Signed-off-by: aiordache <[email protected]>
1 parent 30ff9f3 commit 2c522fb

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

docker/api/container.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ def create_host_config(self, *args, **kwargs):
506506
bytes) or a string with a units identification char
507507
(``100000b``, ``1000k``, ``128m``, ``1g``). If a string is
508508
specified without a units character, bytes are assumed as an
509-
mem_reservation (int or str): Memory soft limit.
509+
mem_reservation (float or str): Memory soft limit.
510510
mem_swappiness (int): Tune a container's memory swappiness
511511
behavior. Accepts number between 0 and 100.
512512
memswap_limit (str or int): Maximum amount of memory + swap a
@@ -1219,8 +1219,8 @@ def update_container(
12191219
cpu_shares (int): CPU shares (relative weight)
12201220
cpuset_cpus (str): CPUs in which to allow execution
12211221
cpuset_mems (str): MEMs in which to allow execution
1222-
mem_limit (int or str): Memory limit
1223-
mem_reservation (int or str): Memory soft limit
1222+
mem_limit (float or str): Memory limit
1223+
mem_reservation (float or str): Memory soft limit
12241224
memswap_limit (int or str): Total memory (memory + swap), -1 to
12251225
disable swap
12261226
kernel_memory (int or str): Kernel memory limit

docker/utils/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def parse_bytes(s):
412412

413413
if suffix in units.keys() or suffix.isdigit():
414414
try:
415-
digits = int(digits_part)
415+
digits = float(digits_part)
416416
except ValueError:
417417
raise errors.DockerException(
418418
'Failed converting the string value for memory ({0}) to'

tests/unit/utils_test.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,21 @@
55
import os
66
import os.path
77
import shutil
8-
import sys
98
import tempfile
109
import unittest
1110

12-
11+
import pytest
12+
import six
1313
from docker.api.client import APIClient
1414
from docker.constants import IS_WINDOWS_PLATFORM
1515
from docker.errors import DockerException
16-
from docker.utils import (
17-
convert_filters, convert_volume_binds, decode_json_header, kwargs_from_env,
18-
parse_bytes, parse_devices, parse_env_file, parse_host,
19-
parse_repository_tag, split_command, update_headers,
20-
)
21-
16+
from docker.utils import (convert_filters, convert_volume_binds,
17+
decode_json_header, kwargs_from_env, parse_bytes,
18+
parse_devices, parse_env_file, parse_host,
19+
parse_repository_tag, split_command, update_headers)
2220
from docker.utils.ports import build_port_bindings, split_port
2321
from docker.utils.utils import format_environment
2422

25-
import pytest
26-
27-
import six
28-
2923
TEST_CERT_DIR = os.path.join(
3024
os.path.dirname(__file__),
3125
'testdata/certs',
@@ -447,11 +441,7 @@ def test_parse_bytes_invalid(self):
447441
parse_bytes("127.0.0.1K")
448442

449443
def test_parse_bytes_float(self):
450-
with pytest.raises(DockerException):
451-
parse_bytes("1.5k")
452-
453-
def test_parse_bytes_maxint(self):
454-
assert parse_bytes("{0}k".format(sys.maxsize)) == sys.maxsize * 1024
444+
assert parse_bytes("1.5k") == 1536
455445

456446

457447
class UtilsTest(unittest.TestCase):

0 commit comments

Comments
 (0)