• 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

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
×
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')
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
×
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')
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