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

LudovicRousseau / pyscard / 11323269743

14 Oct 2024 07:55AM UTC coverage: 64.919% (+0.3%) from 64.591%
11323269743

push

github

LudovicRousseau
Test and improve the synchronization code

Added
-----

* Add tests to bring `Synchronization.py` to 100% coverage
* Add type annotations to the synchronization primitives
* Add `typing_extensions` as a Python 3.9 requirement;
  this is needed to annotate that the `synchronized()` decorator
  takes a function with a certain set of parameter and return types
  and returns a function with the same parameter and return types

Changed
-------

* Make the synchronized function's `self` parameter explicit;
  this allows the `self` parameter to be type-annotated
  so the dependency on the `self.mutex` attribute is explicit
* Use the `self.mutex` lock as a context manager
* Support keyword arguments to synchronized functions

Fixed
-----

* Wrap synchronized functions correctly;
  previous behavior was to lose the function name and docstring

Removed
-------

* Remove `print()` lines that are commented out
* Remove a `bytes` instance check; method names can only be strings

100 of 466 branches covered (21.46%)

21 of 22 new or added lines in 1 file covered. (95.45%)

34 existing lines in 22 files now uncovered.

4210 of 6485 relevant lines covered (64.92%)

4.16 hits per line

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

0.0
/src/smartcard/ExclusiveTransmitCardConnection.py
1
"""Sample CardConnectionDecorator that provides exclusive transmit()
2

3
__author__ = "https://www.gemalto.com/"
4

5
Copyright 2001-2012 gemalto
6
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
7

8
This file is part of pyscard.
9

10
pyscard is free software; you can redistribute it and/or modify
11
it under the terms of the GNU Lesser General Public License as published by
12
the Free Software Foundation; either version 2.1 of the License, or
13
(at your option) any later version.
14

15
pyscard is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
GNU Lesser General Public License for more details.
19

20
You should have received a copy of the GNU Lesser General Public License
21
along with pyscard; if not, write to the Free Software
22
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
"""
24

25
import smartcard.pcsc
×
26
from smartcard.CardConnectionDecorator import CardConnectionDecorator
×
27
from smartcard.Exceptions import CardConnectionException
×
28
from smartcard.scard import (
×
29
    SCARD_LEAVE_CARD,
30
    SCARD_S_SUCCESS,
31
    SCardBeginTransaction,
32
    SCardEndTransaction,
33
    SCardGetErrorMessage,
34
)
35

36

37
class ExclusiveTransmitCardConnection(CardConnectionDecorator):
×
38
    """This decorator uses
39
    L{SCardBeginTransaction}/L{SCardEndTransaction} to preserve other
40
    processes of threads to access the card during transmit()."""
41

42
    def __init__(self, cardconnection):
×
43
        CardConnectionDecorator.__init__(self, cardconnection)
×
44

45
    def lock(self):
×
46
        """Lock card with L{SCardBeginTransaction}."""
47

48
        component = self.component
×
UNCOV
49
        while True:
50
            if isinstance(
×
51
                component, smartcard.pcsc.PCSCCardConnection.PCSCCardConnection
52
            ):
53
                hresult = SCardBeginTransaction(component.hcard)
×
54
                if SCARD_S_SUCCESS != hresult:
×
55
                    raise CardConnectionException(
×
56
                        "Failed to lock with SCardBeginTransaction: "
57
                        + SCardGetErrorMessage(hresult)
58
                    )
59
                else:
60
                    # print('locked')
UNCOV
61
                    pass
62
                break
×
63
            if hasattr(component, "component"):
×
64
                component = component.component
×
65
            else:
66
                break
×
67

68
    def unlock(self):
×
69
        """Unlock card with L{SCardEndTransaction}."""
70
        component = self.component
×
UNCOV
71
        while True:
72
            if isinstance(
×
73
                component, smartcard.pcsc.PCSCCardConnection.PCSCCardConnection
74
            ):
75
                hresult = SCardEndTransaction(component.hcard, SCARD_LEAVE_CARD)
×
76
                if SCARD_S_SUCCESS != hresult:
×
77
                    raise CardConnectionException(
×
78
                        "Failed to unlock with SCardEndTransaction: "
79
                        + SCardGetErrorMessage(hresult)
80
                    )
81
                else:
82
                    # print('unlocked')
UNCOV
83
                    pass
84
                break
×
85
            if hasattr(component, "component"):
×
86
                component = component.component
×
87
            else:
88
                break
×
89

90
    def transmit(self, command, protocol=None):
×
91
        """Gain exclusive access to card during APDU transmission for if this
92
        decorator decorates a PCSCCardConnection."""
93
        data, sw1, sw2 = CardConnectionDecorator.transmit(self, command, protocol)
×
94
        return data, sw1, sw2
×
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