annotate Lib/xdrlib.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents d3f9a6d7f6e4
children
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
69010
d3f9a6d7f6e4 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: 40412
diff changeset
56 def pack_int(self, x):
d3f9a6d7f6e4 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: 40412
diff changeset
57 self.__buf.write(struct.pack('>l', x))
d3f9a6d7f6e4 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: 40412
diff changeset
58
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 pack_enum = pack_int
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 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
62 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
63 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
64
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 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
66 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
67 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
68
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 pack_hyper = pack_uhyper
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 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
72 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
73 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
74 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
75
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 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
77 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
78 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
79 raise ConversionError, msg
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 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
82 if n < 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
83 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
84 data = s[:n]
37278
bb1b48516131 Make xdrlib use floor division instead of classic division.
Georg Brandl <georg@python.org>
parents: 35656
diff changeset
85 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
86 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
87 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
88
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 pack_fopaque = pack_fstring
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 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
92 n = len(s)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
93 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
94 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
95
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 pack_opaque = pack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 pack_bytes = pack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 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
100 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
101 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
102 pack_item(item)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
103 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
104
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 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
106 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
107 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
108 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
109 pack_item(item)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 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
112 n = len(list)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
113 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
114 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
115
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
15874
4c01ee0a8073 Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
117
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 class Unpacker:
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 """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
120
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 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
122 self.reset(data)
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 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
125 self.__buf = data
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
126 self.__pos = 0
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 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
129 return self.__pos
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 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
132 self.__pos = position
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
4578
6f1a120ae9c0 Unpacker.get_buffer(): new method to access the internal buffer of data
Barry Warsaw <barry@python.org>
parents: 4450
diff changeset
134 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
135 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
136
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 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
138 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
139 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
140
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 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
142 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
143 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
144 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
145 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
146 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
147 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
148 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
149 return int(x)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
150 except OverflowError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
151 return x
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 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
154 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
155 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
156 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
157 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
158 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
159 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
160
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 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
162
77b57087fe45 Patch #1049151: adding bool support to xdrlib.py.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
163 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
164 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
165
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 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
167 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
168 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
169 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
170
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 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
172 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
173 if x >= 0x8000000000000000L:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
174 x = x - 0x10000000000000000L
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
175 return x
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 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
178 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
179 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
180 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
181 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
182 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
183 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
184
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 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
186 i = self.__pos
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
187 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
188 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
189 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
190 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
191 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
192
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 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
194 if n < 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
195 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
196 i = self.__pos
37278
bb1b48516131 Make xdrlib use floor division instead of classic division.
Georg Brandl <georg@python.org>
parents: 35656
diff changeset
197 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
198 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
199 raise EOFError
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
200 self.__pos = j
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
201 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
202
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 unpack_fopaque = unpack_fstring
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 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
206 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
207 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
208
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 unpack_opaque = unpack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 unpack_bytes = unpack_string
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 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
213 list = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
214 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
215 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
216 if x == 0: break
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 7755
diff changeset
217 if x != 1:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
218 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
219 item = unpack_item()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
220 list.append(item)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
221 return list
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 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
224 list = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5665
diff changeset
225 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
226 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
227 return list
4045
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
96e0a7204e31 XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 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
230 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
231 return self.unpack_farray(n, unpack_item)