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

LudovicRousseau / pyscard / 17499293285

05 Sep 2025 04:47PM UTC coverage: 64.674% (-0.3%) from 64.994%
17499293285

push

github

web-flow
Merge pull request #233 from LudovicRousseau/dependabot/github_actions/github-actions-a331d3ec2d

build(deps): bump actions/checkout from 4 to 5 in the github-actions group

106 of 475 branches covered (22.32%)

4046 of 6256 relevant lines covered (64.67%)

2.87 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
12✔
29

30

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

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

61

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

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

73

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

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

80

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

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

87

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

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

97

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

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

104

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

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

120

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