• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LudovicRousseau / pyscard / 14132208658

28 Mar 2025 03:17PM UTC coverage: 66.175% (+0.05%) from 66.13%
14132208658

push

github

LudovicRousseau
Remove obsolete issue template

103 of 480 branches covered (21.46%)

4441 of 6711 relevant lines covered (66.17%)

4.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/smartcard/Synchronization.py
1
"""
2
from Thinking in Python, Bruce Eckel
3
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Observer.html
4

5
(c) Copyright 2008, Creative Commons Attribution-Share Alike 3.0.
6

7
Simple emulation of Java's 'synchronized'
8
keyword, from Peter Norvig.
9
"""
10

11
from __future__ import annotations
20✔
12

13
import functools
20✔
14
import sys
20✔
15
import threading
20✔
16
from collections.abc import Iterable
20✔
17
from typing import Any, Callable, Protocol, TypeVar
20✔
18

19
if sys.version_info >= (3, 10):
20✔
20
    from typing import ParamSpec
16✔
21
else:
22
    from typing_extensions import ParamSpec
4✔
23

24

25
T = TypeVar("T")
20✔
26
P = ParamSpec("P")
20✔
27

28

29
def synchronized(method: Callable[P, T]) -> Callable[P, T]:
20✔
30
    @functools.wraps(method)
20✔
31
    def f(self: _SynchronizationProtocol, *args: Any, **kwargs: Any) -> Any:
20✔
32
        with self.mutex:
18✔
33
            return method(self, *args, **kwargs)
18✔
34

35
    return f
20✔
36

37

38
def synchronize(klass: type, names: str | Iterable[str] | None = None) -> None:
20✔
39
    """Synchronize methods in the given class.
40
    Only synchronize the methods whose names are
41
    given, or all methods if names=None."""
42

43
    if isinstance(names, str):
20✔
44
        names = names.split()
20✔
45
    for name, val in list(klass.__dict__.items()):
20✔
46
        if callable(val) and name != "__init__" and (names is None or name in names):
20✔
47
            setattr(klass, name, synchronized(val))
20✔
48

49

50
class _SynchronizationProtocol(Protocol):
20✔
51
    mutex: threading.Lock | threading.RLock
20✔
52

53

54
class Synchronization(_SynchronizationProtocol):
20✔
55
    # You can create your own self.mutex, or inherit from this class:
56

57
    def __init__(self):
20✔
58
        self.mutex = threading.RLock()
18✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc