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

LudovicRousseau / pyscard / 14430710036

13 Apr 2025 03:09PM UTC coverage: 66.013% (-0.03%) from 66.043%
14430710036

push

github

LudovicRousseau
Release 2.2.2

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>

104 of 483 branches covered (21.53%)

4444 of 6732 relevant lines covered (66.01%)

4.58 hits per line

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

100.0
/src/smartcard/Exceptions.py
1
"""Smartcard module exceptions.
2

3
This module defines the exceptions raised by the smartcard module.
4

5
__author__ = "https://www.gemalto.com/"
6

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

10
This file is part of pyscard.
11

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

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

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

27
from smartcard.scard import SCardGetErrorMessage
20✔
28

29

30
class SmartcardException(Exception):
20✔
31
    """Base class for smartcard exceptions.
32

33
    smartcard exceptions are generated by the smartcard module and
34
    shield scard (i.e. PCSC) exceptions raised by the scard module.
35

36
    """
37

38
    def __init__(self, message="", hresult=-1, *args):
20✔
39
        super().__init__(message, *args)
18✔
40
        self.hresult = int(hresult)
18✔
41

42
    def __str__(self):
20✔
43
        text = super().__str__()
18✔
44
        if self.hresult != -1:
18✔
45
            if text:
17✔
46
                text += ": "
17✔
47
            hresult = self.hresult
17✔
48
            if hresult < 0:
17✔
49
                # convert 0x-7FEFFFE3 into 0x8010001D
50
                hresult += 0x100000000
13✔
51
            text += f"{SCardGetErrorMessage(self.hresult)} (0x{hresult:08X})"
17✔
52

53
        return text
18✔
54

55

56
class CardConnectionException(SmartcardException):
20✔
57
    """Raised when a CardConnection class method fails."""
58

59
    pass
20✔
60

61

62
class CardRequestException(SmartcardException):
20✔
63
    """Raised when a CardRequest wait fails."""
64

65
    pass
20✔
66

67

68
class CardRequestTimeoutException(SmartcardException):
20✔
69
    """Raised when a CardRequest times out."""
70

71
    def __init__(self, hresult=-1, *args):
20✔
72
        SmartcardException.__init__(
17✔
73
            self, "Time-out during card request", hresult=hresult, *args
74
        )
75

76

77
class CardServiceException(SmartcardException):
20✔
78
    """Raised when a CardService class method fails."""
79

80
    pass
20✔
81

82

83
class CardServiceStoppedException(SmartcardException):
20✔
84
    """Raised when the CardService was stopped"""
85

86
    pass
20✔
87

88

89
class CardServiceNotFoundException(SmartcardException):
20✔
90
    """Raised when the CardService is not found"""
91

92
    pass
20✔
93

94

95
class InvalidATRMaskLengthException(SmartcardException):
20✔
96
    """Raised when an ATR mask does not match an ATR length."""
97

98
    def __init__(self, mask):
20✔
99
        SmartcardException.__init__(self, "Invalid ATR mask length: %s" % mask)
17✔
100

101

102
class InvalidReaderException(SmartcardException):
20✔
103
    """Raised when trying to access an invalid smartcard reader."""
104

105
    def __init__(self, readername):
20✔
106
        SmartcardException.__init__(self, "Invalid reader: %s" % readername)
17✔
107

108

109
class ListReadersException(SmartcardException):
20✔
110
    """Raised when smartcard readers cannot be listed."""
111

112
    def __init__(self, hresult):
20✔
113
        SmartcardException.__init__(self, "Failed to list readers", hresult=hresult)
17✔
114

115

116
class NoCardException(SmartcardException):
20✔
117
    """Raised when no card in is present in reader."""
118

119
    def __init__(self, message, hresult):
20✔
120
        SmartcardException.__init__(self, message, hresult=hresult)
17✔
121

122

123
class NoReadersException(SmartcardException):
20✔
124
    """Raised when the system has no smartcard reader."""
125

126
    def __init__(self, *args):
20✔
127
        SmartcardException.__init__(self, "No reader found", *args)
17✔
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