annotate Lib/xdrlib.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents bd5e821f201c
children e95e647040de 67053b135ed9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Implements (a subset of) Sun XDR -- eXternal Data Representation.
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 See: RFC 1014
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 """
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import struct
42367
8ff899641563 Move xdrlib over to the bytes type.
Brett Cannon <bcannon@gmail.com>
parents: 41760
diff changeset
8 from io import BytesIO
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
10 __all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
11
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # exceptions
22703
e0ee009417df Derive exception classes from Exception
Neal Norwitz <nnorwitz@gmail.com>
parents: 19313
diff changeset
13 class Error(Exception):
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 """Exception class for this module. Use:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 except xdrlib.Error, var:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # var has the Error instance for the exception
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 Public ivars:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 msg -- contains the message
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 """
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 def __init__(self, msg):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
24 self.msg = msg
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 def __repr__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
26 return repr(self.msg)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 def __str__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
28 return str(self.msg)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 class ConversionError(Error):
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 pass
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
35
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 class Packer:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 """Pack various data representations into a buffer."""
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 def __init__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
40 self.reset()
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def reset(self):
42367
8ff899641563 Move xdrlib over to the bytes type.
Brett Cannon <bcannon@gmail.com>
parents: 41760
diff changeset
43 self.__buf = BytesIO()
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def get_buffer(self):
19313
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
46 return self.__buf.getvalue()
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # backwards compatibility
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 get_buf = get_buffer
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def pack_uint(self, x):
19313
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
51 self.__buf.write(struct.pack('>L', x))
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
69012
bd5e821f201c Issue #9696: Fix exception incorrectly raised by xdrlib.Packer.pack_int when trying to pack a negative (in-range) integer.
Mark Dickinson <mdickinson@enthought.com>
parents: 43020
diff changeset
53 def pack_int(self, x):
bd5e821f201c Issue #9696: Fix exception incorrectly raised by xdrlib.Packer.pack_int when trying to pack a negative (in-range) integer.
Mark Dickinson <mdickinson@enthought.com>
parents: 43020
diff changeset
54 self.__buf.write(struct.pack('>l', x))
bd5e821f201c Issue #9696: Fix exception incorrectly raised by xdrlib.Packer.pack_int when trying to pack a negative (in-range) integer.
Mark Dickinson <mdickinson@enthought.com>
parents: 43020
diff changeset
55
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 pack_enum = pack_int
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def pack_bool(self, x):
42367
8ff899641563 Move xdrlib over to the bytes type.
Brett Cannon <bcannon@gmail.com>
parents: 41760
diff changeset
59 if x: self.__buf.write(b'\0\0\0\1')
8ff899641563 Move xdrlib over to the bytes type.
Brett Cannon <bcannon@gmail.com>
parents: 41760
diff changeset
60 else: self.__buf.write(b'\0\0\0\0')
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 def pack_uhyper(self, x):
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
63 self.pack_uint(x>>32 & 0xffffffff)
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
64 self.pack_uint(x & 0xffffffff)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 pack_hyper = pack_uhyper
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 def pack_float(self, x):
19313
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
69 try: self.__buf.write(struct.pack('>f', x))
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
70 except struct.error as msg:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42367
diff changeset
71 raise ConversionError(msg)
4860
5c6fbdc50e1d Use the new struct module's ability to pack and unpack standardized
Guido van Rossum <guido@python.org>
parents: 4578
diff changeset
72
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def pack_double(self, x):
19313
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
74 try: self.__buf.write(struct.pack('>d', x))
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
75 except struct.error as msg:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42367
diff changeset
76 raise ConversionError(msg)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 def pack_fstring(self, n, s):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
79 if n < 0:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42367
diff changeset
80 raise ValueError('fstring size must be nonnegative')
35656
a2cf25642c8e patch [ 1300515 ] xdrlib.py: pack_fstring() did not use null bytes for padding
Georg Brandl <georg@python.org>
parents: 34632
diff changeset
81 data = s[:n]
37870
c4341e6c6a03 Fix typical truedivision problem (using the result of division as an index.)
Thomas Wouters <thomas@python.org>
parents: 35656
diff changeset
82 n = ((n+3)//4)*4
42367
8ff899641563 Move xdrlib over to the bytes type.
Brett Cannon <bcannon@gmail.com>
parents: 41760
diff changeset
83 data = data + (n - len(data)) * b'\0'
19313
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
84 self.__buf.write(data)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 pack_fopaque = pack_fstring
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def pack_string(self, s):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
89 n = len(s)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
90 self.pack_uint(n)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
91 self.pack_fstring(n, s)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 pack_opaque = pack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 pack_bytes = pack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 def pack_list(self, list, pack_item):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
97 for item in list:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
98 self.pack_uint(1)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
99 pack_item(item)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
100 self.pack_uint(0)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 def pack_farray(self, n, list, pack_item):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 7755
diff changeset
103 if len(list) != n:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42367
diff changeset
104 raise ValueError('wrong array size')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
105 for item in list:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
106 pack_item(item)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 def pack_array(self, list, pack_item):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
109 n = len(list)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
110 self.pack_uint(n)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
111 self.pack_farray(n, list, pack_item)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
114
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 class Unpacker:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 """Unpacks various data representations from the given buffer."""
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 def __init__(self, data):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
119 self.reset(data)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def reset(self, data):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
122 self.__buf = data
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
123 self.__pos = 0
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 def get_position(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
126 return self.__pos
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 def set_position(self, position):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
129 self.__pos = position
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
4578
6f1a120ae9c0 Unpacker.get_buffer(): new method to access the internal buffer of data
Barry Warsaw <barry@python.org>
parents: 4450
diff changeset
131 def get_buffer(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
132 return self.__buf
4578
6f1a120ae9c0 Unpacker.get_buffer(): new method to access the internal buffer of data
Barry Warsaw <barry@python.org>
parents: 4450
diff changeset
133
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 def done(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
135 if self.__pos < len(self.__buf):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
136 raise Error('unextracted data remains')
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 def unpack_uint(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
139 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
140 self.__pos = j = i+4
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
141 data = self.__buf[i:j]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
142 if len(data) < 4:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
143 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
144 x = struct.unpack('>L', data)[0]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
145 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
146 return int(x)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
147 except OverflowError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
148 return x
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 def unpack_int(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
151 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
152 self.__pos = j = i+4
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
153 data = self.__buf[i:j]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
154 if len(data) < 4:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
155 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
156 return struct.unpack('>l', data)[0]
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 unpack_enum = unpack_int
34632
77b57087fe45 Patch #1049151: adding bool support to xdrlib.py.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
159
77b57087fe45 Patch #1049151: adding bool support to xdrlib.py.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
160 def unpack_bool(self):
77b57087fe45 Patch #1049151: adding bool support to xdrlib.py.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
161 return bool(self.unpack_int())
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 def unpack_uhyper(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
164 hi = self.unpack_uint()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
165 lo = self.unpack_uint()
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
166 return int(hi)<<32 | lo
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def unpack_hyper(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
169 x = self.unpack_uhyper()
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
170 if x >= 0x8000000000000000:
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
171 x = x - 0x10000000000000000
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
172 return x
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 def unpack_float(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
175 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
176 self.__pos = j = i+4
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
177 data = self.__buf[i:j]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
178 if len(data) < 4:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
179 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
180 return struct.unpack('>f', data)[0]
4860
5c6fbdc50e1d Use the new struct module's ability to pack and unpack standardized
Guido van Rossum <guido@python.org>
parents: 4578
diff changeset
181
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 def unpack_double(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
183 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
184 self.__pos = j = i+8
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
185 data = self.__buf[i:j]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
186 if len(data) < 8:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
187 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
188 return struct.unpack('>d', data)[0]
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 def unpack_fstring(self, n):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
191 if n < 0:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42367
diff changeset
192 raise ValueError('fstring size must be nonnegative')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
193 i = self.__pos
37870
c4341e6c6a03 Fix typical truedivision problem (using the result of division as an index.)
Thomas Wouters <thomas@python.org>
parents: 35656
diff changeset
194 j = i + (n+3)//4*4
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
195 if j > len(self.__buf):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
196 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
197 self.__pos = j
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
198 return self.__buf[i:i+n]
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 unpack_fopaque = unpack_fstring
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 def unpack_string(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
203 n = self.unpack_uint()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
204 return self.unpack_fstring(n)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 unpack_opaque = unpack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 unpack_bytes = unpack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 def unpack_list(self, unpack_item):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
210 list = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
211 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
212 x = self.unpack_uint()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
213 if x == 0: break
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 7755
diff changeset
214 if x != 1:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42367
diff changeset
215 raise ConversionError('0 or 1 expected, got %r' % (x,))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
216 item = unpack_item()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
217 list.append(item)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
218 return list
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 def unpack_farray(self, n, unpack_item):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
221 list = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
222 for i in range(n):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
223 list.append(unpack_item())
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
224 return list
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 def unpack_array(self, unpack_item):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
227 n = self.unpack_uint()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
228 return self.unpack_farray(n, unpack_item)