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

LudovicRousseau / pyscard / 20728839540

05 Jan 2026 08:53PM UTC coverage: 67.469%. Remained the same
20728839540

push

github

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

[pre-commit.ci] pre-commit autoupdate

108 of 485 branches covered (22.27%)

4739 of 7024 relevant lines covered (67.47%)

5.74 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
24✔
29

30

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

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

61

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

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

73

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

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

80

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

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

87

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

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

97

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

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

104

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

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

120

121
if __name__ == "__main__":
24✔
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