Answer product-owner round 1: 0 🔴, 1 🟡 (0 misread); read -m and --memory as a limit only with a size, so python -m pytest in the container is no limit, and read timeout as a launcher; also take the technical principles' nits: --help counts only beside docker's subcommand, sort STARTS_CONTAINER, print the example limit once, and name item 227 in the decision

This commit is contained in:
2026-10-24 00:36:19 +02:00
parent 500e2c2947
commit 2d219ff6c8
5 changed files with 23 additions and 9 deletions
+18 -4
View File
@@ -6,9 +6,11 @@ from shell_command import launched
# The options of docker, docker buildx and docker compose that take a value, so the value is not read as a subcommand.
DOCKER_VALUED = frozenset({"--ansi", "--builder", "--config", "--context", "--env-file", "--file", "--host", "--log-level", "--parallel", "--profile", "--progress", "--project-directory", "--project-name", "--tlscacert", "--tlscert", "--tlskey", "-c", "-f", "-H", "-l", "-p"})
# The subcommands that start a container, which takes the memory limit docker run's -m sets.
STARTS_CONTAINER = (["run"], ["create"], ["container", "run"], ["container", "create"])
STARTS_CONTAINER = (["container", "create"], ["container", "run"], ["create"], ["run"])
# A size as docker reads one, such as 4g, 512M or 1.5GiB: the pattern go-units' RAMInBytes parses.
SIZE = r"\d+(?:\.\d+)*[kKmMgGtTpP]?[iI]?[bB]?"
# -m4g is -m with its value attached; --memory-swap alone sets no limit.
MEMORY_LIMIT = re.compile(r"-m.*|--memory(=.*)?")
ATTACHED_LIMIT = re.compile(rf"(?:-m|--memory=){SIZE}")
def docker_subcommand(arguments: list[str]) -> list[str]:
@@ -30,12 +32,24 @@ def docker_subcommand(arguments: list[str]) -> list[str]:
def unlimited(arguments: list[str]) -> bool:
"""Whether docker's arguments start a container, with docker run or docker create, and set it no memory limit.
A --memory among the container's own arguments, past its image, reads as the limit too: telling it apart needs every value-taking option of docker run.
A -m or --memory with a size among the container's own arguments, past its image, reads as the limit too: telling it apart needs every value-taking option of docker run. Taking only a size keeps python -m pytest from reading as one.
"""
return "--help" not in arguments and docker_subcommand(arguments) in STARTS_CONTAINER and not any(MEMORY_LIMIT.fullmatch(word) for word in arguments)
subcommand = docker_subcommand(arguments)
return subcommand in STARTS_CONTAINER and not limited(arguments) and not helped(arguments, subcommand)
def limited(arguments: list[str]) -> bool:
"""Whether the arguments carry a memory limit: -m or --memory with a size, as its next word or attached."""
return any(ATTACHED_LIMIT.fullmatch(word) or (word in ("-m", "--memory") and re.fullmatch(SIZE, value)) for word, value in zip(arguments, [*arguments[1:], ""]))
def unlimited_container(words: list[str]) -> bool:
"""Whether the simple command, directly or through a launcher, starts a container that has no memory limit."""
docker = launched(words, {"docker"})
return docker is not None and unlimited(docker.arguments)
def helped(arguments: list[str], subcommand: list[str]) -> bool:
"""Whether docker only prints its help: a --help before the subcommand or as the first word after it, not one the container's own command takes."""
after = arguments.index(subcommand[-1]) + 1
return "--help" in arguments[:after + 1]