Skip to content

Fix Python 3 incompatibilities in bin/docker-to-squash.py - #9296

Open
ToniAdreal wants to merge 1 commit into
apache:masterfrom
ToniAdreal:fix-9291-docker-to-squash-py3
Open

ToniAdreal wants to merge 1 commit into
apache:masterfrom
ToniAdreal:fix-9291-docker-to-squash-py3

Conversation

@ToniAdreal

Copy link
Copy Markdown

Fixes #9291.

bin/docker-to-squash.py cannot run on Python 3 at all. Reproduced on Python 3.12.3 against master (4fca209):

  • python3 docker-to-squash.py --help fails immediately with ImportError: cannot import name 'Iterable' from 'collections'.
  • After fixing only that import, building the argument parser raises NameError: name 'long' is not defined for --untagged-transition-sec.
  • calculate_string_hash("x") raises TypeError: Strings must be encoded before hashing.
  • write_local_image_tag_to_hash raises AttributeError: 'dict' object has no attribute 'iteritems'.

Changes, all in bin/docker-to-squash.py:

  • import Iterable from collections.abc
  • type=long -> type=int for --untagged-transition-sec
  • encode the string as UTF-8 before hashing in calculate_string_hash
  • dict.iteritems() -> dict.items()
  • two image is not "" comparisons -> != "" (SyntaxWarning since Python 3.8)

Testing: python3 -m unittest test_docker_to_squash.py in bin/ errors on the import before this change and passes after it. --help now builds the full parser, and calculate_string_hash / write_local_image_tag_to_hash were exercised directly.

This is one of the four docker-to-squash.py fixes that #9284 left to separate PRs (the others are #9289, #9290, #9292). It is also a prerequisite for testing the rest, since the test file cannot be imported on Python 3.10+ without it.

The script cannot run on Python 3: Iterable is imported from collections (removed in 3.10), the --untagged-transition-sec argument uses type=long, calculate_string_hash passes a str to hashlib, and write_local_image_tag_to_hash uses dict.iteritems(). Import Iterable from collections.abc, use type=int, encode the string as UTF-8 before hashing, and use dict.items(). Also replace two 'is not ""' comparisons with '!=' to silence SyntaxWarning.

Signed-off-by: ToniAdreal <184365500+ToniAdreal@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docker-to-squash.py does not run on Python 3

1 participant