1 """Smartcard module exceptions.
2
3 This module defines the exceptions raised by the smartcard.pcsc modules.
4
5 __author__ = "http://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
28 import smartcard.scard
29
30
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
40 """Constructor that stores the pcsc error status."""
41 self.hresult = hresult
42
47
48
50 """Raised when scard fails to add a new reader to a PCSC reader group."""
51
52 - def __init__(self, hresult, readername="", groupname=""):
56
61
62
63 -class EstablishContextException(BaseSCardException):
64 """Raised when scard failed to establish context with PCSC."""
65
67 """Returns a string representation of the exception."""
68 return repr('Failure to establish context: ' +
69 smartcard.scard.SCardGetErrorMessage(self.hresult))
70
71
73 """Raised when scard failed to list readers."""
74
78
79
81 """Raised when scard fails to introduce a new reader to PCSC."""
82
83 - def __init__(self, hresult, readername=""):
86
90
91
92 -class ReleaseContextException(BaseSCardException):
93 """Raised when scard failed to release PCSC context."""
94
96 return repr('Failure to release context: ' +
97 smartcard.scard.SCardGetErrorMessage(self.hresult))
98
99
101 """Raised when scard fails to remove a reader from a PCSC reader group."""
102
103 - def __init__(self, hresult, readername="", groupname=""):
107
112
113 if __name__ == "__main__":
114 try:
115 raise EstablishContextException(smartcard.scard.SCARD_E_NO_MEMORY)
116 except BaseSCardException as exc:
117 print(exc)
118