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

LudovicRousseau / pyscard / 16621389771

30 Jul 2025 11:33AM UTC coverage: 66.181% (+0.1%) from 66.048%
16621389771

push

github

LudovicRousseau
Release 2.3.0

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

108 of 485 branches covered (22.27%)

4452 of 6727 relevant lines covered (66.18%)

4.77 hits per line

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

91.67
/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, *args, hresult=-1, message=""):
20✔
40
        """Constructor that stores the pcsc error status."""
41
        if not message:
19✔
42
            message = "scard exception"
19✔
43
        if -1 == hresult and len(args) > 0:
19✔
44
            hresult = args[0]
19✔
45
            args = args[1:]
19✔
46
        super().__init__(message, *args)
19✔
47
        self.message = message
19✔
48
        self.hresult = hresult
19✔
49

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

61

62
class AddReaderToGroupException(BaseSCardException):
20✔
63
    """Raised when scard fails to add a new reader to a PCSC reader group."""
64

65
    def __init__(self, hresult, readername="", groupname=""):
20✔
66
        super().__init__(
19✔
67
            message="Failed to add reader: " + readername + " to group: " + groupname,
68
            hresult=hresult,
69
        )
70
        self.readername = readername
19✔
71
        self.groupname = groupname
19✔
72

73

74
class EstablishContextException(BaseSCardException):
20✔
75
    """Raised when scard failed to establish context with PCSC."""
76

77
    def __init__(self, hresult):
20✔
78
        super().__init__(message="Failed to establish context", hresult=hresult)
19✔
79

80

81
class ListReadersException(BaseSCardException):
20✔
82
    """Raised when scard failed to list readers."""
83

84
    def __init__(self, hresult):
20✔
85
        super().__init__(message="Failed to list readers", hresult=hresult)
19✔
86

87

88
class IntroduceReaderException(BaseSCardException):
20✔
89
    """Raised when scard fails to introduce a new reader to PCSC."""
90

91
    def __init__(self, hresult, readername=""):
20✔
92
        super().__init__(
19✔
93
            message="Failed to introduce a new reader: " + readername, hresult=hresult
94
        )
95
        self.readername = readername
19✔
96

97

98
class ReleaseContextException(BaseSCardException):
20✔
99
    """Raised when scard failed to release PCSC context."""
100

101
    def __init__(self, hresult):
20✔
102
        super().__init__(message="Failed to release context", hresult=hresult)
19✔
103

104

105
class RemoveReaderFromGroupException(BaseSCardException):
20✔
106
    """Raised when scard fails to remove a reader from a PCSC reader group."""
107

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

120

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