• 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

91.11
/src/smartcard/pcsc/PCSCExceptions.py
1
"""Smartcard module exceptions.
2

3
This module defines the exceptions raised by the smartcard.pcsc modules.
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
# gemalto scard library
28
import smartcard.scard
20✔
29

30

31
class BaseSCardException(Exception):
20✔
32
    """Base class for scard (aka PCSC) exceptions.
33

34
    scard exceptions are raised by the scard module, i.e.
35
    low-level PCSC access to readers and cards.
36

37
    """
38

39
    def __init__(self, hresult=-1, message="", *args):
20✔
40
        """Constructor that stores the pcsc error status."""
41
        if not message:
18✔
42
            message = "scard exception"
18✔
43
        super().__init__(message, *args)
18✔
44
        self.message = message
18✔
45
        self.hresult = hresult
18✔
46

47
    def __str__(self):
20✔
48
        """Returns a string representation of the exception."""
49
        text = super().__str__()
18✔
50
        if self.hresult != -1:
18✔
51
            hresult = self.hresult
18✔
52
            if hresult < 0:
18✔
53
                # convert 0x-7FEFFFE3 into 0x8010001D
54
                hresult += 0x100000000
14✔
55
            text += f": {smartcard.scard.SCardGetErrorMessage(self.hresult)} (0x{hresult:08X})"
18✔
56
        return text
18✔
57

58

59
class AddReaderToGroupException(BaseSCardException):
20✔
60
    """Raised when scard fails to add a new reader to a PCSC reader group."""
61

62
    def __init__(self, hresult, readername="", groupname=""):
20✔
63
        super().__init__(
18✔
64
            message="Failed to add reader: " + readername + " to group: " + groupname,
65
            hresult=hresult,
66
        )
67
        self.readername = readername
18✔
68
        self.groupname = groupname
18✔
69

70

71
class EstablishContextException(BaseSCardException):
20✔
72
    """Raised when scard failed to establish context with PCSC."""
73

74
    def __init__(self, hresult):
20✔
75
        super().__init__(message="Failed to establish context", hresult=hresult)
18✔
76

77

78
class ListReadersException(BaseSCardException):
20✔
79
    """Raised when scard failed to list readers."""
80

81
    def __init__(self, hresult):
20✔
82
        super().__init__(message="Failed to list readers", hresult=hresult)
18✔
83

84

85
class IntroduceReaderException(BaseSCardException):
20✔
86
    """Raised when scard fails to introduce a new reader to PCSC."""
87

88
    def __init__(self, hresult, readername=""):
20✔
89
        super().__init__(
18✔
90
            message="Failed to introduce a new reader: " + readername, hresult=hresult
91
        )
92
        self.readername = readername
18✔
93

94

95
class ReleaseContextException(BaseSCardException):
20✔
96
    """Raised when scard failed to release PCSC context."""
97

98
    def __init__(self, hresult):
20✔
99
        super().__init__(message="Failed to release context", hresult=hresult)
18✔
100

101

102
class RemoveReaderFromGroupException(BaseSCardException):
20✔
103
    """Raised when scard fails to remove a reader from a PCSC reader group."""
104

105
    def __init__(self, hresult, readername="", groupname=""):
20✔
106
        BaseSCardException.__init__(self, hresult)
18✔
107
        self.readername = readername
18✔
108
        self.groupname = groupname
18✔
109
        super().__init__(
18✔
110
            message="Failed to remove reader: "
111
            + readername
112
            + " from group: "
113
            + groupname,
114
            hresult=hresult,
115
        )
116

117

118
if __name__ == "__main__":
20✔
119
    try:
×
120
        raise EstablishContextException(smartcard.scard.SCARD_E_NO_MEMORY)
×
121
    except BaseSCardException as exc:
×
122
        print(exc)
×
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