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

kontron / python-ipmi / 5573391563

pending completion
5573391563

push

github

hthiery
tests: add check_completion_code unit tests

Signed-off-by: canteuni <21243338+canteuni@users.noreply.github.com>

4623 of 6665 relevant lines covered (69.36%)

4.15 hits per line

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

85.57
/pyipmi/utils.py
1
# Copyright (c) 2014  Kontron Europe GmbH
2
#
3
# This library is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU Lesser General Public
5
# License as published by the Free Software Foundation; either
6
# version 2.1 of the License, or (at your option) any later version.
7
#
8
# This library is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
# Lesser General Public License for more details.
12
#
13
# You should have received a copy of the GNU Lesser General Public
14
# License along with this library; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
16

17
import sys
6✔
18
import codecs
6✔
19
from array import array
6✔
20
from .msgs import constants
6✔
21
from .errors import DecodingError, CompletionCodeError
6✔
22

23

24
_PY3 = (sys.version_info >= (3,))
6✔
25

26

27
def py3enc_unic_bytes_fix(dat):
6✔
28
    # python 3 unicode fix
29
    if isinstance(dat, str) and _PY3:
×
30
        dat = dat.encode('raw_unicode_escape')
×
31
    return dat
×
32

33

34
def py3dec_unic_bytes_fix(dat):
6✔
35
    # python 3 unicode fix
36
    if _PY3:
6✔
37
        return dat.decode('raw_unicode_escape')
6✔
38
    return dat
×
39

40

41
def py3_array_frombytes(msg, data):
6✔
42
    if _PY3:
6✔
43
        return msg.frombytes(data)
6✔
44
    else:
45
        return msg.fromstring(data)
×
46

47

48
def py3_array_tobytes(msg):
6✔
49
    if _PY3:
6✔
50
        return msg.tobytes()
6✔
51
    else:
52
        return msg.tostring()
×
53

54

55
def check_completion_code(cc):
6✔
56
    if cc != constants.CC_OK:
6✔
57
        raise CompletionCodeError(cc)
6✔
58

59

60
def check_rsp_completion_code(rsp):
6✔
61
    """
62
    Check the completion code of a specific response and raise
63
    CompletionCodeError in case there's an error.
64

65
    This method allows to pass more metadata than the `check_completion_code`
66
    method to try to interpret command-specific completion codes description in
67
    case there is an error.
68

69
    `rsp` should be a subclass of `Message` here.
70
    """
71
    if rsp.completion_code != constants.CC_OK:
6✔
72
        raise CompletionCodeError(
6✔
73
            rsp.completion_code,
74
            cmdid=rsp.cmdid,
75
            netfn=rsp.netfn & 0xfe,  # Get the request NetFn from response NetFn
76
            group_extension=rsp.group_extension)
77

78

79
def chunks(data, count):
6✔
80
    for i in range(0, len(data), count):
6✔
81
        yield data[i:i+count]
6✔
82

83

84
class ByteBuffer(object):
6✔
85
    def __init__(self, data=None):
6✔
86

87
        if data is not None:
6✔
88
            self.array = array('B', data)
6✔
89
        else:
90
            self.array = array('B')
6✔
91

92
    def push_unsigned_int(self, value, length):
6✔
93
        for i in range(length):
6✔
94
            self.array.append((value >> (8*i) & 0xff))
6✔
95

96
    def pop_unsigned_int(self, length):
6✔
97
        value = 0
6✔
98
        for i in range(length):
6✔
99
            try:
6✔
100
                value |= self.array.pop(0) << (8*i)
6✔
101
            except IndexError:
6✔
102
                raise DecodingError('Data too short for message')
6✔
103
        return value
6✔
104

105
    def push_string(self, value):
6✔
106
        if _PY3 and isinstance(value, str):
6✔
107
            # Encode Unicode to UTF-8
108
            value = value.encode()
6✔
109
        py3_array_frombytes(self.array, value)
6✔
110

111
    def pop_string(self, length):
6✔
112
        string = self.array[0:length]
6✔
113
        del self.array[0:length]
6✔
114
        return py3_array_tobytes(string)
6✔
115
        # return py3dec_unic_bytes_fix(string.tostring())
116

117
    def pop_slice(self, length):
6✔
118
        if len(self.array) < length:
6✔
119
            raise DecodingError('Data too short for message')
6✔
120

121
        c = ByteBuffer(self.array[0:length])
6✔
122
        self.__delslice__(0, length)
6✔
123
        return c
6✔
124

125
    def tobytes(self):
6✔
126
        return self.array.tobytes()
×
127

128
    def tostring(self):
6✔
129
        return py3_array_tobytes(self.array)
6✔
130

131
    def extend(self, data):
6✔
132
        self.array.extend(data)
6✔
133

134
    def append_array(self, a):
6✔
135
        self.array.extend(a)
×
136

137
    def __getslice__(self, a, b):
6✔
138
        return self.array[a:b]
×
139

140
    def __delslice__(self, a, b):
6✔
141
        del self.array[a:b]
6✔
142

143
    def __len__(self):
6✔
144
        return len(self.array)
6✔
145

146
    def __getitem__(self, idx):
6✔
147
        return self.array[idx]
6✔
148

149

150
BCD_MAP = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '-', '.']
6✔
151

152

153
def bcd_encode(input, errors='strict'):
6✔
154
    raise NotImplementedError()
×
155

156

157
def bcd_decode(encoded_input):
6✔
158
    chars = list()
6✔
159
    try:
6✔
160
        for data in encoded_input:
6✔
161
            if not _PY3:
6✔
162
                data = ord(data)
×
163
            chars.append(BCD_MAP[data >> 4 & 0xf] + BCD_MAP[data & 0xf])
6✔
164
        return (''.join(chars), len(encoded_input) * 2)
6✔
165
    except IndexError:
×
166
        raise ValueError()
×
167

168

169
def bcd_search(name):
6✔
170
    # Python 3.9 normalizes 'bcd+' as 'bcd_'
171
    if name not in ('bcd+', 'bcd'):
6✔
172
        return None
×
173
    return codecs.CodecInfo(name='bcd+', encode=bcd_encode, decode=bcd_decode)
6✔
174

175

176
def is_string(string):
6✔
177
    if _PY3:
6✔
178
        return isinstance(string, str)
6✔
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