backports — Python Backports#

This module contains backports to support older Python versions.

Caution

This module is not part of the public pywikibot API. Breaking changes may be made at any time, and the module is not subject to deprecation requirements.

Changed in version 10.0: This module is ‘private’.

class backports.RLock(*args, **kwargs)[source]#

Bases: object

Context manager which implements extended reentrant lock objects.

This RLock is implicit derived from threading.RLock but provides a locked() method like in threading.Lock and a count attribute which gives the active recursion level of locks.

Usage:

>>> lock = RLock()
>>> lock.acquire()
True
>>> with lock: print(lock.count)  # nested lock
2
>>> lock.locked()
True
>>> lock.release()
>>> lock.locked()
False

Added in version 6.2.

Changed in version 10.2: moved from tools.threading to backports.

Deprecated since version 10.2: Passing any arguments is deprecated; a TypeError will be raised with Pywikibot 13.0.

Removed in version 16.0.

Note

Passing any arguments has no effect and has been deprecated since Python 3.14 and was removed in Python 3.15.

property count: Any[source]#

Deprecated.

Return number of acquired locks.

Deprecated since version 10.2.

locked()[source]#

Return true if the lock is acquired.

backports.batched(iterable, n, *, strict=False)[source]#

Batch data from the iterable into tuples of length n.

Note

The last batch may be shorter than n if strict is True or raise a ValueError otherwise.

Example:

>>> i = batched(range(25), 10)
>>> print(next(i))
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> print(next(i))
(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
>>> print(next(i))
(20, 21, 22, 23, 24)
>>> print(next(i))
Traceback (most recent call last):
 ...
StopIteration
>>> list(batched('ABCD', 2))
[('A', 'B'), ('C', 'D')]
>>> list(batched('ABCD', 3, strict=False))
[('A', 'B', 'C'), ('D',)]
>>> list(batched('ABCD', 3, strict=True))
Traceback (most recent call last):
 ...
ValueError: batched(): incomplete batch

See also

itertools.batched, backported from Python 3.12.

Added in version 8.2.

Changed in version 9.0: Added strict option, backported from Python 3.13

Removed in version 15.0.

Parameters:
  • n (int) – How many items of the iterable to get in one chunk

  • strict (bool) – Raise a ValueError if the final batch is shorter than n.

Raises:
  • ValueError – batched(): incomplete batch

  • TypeError – n cannot be interpreted as an integer

Return type:

Generator[tuple]

backports.pairwise(iterable)[source]#

Return successive overlapping pairs taken from the input iterable.

See also

itertools.pairwise, backported from Python 3.10.

Added in version 7.6.

Removed in version 12.0.

class backports.sentinel(name, /, *, repr=None)[source]#

Bases: object

Implementation of a unique sentinel object.

Usage:

>>> MISSING = sentinel('MISSING')
>>> MISSING
MISSING
>>> MISSING = sentinel('MISSING', repr="'MISSING'")
>>> MISSING
'MISSING'
>>> import copy
>>> value = copy.copy(MISSING)
>>> value is MISSING
True

Added in version 11.6.

Removed in version 17.0.

See also

Parameters:
  • name (str) – Name of the sentinel object; it should be the name of the variable to which the sentinel shall be assigned.

  • repr (str | None) – Representation returned by repr().

Raises:
  • TypeError – If name is not a string or subclassing is attempted.

  • AttributeError – If an attribute other than __module__ is assigned.