Skip to content

Incorrect exception message in Barrier.__init__ #129403

Description

@Locked-chess-official

Bug report

Bug description:

# Add a code block here, if required
>>>import threading
>>>x=threading.Barrier(parties=0.5)
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    x=Barrier(parties=0.5)
ValueError: parties must be > 0

Yes. As you see 0.5 is below 0.
To solve this problem, adding this sentence is viable.

class Barrier:
    """Implements a Barrier.

    Useful for synchronizing a fixed number of threads at known synchronization
    points.  Threads block on 'wait()' and are simultaneously awoken once they
    have all made that call.

    """

    def __init__(self, parties, action=None, timeout=None):
        """Create a barrier, initialised to 'parties' threads.

        'action' is a callable which, when supplied, will be called by one of
        the threads after they have all entered the barrier and just prior to
        releasing them all. If a 'timeout' is provided, it is used as the
        default for all subsequent 'wait()' calls.
        #if not isinstance(parties, int):
        #        raise TypeError("parties must be an integer")
        if parties < 1:
                raise ValueError("parties must be > 0")

        """
        self._cond = Condition(Lock())
        self._action = action
        self._timeout = timeout
        self._parties = parties
        self._state = 0  # 0 filling, 1 draining, -1 resetting, -2 broken
        self._count = 0

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Activity

  1. Locked-chess-official commented on Jan 28, 2025

    @Locked-chess-official
    ContributorAuthor

    I think that it maybe a wrong tips bug. The judgement statement is "if parties <1'", but the exception's tips is "parties must be > 0".

  2. sobolevn commented on Jan 28, 2025

    @sobolevn
    Member

    The same happens here:

    if parties < 1:
    raise ValueError('parties must be > 0')

    cpython/Lib/threading.py

    Lines 696 to 697 in 180ee43

    if parties < 1:
    raise ValueError("parties must be > 0")

    PR with the exc message change from > 0 to >= 1 is welcome! 👍

  3. changed the title [-]0.5<0[/-] [+]Incorrect exception message in `Barrier.__init__`[/+] on Jan 28, 2025
  4. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Jan 28, 2025
  5. added a commit that references this issue on Jan 30, 2025
  6. added 2 commits that reference this issue on Jan 30, 2025
  7. added 2 commits that reference this issue on Jan 30, 2025
  8. added a commit that references this issue on Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13only security fixes3.14bugs and security fixeseasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions