ยปCore Development>Code coverage>Lib/xdrlib.py

Python code coverage for Lib/xdrlib.py

#countcontent
1n/a"""Implements (a subset of) Sun XDR -- eXternal Data Representation.
2n/a
3n/aSee: RFC 1014
4n/a
5n/a"""
6n/a
7n/aimport struct
8n/afrom io import BytesIO
9n/afrom functools import wraps
10n/a
11n/a__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
12n/a
13n/a# exceptions
14n/aclass Error(Exception):
15n/a """Exception class for this module. Use:
16n/a
17n/a except xdrlib.Error as var:
18n/a # var has the Error instance for the exception
19n/a
20n/a Public ivars:
21n/a msg -- contains the message
22n/a
23n/a """
24n/a def __init__(self, msg):
25n/a self.msg = msg
26n/a def __repr__(self):
27n/a return repr(self.msg)
28n/a def __str__(self):
29n/a return str(self.msg)
30n/a
31n/a
32n/aclass ConversionError(Error):
33n/a pass
34n/a
35n/adef raise_conversion_error(function):
36n/a """ Wrap any raised struct.errors in a ConversionError. """
37n/a
38n/a @wraps(function)
39n/a def result(self, value):
40n/a try:
41n/a return function(self, value)
42n/a except struct.error as e:
43n/a raise ConversionError(e.args[0]) from None
44n/a return result
45n/a
46n/a
47n/aclass Packer:
48n/a """Pack various data representations into a buffer."""
49n/a
50n/a def __init__(self):
51n/a self.reset()
52n/a
53n/a def reset(self):
54n/a self.__buf = BytesIO()
55n/a
56n/a def get_buffer(self):
57n/a return self.__buf.getvalue()
58n/a # backwards compatibility
59n/a get_buf = get_buffer
60n/a
61n/a @raise_conversion_error
62n/a def pack_uint(self, x):
63n/a self.__buf.write(struct.pack('>L', x))
64n/a
65n/a @raise_conversion_error
66n/a def pack_int(self, x):
67n/a self.__buf.write(struct.pack('>l', x))
68n/a
69n/a pack_enum = pack_int
70n/a
71n/a def pack_bool(self, x):
72n/a if x: self.__buf.write(b'\0\0\0\1')
73n/a else: self.__buf.write(b'\0\0\0\0')
74n/a
75n/a def pack_uhyper(self, x):
76n/a try:
77n/a self.pack_uint(x>>32 & 0xffffffff)
78n/a except (TypeError, struct.error) as e:
79n/a raise ConversionError(e.args[0]) from None
80n/a try:
81n/a self.pack_uint(x & 0xffffffff)
82n/a except (TypeError, struct.error) as e:
83n/a raise ConversionError(e.args[0]) from None
84n/a
85n/a pack_hyper = pack_uhyper
86n/a
87n/a @raise_conversion_error
88n/a def pack_float(self, x):
89n/a self.__buf.write(struct.pack('>f', x))
90n/a
91n/a @raise_conversion_error
92n/a def pack_double(self, x):
93n/a self.__buf.write(struct.pack('>d', x))
94n/a
95n/a def pack_fstring(self, n, s):
96n/a if n < 0:
97n/a raise ValueError('fstring size must be nonnegative')
98n/a data = s[:n]
99n/a n = ((n+3)//4)*4
100n/a data = data + (n - len(data)) * b'\0'
101n/a self.__buf.write(data)
102n/a
103n/a pack_fopaque = pack_fstring
104n/a
105n/a def pack_string(self, s):
106n/a n = len(s)
107n/a self.pack_uint(n)
108n/a self.pack_fstring(n, s)
109n/a
110n/a pack_opaque = pack_string
111n/a pack_bytes = pack_string
112n/a
113n/a def pack_list(self, list, pack_item):
114n/a for item in list:
115n/a self.pack_uint(1)
116n/a pack_item(item)
117n/a self.pack_uint(0)
118n/a
119n/a def pack_farray(self, n, list, pack_item):
120n/a if len(list) != n:
121n/a raise ValueError('wrong array size')
122n/a for item in list:
123n/a pack_item(item)
124n/a
125n/a def pack_array(self, list, pack_item):
126n/a n = len(list)
127n/a self.pack_uint(n)
128n/a self.pack_farray(n, list, pack_item)
129n/a
130n/a
131n/a
132n/aclass Unpacker:
133n/a """Unpacks various data representations from the given buffer."""
134n/a
135n/a def __init__(self, data):
136n/a self.reset(data)
137n/a
138n/a def reset(self, data):
139n/a self.__buf = data
140n/a self.__pos = 0
141n/a
142n/a def get_position(self):
143n/a return self.__pos
144n/a
145n/a def set_position(self, position):
146n/a self.__pos = position
147n/a
148n/a def get_buffer(self):
149n/a return self.__buf
150n/a
151n/a def done(self):
152n/a if self.__pos < len(self.__buf):
153n/a raise Error('unextracted data remains')
154n/a
155n/a def unpack_uint(self):
156n/a i = self.__pos
157n/a self.__pos = j = i+4
158n/a data = self.__buf[i:j]
159n/a if len(data) < 4:
160n/a raise EOFError
161n/a return struct.unpack('>L', data)[0]
162n/a
163n/a def unpack_int(self):
164n/a i = self.__pos
165n/a self.__pos = j = i+4
166n/a data = self.__buf[i:j]
167n/a if len(data) < 4:
168n/a raise EOFError
169n/a return struct.unpack('>l', data)[0]
170n/a
171n/a unpack_enum = unpack_int
172n/a
173n/a def unpack_bool(self):
174n/a return bool(self.unpack_int())
175n/a
176n/a def unpack_uhyper(self):
177n/a hi = self.unpack_uint()
178n/a lo = self.unpack_uint()
179n/a return int(hi)<<32 | lo
180n/a
181n/a def unpack_hyper(self):
182n/a x = self.unpack_uhyper()
183n/a if x >= 0x8000000000000000:
184n/a x = x - 0x10000000000000000
185n/a return x
186n/a
187n/a def unpack_float(self):
188n/a i = self.__pos
189n/a self.__pos = j = i+4
190n/a data = self.__buf[i:j]
191n/a if len(data) < 4:
192n/a raise EOFError
193n/a return struct.unpack('>f', data)[0]
194n/a
195n/a def unpack_double(self):
196n/a i = self.__pos
197n/a self.__pos = j = i+8
198n/a data = self.__buf[i:j]
199n/a if len(data) < 8:
200n/a raise EOFError
201n/a return struct.unpack('>d', data)[0]
202n/a
203n/a def unpack_fstring(self, n):
204n/a if n < 0:
205n/a raise ValueError('fstring size must be nonnegative')
206n/a i = self.__pos
207n/a j = i + (n+3)//4*4
208n/a if j > len(self.__buf):
209n/a raise EOFError
210n/a self.__pos = j
211n/a return self.__buf[i:i+n]
212n/a
213n/a unpack_fopaque = unpack_fstring
214n/a
215n/a def unpack_string(self):
216n/a n = self.unpack_uint()
217n/a return self.unpack_fstring(n)
218n/a
219n/a unpack_opaque = unpack_string
220n/a unpack_bytes = unpack_string
221n/a
222n/a def unpack_list(self, unpack_item):
223n/a list = []
224n/a while 1:
225n/a x = self.unpack_uint()
226n/a if x == 0: break
227n/a if x != 1:
228n/a raise ConversionError('0 or 1 expected, got %r' % (x,))
229n/a item = unpack_item()
230n/a list.append(item)
231n/a return list
232n/a
233n/a def unpack_farray(self, n, unpack_item):
234n/a list = []
235n/a for i in range(n):
236n/a list.append(unpack_item())
237n/a return list
238n/a
239n/a def unpack_array(self, unpack_item):
240n/a n = self.unpack_uint()
241n/a return self.unpack_farray(n, unpack_item)