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

LudovicRousseau / pyscard / 18294853570

06 Oct 2025 09:23PM UTC coverage: 64.994%. Remained the same
18294853570

push

github

web-flow
Merge pull request #239 from LudovicRousseau/pre-commit-ci-update-config

[pre-commit.ci] pre-commit autoupdate

106 of 475 branches covered (22.32%)

4066 of 6256 relevant lines covered (64.99%)

4.98 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
19✔
28

29

30
class SmartcardException(Exception):
19✔
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, *args, message="", hresult=-1):
19✔
39
        if not message and len(args) > 0:
19✔
40
            message = args[0]
19✔
41
            args = args[1:]
19✔
42
        if -1 == hresult and len(args) > 0:
19✔
43
            hresult = args[0]
17✔
44
            args = args[1:]
17✔
45
        super().__init__(message, *args)
19✔
46
        self.hresult = int(hresult)
19✔
47

48
    def __str__(self):
19✔
49
        text = super().__str__()
19✔
50
        if self.hresult != -1:
19✔
51
            if text:
17✔
52
                text += ": "
17✔
53
            hresult = self.hresult
17✔
54
            if hresult < 0:
17✔
55
                # convert 0x-7FEFFFE3 into 0x8010001D
56
                hresult += 0x100000000
13✔
57
            text += f"{SCardGetErrorMessage(self.hresult)} (0x{hresult:08X})"
17✔
58

59
        return text
19✔
60

61

62
class CardConnectionException(SmartcardException):
19✔
63
    """Raised when a CardConnection class method fails."""
64

65

66
class CardRequestException(SmartcardException):
19✔
67
    """Raised when a CardRequest wait fails."""
68

69

70
class CardRequestTimeoutException(SmartcardException):
19✔
71
    """Raised when a CardRequest times out."""
72

73
    def __init__(self, *args, hresult=-1):
19✔
74
        if -1 == hresult and len(args) > 0:
17✔
75
            hresult = args[0]
17✔
76
            args = args[1:]
17✔
77
        SmartcardException.__init__(
17✔
78
            self, "Time-out during card request", hresult=hresult, *args
79
        )
80

81

82
class CardServiceException(SmartcardException):
19✔
83
    """Raised when a CardService class method fails."""
84

85

86
class CardServiceStoppedException(SmartcardException):
19✔
87
    """Raised when the CardService was stopped"""
88

89

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

93

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

97
    def __init__(self, mask):
19✔
98
        SmartcardException.__init__(self, f"Invalid ATR mask length: {mask}")
17✔
99

100

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

104
    def __init__(self, readername):
19✔
105
        SmartcardException.__init__(self, f"Invalid reader: {readername}")
17✔
106

107

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

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

114

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

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

121

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

125
    def __init__(self, *args):
19✔
126
        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