From d6dc9dcbc2662480184c60fbca30f3a69c19f9d1 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Fri, 6 Nov 2026 10:49:30 +0100 Subject: [PATCH 1/9] Repo hygiene: SECURITY.md, pinned actions, uniform project.urls (#335) * Repo hygiene: SECURITY.md, pinned actions, uniform project.urls - Add SECURITY.md pointing to GitHub private advisory form - Pin pypa/gh-action-pypi-publish to v1.14.3 SHA (was floating release/v1) - Complete [project.urls] (Repository/Documentation/Issues/Changelog/Funding) Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_013r9LX6taXi8NfwLyPQeEqf * SECURITY.md: markdown lint fixes (MD034, MD013) Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_013r9LX6taXi8NfwLyPQeEqf --------- Co-authored-by: Claude Fable 5.1 --- .github/workflows/release.yml | 2 +- SECURITY.md | 18 ++++++++++++++++++ pyproject.toml | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 SECURITY.md diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 83b75401..35364ee6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -106,4 +106,4 @@ jobs: name: dist path: dist/ - name: Publish via Trusted Publishing - uses: pypa/gh-action-pypi-publish@release/v1 + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 00000000..9fd1e6b7 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,18 @@ +# Security policy + +## Supported versions + +Security fixes land on the latest release. Earlier releases are not +maintained. + +## Reporting a vulnerability + +Report vulnerabilities privately through GitHub's advisory form: + + + +That keeps the report out of the public issue tracker until a fix is +available. If you cannot use the form, mail instead. + +Please include the progressbar2 version, the configuration that triggers the +issue and a minimal reproduction. Expect an acknowledgement within a week. \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 8b3f9d36..7f84c364 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -114,6 +114,7 @@ bugs = 'https://github.com/wolph/python-progressbar/issues' documentation = 'https://progressbar-2.readthedocs.io/en/latest/' repository = 'https://github.com/wolph/python-progressbar/' Funding = 'https://github.com/sponsors/wolph' +Changelog = 'https://github.com/wolph/python-progressbar/releases' [project.scripts] progressbar = 'progressbar.__main__:main' From b79a8575f8219f06df540fe16891b26a2e2531e8 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Sat, 7 Nov 2026 00:58:46 +0100 Subject: [PATCH 2/9] Add Dependabot config for GitHub Actions and dependencies (#337) Monthly grouped updates with a 14 day cooldown, labelled meta: CI and meta: deps, matching mt940 and portalocker. Claude-Session: https://claude.ai/code/session_013r9LX6taXi8NfwLyPQeEqf Co-authored-by: Claude Fable 5.1 --- .github/dependabot.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..fab0a58e --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,29 @@ +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + target-branch: develop + labels: + - "meta: CI" + schedule: + interval: monthly + cooldown: + default-days: 14 + groups: + actions: + patterns: + - "*" + + - package-ecosystem: pip + directory: / + target-branch: develop + labels: + - "meta: deps" + schedule: + interval: monthly + cooldown: + default-days: 14 + groups: + python: + patterns: + - "*" From 3e65cb3e25dcfd131040c3d30b2d5325a7021711 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 08:03:32 +0100 Subject: [PATCH 3/9] build(deps): bump the actions group with 5 updates (#339) Bumps the actions group with 5 updates: | Package | From | To | | --- | --- | --- | | [actions/checkout](https://github.com/actions/checkout) | `6` | `7` | | [actions/setup-python](https://github.com/actions/setup-python) | `6` | `7` | | [actions/upload-artifact](https://github.com/actions/upload-artifact) | `4` | `7` | | [actions/download-artifact](https://github.com/actions/download-artifact) | `7` | `8` | | [actions/stale](https://github.com/actions/stale) | `8` | `11` | Updates `actions/checkout` from 6 to 7 - [Release notes](https://github.com/actions/checkout/releases) - [Commits](https://github.com/actions/checkout/compare/v6...v7) Updates `actions/setup-python` from 6 to 7 - [Release notes](https://github.com/actions/setup-python/releases) - [Commits](https://github.com/actions/setup-python/compare/v6...v7) Updates `actions/upload-artifact` from 4 to 7 - [Release notes](https://github.com/actions/upload-artifact/releases) - [Commits](https://github.com/actions/upload-artifact/compare/v4...v7) Updates `actions/download-artifact` from 7 to 8 - [Release notes](https://github.com/actions/download-artifact/releases) - [Commits](https://github.com/actions/download-artifact/compare/v7...v8) Updates `actions/stale` from 8 to 11 - [Release notes](https://github.com/actions/stale/releases) - [Changelog](https://github.com/actions/stale/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/stale/compare/v8...v11) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: '7' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions - dependency-name: actions/setup-python dependency-version: '7' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions - dependency-name: actions/upload-artifact dependency-version: '7' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions - dependency-name: actions/download-artifact dependency-version: '8' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions - dependency-name: actions/stale dependency-version: '11' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/codeql.yml | 2 +- .github/workflows/docs.yml | 6 +++--- .github/workflows/main.yml | 12 ++++++------ .github/workflows/release.yml | 6 +++--- .github/workflows/stale.yml | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index ee7cae26..bd50c946 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -24,7 +24,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v6 + uses: actions/checkout@v7 - name: Initialize CodeQL uses: github/codeql-action/init@v3 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 89bac84b..65e204de 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -22,9 +22,9 @@ jobs: # minutes leaves headroom without letting a genuine hang run forever. timeout-minutes: 20 steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@v7 - name: Set up Python 3.13 - uses: actions/setup-python@v6 + uses: actions/setup-python@v7 with: python-version: '3.13' - name: Install dependencies @@ -38,7 +38,7 @@ jobs: run: python -m pytest tests/console -v --no-cov - name: Upload the built docs if: always() - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v7 with: name: docs-html path: docs/_build/html diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 10025e64..49e595d8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,9 +19,9 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@v7 - name: Set up Python 3.13 - uses: actions/setup-python@v6 + uses: actions/setup-python@v7 with: python-version: '3.13' - name: Install dependencies @@ -80,9 +80,9 @@ jobs: tox-env: codespell steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@v7 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v6 + uses: actions/setup-python@v7 with: python-version: ${{ matrix.python-version }} allow-prereleases: true @@ -122,9 +122,9 @@ jobs: # pytest's fd-level capture. PYTHONUTF8: '1' steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@v7 - name: Set up Python 3.12 - uses: actions/setup-python@v6 + uses: actions/setup-python@v7 with: python-version: '3.12' - name: Install dependencies diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 35364ee6..4d0e0011 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,7 +18,7 @@ jobs: name: Build distributions runs-on: ubuntu-latest steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@v7 with: ref: ${{ inputs.ref || github.ref }} - name: Verify the ref is a release tag matching __version__ @@ -86,7 +86,7 @@ jobs: echo "::error::distribution contains stray conflict or backup files" exit 1 fi - - uses: actions/upload-artifact@v6 + - uses: actions/upload-artifact@v7 with: name: dist path: dist/ @@ -101,7 +101,7 @@ jobs: permissions: id-token: write steps: - - uses: actions/download-artifact@v7 + - uses: actions/download-artifact@v8 with: name: dist path: dist/ diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index eb76940c..3f05c50a 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -12,7 +12,7 @@ jobs: issues: write pull-requests: write steps: - - uses: actions/stale@v8 + - uses: actions/stale@v11 with: days-before-issue-stale: 30 exempt-issue-labels: in-progress,help-wanted,pinned,security,enhancement From 16b5ad2169a017b74dfefb6b42583eb3d940e218 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 08:04:51 +0100 Subject: [PATCH 4/9] build(deps): bump the python group with 4 updates (#338) Updates the requirements on [speedups](https://github.com/WoLpH/speedups), [furo](https://github.com/pradyunsg/furo), [tqdm](https://github.com/tqdm/tqdm) and [click](https://github.com/pallets/click) to permit the latest version. Updates `speedups` from 2.3.0 to 2.3.3 - [Release notes](https://github.com/WoLpH/speedups/releases) - [Commits](https://github.com/WoLpH/speedups/compare/v2.3.0...v2.3.3) Updates `furo` to 2025.12.19 - [Release notes](https://github.com/pradyunsg/furo/releases) - [Changelog](https://github.com/pradyunsg/furo/blob/main/docs/changelog.md) - [Commits](https://github.com/pradyunsg/furo/compare/2024.08.06...2025.12.19) Updates `tqdm` from 4.68.3 to 4.70.1 - [Release notes](https://github.com/tqdm/tqdm/releases) - [Commits](https://github.com/tqdm/tqdm/compare/v4.68.3...v4.70.1) Updates `click` from 8.4.1 to 8.5.0 - [Release notes](https://github.com/pallets/click/releases) - [Changelog](https://github.com/pallets/click/blob/main/CHANGES.md) - [Commits](https://github.com/pallets/click/compare/8.4.1...8.5.0) --- updated-dependencies: - dependency-name: speedups dependency-version: 2.3.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: python - dependency-name: furo dependency-version: 2025.12.19 dependency-type: direct:production dependency-group: python - dependency-name: tqdm dependency-version: 4.70.1 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: python - dependency-name: click dependency-version: 8.5.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: python ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- benchmarks/requirements.txt | 6 +++--- docs/requirements.txt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/benchmarks/requirements.txt b/benchmarks/requirements.txt index cd03cdc3..deacdd28 100644 --- a/benchmarks/requirements.txt +++ b/benchmarks/requirements.txt @@ -1,6 +1,6 @@ -speedups==2.3.0 -tqdm==4.68.3 +speedups==2.3.3 +tqdm==4.70.1 rich==15.0.0 alive-progress==3.3.0 -click==8.4.1 +click==8.5.0 matplotlib==3.11.0 diff --git a/docs/requirements.txt b/docs/requirements.txt index 59565282..fcfc81c4 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,2 +1,2 @@ -e.[docs,tests] -furo>=2024.8.6 +furo>=2025.12.19 From 12f734aa831700dc09c30d5f497df400096911ed Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Mon, 16 Nov 2026 09:20:39 +0100 Subject: [PATCH 5/9] ci: sign release files with Sigstore from a dispatched sign.yml (#341) * ci: sign release files with Sigstore from a dispatched sign.yml * ci: name the sign job and explain its permissions The template from python-project-tools now passes zizmor --pedantic. --- .github/workflows/sign.yml | 217 +++++++++++++++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 .github/workflows/sign.yml diff --git a/.github/workflows/sign.yml b/.github/workflows/sign.yml new file mode 100644 index 00000000..049d178e --- /dev/null +++ b/.github/workflows/sign.yml @@ -0,0 +1,217 @@ +# Signs the PyPI files of a GitHub release with Sigstore, using the identity +# of this workflow run. Copy this file unchanged to .github/workflows/sign.yml. +# `ppt sign` dispatches it and finds the run by its run-name. +name: Sign release + +run-name: Sign ${{ inputs.tag }} ${{ inputs.request_id }} + +on: + workflow_dispatch: + inputs: + tag: + description: Release tag to sign, for example v1.2.3 + required: true + type: string + package: + description: Name of the package on PyPI + required: true + type: string + version: + description: Version on PyPI, when it differs from the tag without its leading v + required: false + default: '' + type: string + request_id: + description: Only used by ppt to find this run + required: false + default: '' + type: string + +permissions: {} + +concurrency: + group: sign-${{ inputs.tag }} + cancel-in-progress: false + +jobs: + sign: + name: Sign the PyPI files of the release + runs-on: ubuntu-latest + timeout-minutes: 30 + permissions: + contents: write # upload the bundles and missing files to the release + id-token: write # the OIDC token Sigstore signs with + env: + GH_REPO: ${{ github.repository }} + TAG: ${{ inputs.tag }} + PACKAGE: ${{ inputs.package }} + PYPI_VERSION: ${{ inputs.version }} + steps: + - name: Validate inputs + run: | + if ! [[ "$TAG" =~ ^v?[0-9][0-9A-Za-z._+!-]*$ ]]; then + echo "Invalid tag input" >&2 + exit 1 + fi + if ! [[ "$PACKAGE" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]]; then + echo "Invalid package input" >&2 + exit 1 + fi + if [ -n "$PYPI_VERSION" ] && ! [[ "$PYPI_VERSION" =~ ^v?[0-9][0-9A-Za-z._+!-]*$ ]]; then + echo "Invalid version input" >&2 + exit 1 + fi + + - name: Read the release + env: + GH_TOKEN: ${{ github.token }} + run: gh release view "$TAG" --json assets > assets.json + + - name: Download the files already on the release + env: + GH_TOKEN: ${{ github.token }} + run: | + mkdir existing + count="$(python3 -I -c ' + import json + import sys + with open(sys.argv[1], encoding="utf-8") as handle: + names = [a["name"] for a in json.load(handle)["assets"]] + suffixes = (".whl", ".tar.gz", ".zip") + print(sum(1 for name in names if name.endswith(suffixes))) + ' assets.json)" + if [ "$count" -eq 0 ]; then + echo "No distribution files on the release yet" + else + gh release download "$TAG" -D existing -p '*.whl' -p '*.tar.gz' -p '*.zip' + fi + + - name: Plan + id: plan + env: + EXISTING_DIR: existing + DIST_DIR: dist + ASSETS_JSON: assets.json + run: | + python3 -I - <<'PYEOF' + # ppt-plan-begin + import hashlib + import json + import os + import re + import sys + import urllib.request + from pathlib import Path + from typing import NoReturn + + TAG: str = os.environ['TAG'] + PACKAGE: str = os.environ['PACKAGE'] + # The version input when ppt passes one, else the tag without its v. + VERSION: str = os.environ.get('PYPI_VERSION') or ( + TAG[1:] if TAG.startswith('v') else TAG + ) + URL_TEMPLATE: str = os.environ.get( + 'PYPI_JSON_URL', 'https://pypi.org/pypi/{package}/{version}/json' + ) + # Only files PyPI serves itself are fetched. Tests point it at file://. + FILES_PREFIX: str = os.environ.get( + 'PYPI_FILES_PREFIX', 'https://files.pythonhosted.org/' + ) + EXISTING_DIR: Path = Path(os.environ['EXISTING_DIR']) + DIST_DIR: Path = Path(os.environ['DIST_DIR']) + + + def fail(message: str) -> NoReturn: + print(message, file=sys.stderr) + raise SystemExit(1) + + + def sha256_of(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + + def fetch(url: str) -> bytes: + with urllib.request.urlopen(url, timeout=120) as response: + data: bytes = response.read() + return data + + + def main() -> None: + json_url: str = URL_TEMPLATE.format(package=PACKAGE, version=VERSION) + info: dict = json.loads(fetch(json_url)) + files: list[dict] = info.get('urls') or [] + if not files: + fail(f'PyPI lists no files for {PACKAGE} {VERSION}') + with open(os.environ['ASSETS_JSON'], encoding='utf-8') as handle: + assets: set[str] = {a['name'] for a in json.load(handle)['assets']} + DIST_DIR.mkdir(parents=True, exist_ok=True) + to_sign: list[str] = [] + to_upload: list[str] = [] + for entry in files: + name: str = entry['filename'] + if not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9._+!-]*', name): + fail(f'Refusing unsafe file name {name!r}') + expected: str = entry['digests']['sha256'] + url: str = entry['url'] + if not url.startswith(FILES_PREFIX): + fail(f'Refusing {name}: its URL does not start with {FILES_PREFIX}') + data: bytes = fetch(url) + if sha256_of(data) != expected: + fail(f'Downloaded {name} does not match the PyPI sha256') + (DIST_DIR / name).write_bytes(data) + if name in assets: + existing: Path = EXISTING_DIR / name + if not existing.is_file(): + fail(f'Release asset {name} could not be downloaded') + if sha256_of(existing.read_bytes()) != expected: + fail(f'Release asset {name} differs from the PyPI file') + else: + to_upload.append(name) + if f'{name}.sigstore.json' not in assets: + to_sign.append(name) + with open(os.environ['GITHUB_OUTPUT'], 'a', encoding='utf-8') as out: + out.write(f'to_sign={" ".join(to_sign)}\n') + out.write(f'to_upload={" ".join(to_upload)}\n') + + + main() + # ppt-plan-end + PYEOF + + - name: List files to sign + id: paths + env: + TO_SIGN: ${{ steps.plan.outputs.to_sign }} + run: | + paths="" + for name in $TO_SIGN; do + paths="$paths dist/$name" + done + echo "sign_inputs=${paths# }" >> "$GITHUB_OUTPUT" + + - name: Sign with Sigstore + if: steps.plan.outputs.to_sign != '' + uses: sigstore/gh-action-sigstore-python@790bc6befb9d733738f18d8f895854b453640ec9 # v3.5.0 + with: + inputs: ${{ steps.paths.outputs.sign_inputs }} + upload-signing-artifacts: false + release-signing-artifacts: false + + - name: Upload to the release + env: + GH_TOKEN: ${{ github.token }} + TO_SIGN: ${{ steps.plan.outputs.to_sign }} + TO_UPLOAD: ${{ steps.plan.outputs.to_upload }} + run: | + files=() + for name in $TO_UPLOAD; do + files+=("dist/$name") + done + for name in $TO_SIGN; do + files+=("dist/$name.sigstore.json") + done + if [ "${#files[@]}" -eq 0 ]; then + echo "Nothing to upload" + exit 0 + fi + gh release upload "$TAG" "${files[@]}" From aac8a7fbff3dbaf9f5bfaa0218193b1fd1e60cd5 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Fri, 6 Nov 2026 11:33:14 +0100 Subject: [PATCH 6/9] Add the missing newline at the end of SECURITY.md --- SECURITY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SECURITY.md b/SECURITY.md index 9fd1e6b7..160b0159 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -15,4 +15,4 @@ That keeps the report out of the public issue tracker until a fix is available. If you cannot use the form, mail instead. Please include the progressbar2 version, the configuration that triggers the -issue and a minimal reproduction. Expect an acknowledgement within a week. \ No newline at end of file +issue and a minimal reproduction. Expect an acknowledgement within a week. From 61634991cd6df0b253d07affea816e2828878663 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Fri, 6 Nov 2026 11:33:15 +0100 Subject: [PATCH 7/9] Fix relabelling, triple flush and lost text regressions from 4.7.0 A bar that leaves a MultiBar takes its generated label widgets with it, so adding it again labels it once. The stop_capturing() that brings the count to zero flushes the original stream once, as in 4.6.0. A WrappingIO.flush() replacement that raises no longer costs the captured text. --- CHANGES.rst | 16 +++++++ progressbar/multi.py | 40 ++++++++++++++-- progressbar/utils.py | 67 +++++++++++++++++++------- tests/test_multibar.py | 90 +++++++++++++++++++++++++++++++++++ tests/test_stream_security.py | 75 +++++++++++++++++++++++++++-- 5 files changed, 266 insertions(+), 22 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 403b62e1..d93a571d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,22 @@ Changelog ========= +Unreleased +---------- + +A bar that leaves a ``MultiBar``, through ``pop``, ``del``, ``clear``, +a replacement under the same label or ``remove_finished``, takes its +generated label widgets with it. Adding it again labels it once. 4.7.0 +left the old labels in ``bar.widgets`` and labelled it twice. + +The ``stop_capturing()`` that brings the count to zero flushes the +original stream once, as 4.6.0 did. 4.7.0 flushed it three times. + +A ``WrappingIO.flush()`` replacement that raises no longer costs the +captured text. The text goes back in front of anything written since, +and the next flush delivers it in order. 4.7.0 drained the buffer first +and lost the text. + 4.7.0 - 2026-11-05 ------------------ diff --git a/progressbar/multi.py b/progressbar/multi.py index 4ffd4cb2..fc7aa499 100644 --- a/progressbar/multi.py +++ b/progressbar/multi.py @@ -176,6 +176,7 @@ class MultiBar(dict[str, bar.ProgressBar]): _previous_output: list[str] _finished_at: _identity.IdentityMap[bar.ProgressBar, float] _labeled: _identity.IdentitySet[bar.ProgressBar] + _labels: _identity.IdentityMap[bar.ProgressBar, list[str]] _custom_labels: bool _membership_box: utils._SharedLock # pyright: ignore[reportPrivateUsage] _print_lock: threading.RLock @@ -237,6 +238,7 @@ def __init__( self._custom_labels = False self._labeled = _identity.IdentitySet[bar.ProgressBar]() + self._labels = _identity.IdentityMap() self._finished_at = _identity.IdentityMap() self._previous_output = [] self._buffer = io.StringIO() @@ -344,6 +346,21 @@ def _forget_bar(self, progress: bar.ProgressBar | None) -> None: if progress is not None: self._finished_at.pop(progress, None) self._labeled.discard(progress) + self._remove_labels(progress, self._labels.pop(progress, [])) + + @staticmethod + def _remove_labels(progress: bar.ProgressBar, labels: list[str]) -> None: + """Take the generated label widgets out of a bar that leaves. + + The widgets are matched by identity: the bar keeps a label of + its own that happens to render the same text. + """ + widgets: collections.abc.MutableSequence[typing.Any] = progress.widgets + for label in labels: + for index, widget in enumerate(widgets): + if widget is label: + del widgets[index] + break def __delitem__(self, key: str) -> None: """Remove a progressbar and its tracking.""" @@ -424,9 +441,12 @@ def clear(self) -> None: removed = list(super().values()) removed.extend(self._finished_at) removed.extend(self._labeled) + for progress in self._labels: + self._remove_labels(progress, self._labels[progress]) super().clear() self._finished_at.clear() self._labeled.clear() + self._labels.clear() # `removed` keeps the bars alive until this method returns, so # their finalisers run after the membership guard is released. @@ -445,11 +465,25 @@ def _label_bar( if self.prepend_label or self.append_label: self._labeled.add(bar) + labels: list[str] = [] if self.prepend_label: # pragma: no branch - widgets.insert(0, self.label_format.format(label=label)) + prefix: str = self.label_format.format(label=label) + widgets.insert(0, prefix) + labels.append(prefix) if self.append_label: # pragma: no branch - widgets.append(self.label_format.format(label=label)) - return True + suffix: str = self.label_format.format(label=label) + widgets.append(suffix) + labels.append(suffix) + if not labels: + return True + with self._membership_guard(label): + if bar in self._labeled: + self._labels[bar] = labels + return True + # The bar left while its labels were being formatted. Take them + # out again, so a later insertion labels it once. + self._remove_labels(bar, labels) + return not require_member def render(self, flush: bool = True, force: bool = False) -> None: """Redraw every bar, only touching lines that actually changed. diff --git a/progressbar/utils.py b/progressbar/utils.py index 69aaa636..c6e0973b 100644 --- a/progressbar/utils.py +++ b/progressbar/utils.py @@ -449,12 +449,33 @@ def _deliver(self, value: str) -> None: if value: # A replacement flush() may log. Call it only during # delivery, outside both the buffer and transition locks. - self.flush() + try: + self.flush() + except BaseException: + # The text was taken already. Put it back in front of + # anything written since, so a flush() that fails once + # (a handler that is not ready yet) costs no output. + self._restore(value) + raise if value and not self.target.closed: self.target.write(value) # when explicitly flushing, always flush the target as well self.flush_target() + def _restore(self, value: str) -> None: + """Put text taken by `_take()` back, ahead of later writes.""" + with ( + self._buffer_box.lock + if self._native_buffer() + else contextlib.nullcontext() + ): + rest: str = self.buffer.getvalue() + self.buffer.seek(0) + self.buffer.truncate(0) + self.buffer.write(value + rest) + if '\n' in value: + self.needs_clear = True + def flush_target(self) -> None: # pragma: no cover """Flush `target` itself, if it's open and flushable.""" if not self.target.closed and getattr(self.target, 'flush', None): @@ -681,6 +702,11 @@ def stop_capturing(self, bar: _ProgressListener | None = None) -> None: """ pending: list[_Pending] = [] failure: BaseException | None = None + # The default hook drains and delivers through `flush()`, as + # 4.6.0 did, so the original stream is flushed once. Only a + # replaced hook, which may not call `super()`, needs this stop + # to deliver its own text before the hook runs. + deliver: bool = not self._default_capture_hook() with self.lock: if bar: # pragma: no branch with self.listeners_lock, contextlib.suppress(KeyError): @@ -690,16 +716,24 @@ def stop_capturing(self, bar: _ProgressListener | None = None) -> None: # the next start_capturing() a no-op self.capturing = max(self.capturing - 1, 0) try: - pending = self._apply_capturing() + if deliver: + pending = self._apply_capturing() + else: + self._propagate_capturing() except BaseException as exception: # noqa: BLE001 failure = exception - if failure is None: + if failure is None and pending: try: self._deliver(pending) except BaseException as exception: # noqa: BLE001 failure = exception self._notify_capturing(failure) + def _default_capture_hook(self) -> bool: + """Return whether `update_capturing` is the one defined here.""" + hook: object = getattr(self.update_capturing, '__func__', None) + return hook is StreamWrapper.update_capturing + def _notify_capturing(self, failure: BaseException | None) -> None: """Notify the public hook without replacing an earlier failure.""" if failure is None: @@ -720,22 +754,28 @@ def update_capturing(self) -> None: # pragma: no cover until something else happens to trigger a flush. Start and stop invoke overrides after the state propagation - attempt. Stop also attempts delivery before invoking the hook, - even if that attempt fails. Calling `super()` reconciles the - current state again and can flush an already empty target. + attempt. A stop with a replaced hook also delivers its own text + before invoking it, even if that delivery fails. Calling + `super()` reconciles the current state again and flushes. Concurrent transitions can change the state before the hook runs, so it can observe a later transition's count and flags. """ with self.lock: - pending: list[_Pending] = self._apply_capturing() - self._deliver(pending) + self._propagate_capturing() if self.capturing <= 0: # 4.6.0 flushed through the public method here, so a # subclass that overrides `flush()` sees the last bar stop - # capturing. The text itself went out above, this drains - # whatever a listener wrote in between. + # capturing, and the original stream is flushed once. self.flush() + def _propagate_capturing(self) -> None: + """Push the capture flag onto the installed wrappers, under `lock`.""" + if isinstance(self.stdout, WrappingIO): + self.stdout.capturing = self.capturing > 0 + + if isinstance(self.stderr, WrappingIO): + self.stderr.capturing = self.capturing > 0 + def _apply_capturing(self) -> list[_Pending]: """Push the capture state onto the wrappers, under `lock`. @@ -748,12 +788,7 @@ def _apply_capturing(self) -> list[_Pending]: same text twice, and an unwrap cannot swap the wrapper out between the type check and the drain. """ - if isinstance(self.stdout, WrappingIO): - self.stdout.capturing = self.capturing > 0 - - if isinstance(self.stderr, WrappingIO): - self.stderr.capturing = self.capturing > 0 - + self._propagate_capturing() if self.capturing > 0: return [] return self._take() diff --git a/tests/test_multibar.py b/tests/test_multibar.py index 20ba30e8..7182167f 100644 --- a/tests/test_multibar.py +++ b/tests/test_multibar.py @@ -1211,3 +1211,93 @@ def insert() -> None: assert multibar['task'] is progress assert next(iter(multibar)) is label assert observed and all(observed) + + +@pytest.mark.parametrize('removal', ['pop', 'del', 'clear', 'replace']) +@pytest.mark.parametrize( + 'prepend,append', [(True, False), (False, True), (True, True)] +) +def test_reinserted_bar_keeps_one_label_at_each_end( + removal: str, prepend: bool, append: bool +) -> None: + # Regression against 4.7.0: a bar that left the multibar kept its + # generated label widgets, so adding it again labelled it twice. + multibar: progressbar.MultiBar = progressbar.MultiBar( + fd=io.StringIO(), + prepend_label=prepend, + append_label=append, + label_format='{label}:', + ) + bar: progressbar.ProgressBar = progressbar.ProgressBar( + widgets=['body'], max_value=1, fd=io.StringIO() + ) + multibar['task'] = bar + bar.start() + multibar.render() + expected: list[str] = ['body'] + if prepend: + expected.insert(0, 'task:') + if append: + expected.append('task:') + assert bar.widgets == expected + for _ in range(2): + if removal == 'pop': + multibar.pop('task') + elif removal == 'del': + del multibar['task'] + elif removal == 'clear': + multibar.clear() + else: + multibar['task'] = progressbar.ProgressBar(fd=io.StringIO()) + assert bar.widgets == ['body'] + multibar['task'] = bar + multibar.render() + assert bar.widgets == expected + + +def test_bar_removed_while_being_labelled_ends_up_unlabelled() -> None: + # The labels are formatted outside the membership guard, so another + # thread can remove the bar in between. Here the removal happens + # from inside the format call, which pins the same interleaving + # without a tracer (which coverage on 3.10 could not measure). + multibar: progressbar.MultiBar = progressbar.MultiBar( + fd=io.StringIO(), prepend_label=True, label_format='{label}:' + ) + bar: progressbar.ProgressBar = progressbar.ProgressBar( + widgets=['body'], max_value=1, fd=io.StringIO() + ) + + class PoppingFormat(str): + def format(self, *args: object, **kwargs: object) -> str: + multibar.pop('task') + return str.format(self, *args, **kwargs) + + multibar.label_format = PoppingFormat('{label}:') + multibar['task'] = bar + assert multibar._label_bar(bar, require_member=True) is False + assert 'task' not in multibar + assert bar.widgets == ['body'] + assert bar not in multibar._labeled + assert bar not in multibar._labels + multibar.label_format = '{label}:' + multibar['task'] = bar + assert multibar._label_bar(bar, require_member=True) is True + assert bar.widgets == ['task:', 'body'] + + +def test_label_already_removed_by_the_caller_costs_no_other_widget() -> None: + multibar: progressbar.MultiBar = progressbar.MultiBar( + fd=io.StringIO(), prepend_label=True, label_format='{label}:' + ) + bar: progressbar.ProgressBar = progressbar.ProgressBar( + widgets=['task:', 'body'], max_value=1, fd=io.StringIO() + ) + multibar['task'] = bar + bar.start() + multibar.render() + assert bar.widgets == ['task:', 'task:', 'body'] + del bar.widgets[0] + multibar.pop('task') + # Only the generated label object is removed, matched by identity, + # so the bar's own equal-looking label stays. + assert bar.widgets == ['task:', 'body'] diff --git a/tests/test_stream_security.py b/tests/test_stream_security.py index 4c752c40..56457181 100644 --- a/tests/test_stream_security.py +++ b/tests/test_stream_security.py @@ -1117,9 +1117,10 @@ def check_locks() -> None: assert streams.capturing == 0 assert stdout.capturing is False and stderr.capturing is False assert err.getvalue() == 'eggs' - assert out.getvalue() == ( - 'spam' if failure_at in ('target_flush', 'drain') else '' - ) + # Only a failed target write costs the text, so a retry cannot + # write it twice. A failed buffer flush keeps it for the hook's + # own flush, as in 4.6.0. + assert out.getvalue() == ('' if failure_at == 'write' else 'spam') streams.unwrap_stdout() streams.unwrap_stderr() @@ -1466,3 +1467,71 @@ def update_capturing(self) -> None: # the stop that brought the count to zero. assert calls == ['update_capturing', 'update_capturing', 'flush'] assert target.getvalue() == 'spam' + + +def test_default_capture_stop_flushes_the_target_once() -> None: + # 4.6.0 flushed the original stream once when the last bar stopped + # capturing. 4.7.0 flushed it three times: from stop_capturing() + # and twice more from the default update_capturing() it then ran. + class Target(io.StringIO): + flushes: int = 0 + + def flush(self) -> None: + self.flushes += 1 + super().flush() + + target: Target = Target() + streams: utils.StreamWrapper = utils.StreamWrapper() + streams.stdout = utils.WrappingIO(target) + streams.wrapped_stdout = 1 + streams.start_capturing() + streams.stdout.write('spam') + assert target.flushes == 0 + streams.stop_capturing() + assert target.getvalue() == 'spam' + assert target.flushes == 1 + + +@pytest.mark.parametrize('shadow', [False, True]) +def test_failing_flush_hook_keeps_the_captured_text(shadow: bool) -> None: + # A flush() replacement that raises once must not cost the text + # already taken from the buffer. 4.6.0 called flush() before + # draining, 4.7.0 drained first and lost the text. + failure: OSError = OSError('temporary flush failure') + attempts: int = 0 + + def flush() -> None: + nonlocal attempts + attempts += 1 + if attempts == 1: + raise failure + + class RetryFlush(utils.WrappingIO): + def flush(self) -> None: + flush() + + target: io.StringIO = io.StringIO() + healthy: io.StringIO = io.StringIO() + wrapper: utils.WrappingIO = ( + utils.WrappingIO(target, capturing=True) + if shadow + else RetryFlush(target, capturing=True) + ) + if shadow: + wrapper.flush = flush # type: ignore[method-assign] + streams: utils.StreamWrapper = utils.StreamWrapper() + streams.stdout = wrapper + streams.stderr = utils.WrappingIO(healthy, capturing=True) + streams.wrapped_stdout = streams.wrapped_stderr = 1 + wrapper.write('first|\n') + streams.stderr.write('healthy|') + with pytest.raises(OSError) as caught: + streams.flush() + assert caught.value is failure + assert healthy.getvalue() == 'healthy|' + assert wrapper.buffer.getvalue() == 'first|\n' + assert wrapper.needs_clear is True + wrapper.write('second|') + streams.flush() + assert target.getvalue() == 'first|\nsecond|' + assert wrapper.buffer.getvalue() == '' From 1b21ef80733a4125f9ed1c82ff05370ca2508fec Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Mon, 16 Nov 2026 13:24:07 +0100 Subject: [PATCH 8/9] Remove a generated label from its own end of the widgets `'{label}'.format()` returns the key object itself, so a bar that holds the same string as a widget of its own carries the label object more than once. The forward search then removed the bar's own widget when the label was appended, and the widgets changed order. The prefix is now searched from the front and the suffix from the back. A replaced `update_capturing()` hook that calls `super()` flushes the original stream twice on the last stop. That is pinned by a test and stated in the changelog. --- CHANGES.rst | 4 ++- progressbar/multi.py | 43 ++++++++++++++++++--------- tests/test_multibar.py | 55 +++++++++++++++++++++++++++++++---- tests/test_stream_security.py | 36 +++++++++++++++++++++++ 4 files changed, 118 insertions(+), 20 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d93a571d..da476114 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -11,7 +11,9 @@ generated label widgets with it. Adding it again labels it once. 4.7.0 left the old labels in ``bar.widgets`` and labelled it twice. The ``stop_capturing()`` that brings the count to zero flushes the -original stream once, as 4.6.0 did. 4.7.0 flushed it three times. +original stream once, as 4.6.0 did. 4.7.0 flushed it three times. A +``StreamWrapper`` subclass whose ``update_capturing()`` calls ``super()`` +gets two flushes, the second one on an empty buffer. A ``WrappingIO.flush()`` replacement that raises no longer costs the captured text. The text goes back in front of anything written since, diff --git a/progressbar/multi.py b/progressbar/multi.py index fc7aa499..465190a1 100644 --- a/progressbar/multi.py +++ b/progressbar/multi.py @@ -39,6 +39,9 @@ SortKeyFunc = collections.abc.Callable[[bar.ProgressBar], typing.Any] _NO_LABEL: object = object() +#: The label widgets a `MultiBar` inserted into one bar: the prefix and +#: the suffix, `None` for the end that got no label. +_Labels = tuple[str | None, str | None] class _Update(typing.Protocol): @@ -176,7 +179,7 @@ class MultiBar(dict[str, bar.ProgressBar]): _previous_output: list[str] _finished_at: _identity.IdentityMap[bar.ProgressBar, float] _labeled: _identity.IdentitySet[bar.ProgressBar] - _labels: _identity.IdentityMap[bar.ProgressBar, list[str]] + _labels: _identity.IdentityMap[bar.ProgressBar, _Labels] _custom_labels: bool _membership_box: utils._SharedLock # pyright: ignore[reportPrivateUsage] _print_lock: threading.RLock @@ -346,19 +349,33 @@ def _forget_bar(self, progress: bar.ProgressBar | None) -> None: if progress is not None: self._finished_at.pop(progress, None) self._labeled.discard(progress) - self._remove_labels(progress, self._labels.pop(progress, [])) + self._remove_labels( + progress, self._labels.pop(progress, (None, None)) + ) @staticmethod - def _remove_labels(progress: bar.ProgressBar, labels: list[str]) -> None: + def _remove_labels(progress: bar.ProgressBar, labels: _Labels) -> None: """Take the generated label widgets out of a bar that leaves. The widgets are matched by identity: the bar keeps a label of - its own that happens to render the same text. + its own that happens to render the same text. The prefix is + searched from the front and the suffix from the back, because + `label_format` can return an object the bar also holds as a + widget of its own (`'{label}'` returns the key itself). """ widgets: collections.abc.MutableSequence[typing.Any] = progress.widgets - for label in labels: - for index, widget in enumerate(widgets): - if widget is label: + prefix: str | None + suffix: str | None + prefix, suffix = labels + index: int + if prefix is not None: + for index in range(len(widgets)): + if widgets[index] is prefix: + del widgets[index] + break + if suffix is not None: + for index in reversed(range(len(widgets))): + if widgets[index] is suffix: del widgets[index] break @@ -465,17 +482,17 @@ def _label_bar( if self.prepend_label or self.append_label: self._labeled.add(bar) - labels: list[str] = [] + prefix: str | None = None + suffix: str | None = None if self.prepend_label: # pragma: no branch - prefix: str = self.label_format.format(label=label) + prefix = self.label_format.format(label=label) widgets.insert(0, prefix) - labels.append(prefix) if self.append_label: # pragma: no branch - suffix: str = self.label_format.format(label=label) + suffix = self.label_format.format(label=label) widgets.append(suffix) - labels.append(suffix) - if not labels: + if prefix is None and suffix is None: return True + labels: _Labels = (prefix, suffix) with self._membership_guard(label): if bar in self._labeled: self._labels[bar] = labels diff --git a/tests/test_multibar.py b/tests/test_multibar.py index 7182167f..411bc347 100644 --- a/tests/test_multibar.py +++ b/tests/test_multibar.py @@ -3,6 +3,7 @@ import random import threading import time +import typing import pytest @@ -1255,6 +1256,40 @@ def test_reinserted_bar_keeps_one_label_at_each_end( assert bar.widgets == expected +@pytest.mark.parametrize( + 'prepend,append', [(True, False), (False, True), (True, True)] +) +def test_label_object_shared_with_a_widget_leaves_from_its_own_end( + prepend: bool, append: bool +) -> None: + # CPython returns the key object itself for `'{label}'.format()`, + # so a bar that carries that string as a widget of its own holds + # the label object more than once. The prefix leaves from the front + # and the suffix from the back, so the bar's own widgets keep their + # order. `Verbatim` pins the shared object on every interpreter. + class Verbatim(str): + def format(self, *args: object, **kwargs: object) -> str: + return typing.cast(str, kwargs['label']) + + key: str = 'spam' + multibar: progressbar.MultiBar = progressbar.MultiBar( + fd=io.StringIO(), + prepend_label=prepend, + append_label=append, + label_format=Verbatim('{label}'), + ) + own: list[str] = [key, 'body', key] + bar: progressbar.ProgressBar = progressbar.ProgressBar( + widgets=list(own), max_value=1, fd=io.StringIO() + ) + multibar[key] = bar + bar.start() + multibar.render() + assert len(bar.widgets) == len(own) + prepend + append + multibar.pop(key) + assert bar.widgets == own + + def test_bar_removed_while_being_labelled_ends_up_unlabelled() -> None: # The labels are formatted outside the membership guard, so another # thread can remove the bar in between. Here the removal happens @@ -1285,19 +1320,27 @@ def format(self, *args: object, **kwargs: object) -> str: assert bar.widgets == ['task:', 'body'] -def test_label_already_removed_by_the_caller_costs_no_other_widget() -> None: +@pytest.mark.parametrize('append', [False, True]) +def test_label_already_removed_by_the_caller_costs_no_other_widget( + append: bool, +) -> None: multibar: progressbar.MultiBar = progressbar.MultiBar( - fd=io.StringIO(), prepend_label=True, label_format='{label}:' + fd=io.StringIO(), + prepend_label=not append, + append_label=append, + label_format='{label}:', ) + own: list[str] = ['body', 'task:'] if append else ['task:', 'body'] bar: progressbar.ProgressBar = progressbar.ProgressBar( - widgets=['task:', 'body'], max_value=1, fd=io.StringIO() + widgets=list(own), max_value=1, fd=io.StringIO() ) multibar['task'] = bar bar.start() multibar.render() - assert bar.widgets == ['task:', 'task:', 'body'] - del bar.widgets[0] + generated: int = -1 if append else 0 + assert bar.widgets == ([*own, 'task:'] if append else ['task:', *own]) + del bar.widgets[generated] multibar.pop('task') # Only the generated label object is removed, matched by identity, # so the bar's own equal-looking label stays. - assert bar.widgets == ['task:', 'body'] + assert bar.widgets == own diff --git a/tests/test_stream_security.py b/tests/test_stream_security.py index 56457181..e0d5cd49 100644 --- a/tests/test_stream_security.py +++ b/tests/test_stream_security.py @@ -1492,6 +1492,42 @@ def flush(self) -> None: assert target.flushes == 1 +@pytest.mark.parametrize('call_super,flushes', [(False, 1), (True, 2)]) +def test_replaced_capture_hook_gets_the_stop_delivery_first( + call_super: bool, flushes: int +) -> None: + # A replaced hook may not call super(), so the last stop delivers + # its own text before the hook runs. A hook that does call super() + # flushes the original stream a second time, on an empty buffer. + # 4.7.0 flushed it three times there, 4.6.0 once. + class Target(io.StringIO): + flushes: int = 0 + + def flush(self) -> None: + self.flushes += 1 + super().flush() + + delivered: list[str] = [] + + class Streams(utils.StreamWrapper): + def update_capturing(self) -> None: + delivered.append(target.getvalue()) + if call_super: + super().update_capturing() + + target: Target = Target() + streams: Streams = Streams() + streams.stdout = utils.WrappingIO(target) + streams.wrapped_stdout = 1 + streams.start_capturing() + streams.stdout.write('spam') + before: int = target.flushes + streams.stop_capturing() + assert delivered == ['', 'spam'] + assert target.getvalue() == 'spam' + assert target.flushes - before == flushes + + @pytest.mark.parametrize('shadow', [False, True]) def test_failing_flush_hook_keeps_the_captured_text(shadow: bool) -> None: # A flush() replacement that raises once must not cost the text From a4aba25bb37f3fd35573ab1426f26defb5aa374f Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Mon, 16 Nov 2026 16:26:55 +0100 Subject: [PATCH 9/9] chore: release 4.7.1 --- CHANGES.rst | 6 ++++-- progressbar/__about__.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index da476114..dd3d637a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,8 +2,8 @@ Changelog ========= -Unreleased ----------- +4.7.1 - 2026-11-16 +------------------ A bar that leaves a ``MultiBar``, through ``pop``, ``del``, ``clear``, a replacement under the same label or ``remove_finished``, takes its @@ -20,6 +20,8 @@ captured text. The text goes back in front of anything written since, and the next flush delivers it in order. 4.7.0 drained the buffer first and lost the text. +The package metadata on PyPI links to the release notes (PR #335). + 4.7.0 - 2026-11-05 ------------------ diff --git a/progressbar/__about__.py b/progressbar/__about__.py index 8b68df67..b041e72e 100644 --- a/progressbar/__about__.py +++ b/progressbar/__about__.py @@ -21,9 +21,9 @@ """.strip().split(), ) __email__ = 'wolph@wol.ph' -__version__ = '4.7.0' +__version__ = '4.7.1' #: Release date of ``__version__`` as a static string. Bump on each release. -__date__ = '2026-11-05' +__date__ = '2026-11-16' __license__ = 'BSD' __copyright__ = 'Copyright 2015-2026 Rick van Hattem (Wolph)' __url__ = 'https://github.com/wolph/python-progressbar'