annotate Lib/xdrlib.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents d800d6737556
children d3f9a6d7f6e4 6b18a095c2e7
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
19313
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
8 try:
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
9 from cStringIO import StringIO as _StringIO
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
10 except ImportError:
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
11 from StringIO import StringIO as _StringIO
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 15874
diff changeset
13 __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
14
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # exceptions
22703
e0ee009417df Derive exception classes from Exception
Neal Norwitz <nnorwitz@gmail.com>
parents: 19313
diff changeset
16 class Error(Exception):
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 """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
18
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 except xdrlib.Error, var:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # 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
21
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 Public ivars:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 msg -- contains the message
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 """
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 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
27 self.msg = msg
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 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
29 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
30 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
31 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
32
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 class ConversionError(Error):
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 pass
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
38
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 class Packer:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 """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
41
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 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
43 self.reset()
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 reset(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 self.__buf = _StringIO()
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 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
49 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
50 # backwards compatibility
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 get_buf = get_buffer
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 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
54 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
55
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 pack_int = pack_uint
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 pack_enum = pack_int
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 def pack_bool(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
60 if x: self.__buf.write('\0\0\0\1')
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
61 else: self.__buf.write('\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
62
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 def pack_uhyper(self, x):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
64 self.pack_uint(x>>32 & 0xffffffffL)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
65 self.pack_uint(x & 0xffffffffL)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 pack_hyper = pack_uhyper
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 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
70 try: self.__buf.write(struct.pack('>f', x))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
71 except struct.error, msg:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
72 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
73
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 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
75 try: self.__buf.write(struct.pack('>d', x))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
76 except struct.error, msg:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
77 raise ConversionError, msg
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 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
80 if n < 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
81 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
82 data = s[:n]
37278
bb1b48516131 Make xdrlib use floor division instead of classic division.
Georg Brandl <georg@python.org>
parents: 35656
diff changeset
83 n = ((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
84 data = data + (n - len(data)) * '\0'
19313
6f62f5a62a2c Use (c)StringIO for collecting bytes. Fixes bug #451622.
Martin v. Löwis <martin@v.loewis.de>
parents: 16889
diff changeset
85 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
86
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 pack_fopaque = pack_fstring
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 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
90 n = len(s)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
91 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
92 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
93
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 pack_opaque = pack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 pack_bytes = pack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 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
98 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
99 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
100 pack_item(item)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
101 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
102
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 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
104 if len(list) != n:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
105 raise ValueError, 'wrong array size'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
106 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
107 pack_item(item)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 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
110 n = len(list)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
111 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
112 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
113
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
115
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 class Unpacker:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 """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
118
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 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
120 self.reset(data)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 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
123 self.__buf = data
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
124 self.__pos = 0
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 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
127 return self.__pos
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 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
130 self.__pos = position
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
4578
6f1a120ae9c0 Unpacker.get_buffer(): new method to access the internal buffer of data
Barry Warsaw <barry@python.org>
parents: 4450
diff changeset
132 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
133 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
134
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 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
136 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
137 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
138
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 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
140 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
141 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
142 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
143 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
144 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
145 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
146 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
147 return int(x)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
148 except OverflowError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
149 return x
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 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
152 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
153 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
154 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
155 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
156 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
157 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
158
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 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
160
77b57087fe45 Patch #1049151: adding bool support to xdrlib.py.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
161 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
162 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
163
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 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
165 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
166 lo = self.unpack_uint()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
167 return long(hi)<<32 | lo
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 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
170 x = self.unpack_uhyper()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
171 if x >= 0x8000000000000000L:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
172 x = x - 0x10000000000000000L
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
173 return x
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 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
176 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
177 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
178 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
179 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
180 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
181 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
182
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 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
184 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
185 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
186 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
187 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
188 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
189 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
190
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 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
192 if n < 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
193 raise ValueError, 'fstring size must be nonnegative'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
194 i = self.__pos
37278
bb1b48516131 Make xdrlib use floor division instead of classic division.
Georg Brandl <georg@python.org>
parents: 35656
diff changeset
195 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
196 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
197 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
198 self.__pos = j
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
199 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
200
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 unpack_fopaque = unpack_fstring
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 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
204 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
205 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
206
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 unpack_opaque = unpack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 unpack_bytes = unpack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 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
211 list = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
212 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
213 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
214 if x == 0: break
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 7755
diff changeset
215 if x != 1:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
216 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
217 item = unpack_item()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
218 list.append(item)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
219 return list
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 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
222 list = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
223 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
224 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
225 return list
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 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
228 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
229 return self.unpack_farray(n, unpack_item)