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

LudovicRousseau / pyscard / 11323269743

14 Oct 2024 07:55AM UTC coverage: 64.919% (+0.3%) from 64.591%
11323269743

push

github

LudovicRousseau
Test and improve the synchronization code

Added
-----

* Add tests to bring `Synchronization.py` to 100% coverage
* Add type annotations to the synchronization primitives
* Add `typing_extensions` as a Python 3.9 requirement;
  this is needed to annotate that the `synchronized()` decorator
  takes a function with a certain set of parameter and return types
  and returns a function with the same parameter and return types

Changed
-------

* Make the synchronized function's `self` parameter explicit;
  this allows the `self` parameter to be type-annotated
  so the dependency on the `self.mutex` attribute is explicit
* Use the `self.mutex` lock as a context manager
* Support keyword arguments to synchronized functions

Fixed
-----

* Wrap synchronized functions correctly;
  previous behavior was to lose the function name and docstring

Removed
-------

* Remove `print()` lines that are commented out
* Remove a `bytes` instance check; method names can only be strings

100 of 466 branches covered (21.46%)

21 of 22 new or added lines in 1 file covered. (95.45%)

34 existing lines in 22 files now uncovered.

4210 of 6485 relevant lines covered (64.92%)

4.16 hits per line

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

100.0
/src/smartcard/Exceptions.py
1
"""Smartcard module exceptions.
2

3
This module defines the exceptions raised by the smartcard module.
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
from smartcard.scard import SCardGetErrorMessage
20✔
28

29

30
class SmartcardException(Exception):
20✔
31
    """Base class for smartcard exceptions.
32

33
    smartcard exceptions are generated by the smartcard module and
34
    shield scard (i.e. PCSC) exceptions raised by the scard module.
35

36
    """
37

38
    def __init__(self, message="", hresult=-1, *args):
20✔
39
        super().__init__(message, *args)
20✔
40
        self.hresult = int(hresult)
20✔
41

42
    def __str__(self):
20✔
43
        text = super().__str__()
18✔
44
        if self.hresult != -1:
18✔
45
            hresult = self.hresult
18✔
46
            if hresult < 0:
18✔
47
                # convert 0x-7FEFFFE3 into 0x8010001D
UNCOV
48
                hresult += 0x100000000
14✔
49
            text += f": {SCardGetErrorMessage(self.hresult)} (0x{hresult:08X})"
18✔
50

51
        return text
18✔
52

53

54
class CardConnectionException(SmartcardException):
20✔
55
    """Raised when a CardConnection class method fails."""
56

57
    pass
20✔
58

59

60
class CardRequestException(SmartcardException):
20✔
61
    """Raised when a CardRequest wait fails."""
62

63
    pass
20✔
64

65

66
class CardRequestTimeoutException(SmartcardException):
20✔
67
    """Raised when a CardRequest times out."""
68

69
    def __init__(self, hresult=-1, *args):
20✔
70
        SmartcardException.__init__(
18✔
71
            self, "Time-out during card request", hresult=hresult, *args
72
        )
73

74

75
class CardServiceException(SmartcardException):
20✔
76
    """Raised when a CardService class method fails."""
77

78
    pass
20✔
79

80

81
class CardServiceStoppedException(SmartcardException):
20✔
82
    """Raised when the CardService was stopped"""
83

84
    pass
20✔
85

86

87
class CardServiceNotFoundException(SmartcardException):
20✔
88
    """Raised when the CardService is not found"""
89

90
    pass
20✔
91

92

93
class InvalidATRMaskLengthException(SmartcardException):
20✔
94
    """Raised when an ATR mask does not match an ATR length."""
95

96
    def __init__(self, mask):
20✔
97
        SmartcardException.__init__(self, "Invalid ATR mask length: %s" % mask)
18✔
98

99

100
class InvalidReaderException(SmartcardException):
20✔
101
    """Raised when trying to access an invalid smartcard reader."""
102

103
    def __init__(self, readername):
20✔
104
        SmartcardException.__init__(self, "Invalid reader: %s" % readername)
18✔
105

106

107
class ListReadersException(SmartcardException):
20✔
108
    """Raised when smartcard readers cannot be listed."""
109

110
    def __init__(self, hresult):
20✔
111
        SmartcardException.__init__(self, "Failed to list readers", hresult=hresult)
18✔
112

113

114
class NoCardException(SmartcardException):
20✔
115
    """Raised when no card in is present in reader."""
116

117
    def __init__(self, message, hresult):
20✔
118
        SmartcardException.__init__(self, message, hresult=hresult)
18✔
119

120

121
class NoReadersException(SmartcardException):
20✔
122
    """Raised when the system has no smartcard reader."""
123

124
    def __init__(self, *args):
20✔
125
        SmartcardException.__init__(self, "No reader found", *args)
18✔
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