Mercurial > cpython-lru_cache-weakref
annotate Lib/xdrlib.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 6f62f5a62a2c |
| children | e0ee009417df |
| 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 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 class Error: |
|
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' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
82 n = ((n+3)/4)*4 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
83 data = s[:n] |
|
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 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 unpack_bool = unpack_int |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 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
|
163 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
|
164 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
|
165 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
|
166 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 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
|
168 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
|
169 if x >= 0x8000000000000000L: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
170 x = x - 0x10000000000000000L |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
171 return x |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 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
|
174 i = self.__pos |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 raise EOFError |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
179 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
|
180 |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 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
|
182 i = self.__pos |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
183 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
|
184 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
|
185 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
|
186 raise EOFError |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
187 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
|
188 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 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
|
190 if n < 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
191 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
|
192 i = self.__pos |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
193 j = i + (n+3)/4*4 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
194 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
|
195 raise EOFError |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
196 self.__pos = j |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
197 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
|
198 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 unpack_fopaque = unpack_fstring |
|
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 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
|
202 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
|
203 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
|
204 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 unpack_opaque = unpack_string |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 unpack_bytes = unpack_string |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 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
|
209 list = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
210 while 1: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
211 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
|
212 if x == 0: break |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
7755
diff
changeset
|
213 if x != 1: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
214 raise ConversionError, '0 or 1 expected, got ' + `x` |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
215 item = unpack_item() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
216 list.append(item) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
217 return list |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 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
|
220 list = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
221 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
|
222 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
|
223 return list |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 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
|
226 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
|
227 return self.unpack_farray(n, unpack_item) |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
229 |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 # test suite |
|
4046
eb9fc404c066
Rename __test() to _test().
Guido van Rossum <guido@python.org>
parents:
4045
diff
changeset
|
231 def _test(): |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 p = Packer() |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 packtest = [ |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
234 (p.pack_uint, (9,)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
235 (p.pack_bool, (None,)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
236 (p.pack_bool, ('hello',)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
237 (p.pack_uhyper, (45L,)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
238 (p.pack_float, (1.9,)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
239 (p.pack_double, (1.9,)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
240 (p.pack_string, ('hello world',)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
241 (p.pack_list, (range(5), p.pack_uint)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
242 (p.pack_array, (['what', 'is', 'hapnin', 'doctor'], p.pack_string)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
243 ] |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 succeedlist = [1] * len(packtest) |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 count = 0 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 for method, args in packtest: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
247 print 'pack test', count, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
248 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
249 apply(method, args) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
250 print 'succeeded' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
251 except ConversionError, var: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
252 print 'ConversionError:', var.msg |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
253 succeedlist[count] = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
254 count = count + 1 |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 data = p.get_buffer() |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 # now verify |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 up = Unpacker(data) |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 unpacktest = [ |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
259 (up.unpack_uint, (), lambda x: x == 9), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
260 (up.unpack_bool, (), lambda x: not x), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
261 (up.unpack_bool, (), lambda x: x), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
262 (up.unpack_uhyper, (), lambda x: x == 45L), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
263 (up.unpack_float, (), lambda x: 1.89 < x < 1.91), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
264 (up.unpack_double, (), lambda x: 1.89 < x < 1.91), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
265 (up.unpack_string, (), lambda x: x == 'hello world'), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
266 (up.unpack_list, (up.unpack_uint,), lambda x: x == range(5)), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
267 (up.unpack_array, (up.unpack_string,), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
268 lambda x: x == ['what', 'is', 'hapnin', 'doctor']), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
269 ] |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 count = 0 |
|
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 for method, args, pred in unpacktest: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
272 print 'unpack test', count, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
273 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
274 if succeedlist[count]: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
275 x = apply(method, args) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
276 print pred(x) and 'succeeded' or 'failed', ':', x |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
277 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
278 print 'skipping' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
279 except ConversionError, var: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
280 print 'ConversionError:', var.msg |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5665
diff
changeset
|
281 count = count + 1 |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
283 |
|
4045
96e0a7204e31
XDR interface, evolved from Demo/rpc/xdr.py by Barry.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 if __name__ == '__main__': |
|
4046
eb9fc404c066
Rename __test() to _test().
Guido van Rossum <guido@python.org>
parents:
4045
diff
changeset
|
285 _test() |
