annotate Lib/binhex.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents 5c2d15c6007e
children 8c17e898e0e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
1 """Macintosh binhex compression/decompression.
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
2
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
3 easy interface:
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
4 binhex(inputfilename, outputfilename)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
5 hexbin(inputfilename, outputfilename)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
6 """
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
7
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
8 #
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
9 # Jack Jansen, CWI, August 1995.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
10 #
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
11 # The module is supposed to be as compatible as possible. Especially the
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
12 # easy interface should work "as expected" on any platform.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
13 # XXXX Note: currently, textfiles appear in mac-form on all platforms.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
14 # We seem to lack a simple character-translate in python.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
15 # (we should probably use ISO-Latin-1 on all but the mac platform).
13050
a195962e389c typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11708
diff changeset
16 # XXXX The simple routines are too simple: they expect to hold the complete
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
17 # files in-core. Should be fixed.
4972
6ece5cfb49ff As per GvR recomendations, remove the restriction that binhex only run on
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 3479
diff changeset
18 # XXXX It would be nice to handle AppleDouble format on unix
6ece5cfb49ff As per GvR recomendations, remove the restriction that binhex only run on
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 3479
diff changeset
19 # (for servers serving macs).
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
20 # XXXX I don't understand what happens when you get 0x90 times the same byte on
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
21 # input. The resulting code (xx 90 90) would appear to be interpreted as an
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
22 # escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
23 #
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
24 import io
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
25 import os
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
26 import sys
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27 import struct
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
28 import binascii
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
29
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15860
diff changeset
30 __all__ = ["binhex","hexbin","Error"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15860
diff changeset
31
14022
d44603f2b928 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
32 class Error(Exception):
d44603f2b928 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
33 pass
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
34
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
35 # States (what have we written)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36 [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
38 # Various constants
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
39 REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
40 LINELEN = 64
42992
ecda33051739 Convert various string literals to bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42934
diff changeset
41 RUNCHAR = b"\x90"
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
42
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
43 #
4972
6ece5cfb49ff As per GvR recomendations, remove the restriction that binhex only run on
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 3479
diff changeset
44 # This code is no longer byte-order dependent
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
45
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
46
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
47 class FInfo:
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
48 def __init__(self):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
49 self.Type = '????'
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
50 self.Creator = '????'
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
51 self.Flags = 0
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
52
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
53 def getfileinfo(name):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
54 finfo = FInfo()
68633
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
55 with io.open(name, 'rb') as fp:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
56 # Quick check for textfile
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
57 data = fp.read(512)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
58 if 0 not in data:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
59 finfo.Type = 'TEXT'
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
60 fp.seek(0, 2)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
61 dsize = fp.tell()
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
62 dir, file = os.path.split(name)
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
63 file = file.replace(':', '-', 1)
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
64 return file, finfo, dsize, 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
65
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
66 class openrsrc:
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
67 def __init__(self, *args):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
68 pass
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
69
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
70 def read(self, *args):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
71 return b''
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
72
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
73 def write(self, *args):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
74 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
75
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
76 def close(self):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
77 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
78
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
79 class _Hqxcoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
80 """Write data to the coder in 3-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
81
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
82 def __init__(self, ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
83 self.ofp = ofp
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
84 self.data = b''
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
85 self.hqxdata = b''
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
86 self.linelen = LINELEN - 1
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
87
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
88 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
89 self.data = self.data + data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
90 datalen = len(self.data)
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
91 todo = (datalen // 3) * 3
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
92 data = self.data[:todo]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
93 self.data = self.data[todo:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
94 if not data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
95 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
96 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
97 self._flush(0)
3479
aec8bbcf8b0e new binhex from Jack (much faster)
Guido van Rossum <guido@python.org>
parents: 3474
diff changeset
98
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
99 def _flush(self, force):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
100 first = 0
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
101 while first <= len(self.hqxdata) - self.linelen:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
102 last = first + self.linelen
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
103 self.ofp.write(self.hqxdata[first:last] + b'\n')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
104 self.linelen = LINELEN
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
105 first = last
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
106 self.hqxdata = self.hqxdata[first:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
107 if force:
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
108 self.ofp.write(self.hqxdata + b':\n')
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
109
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
110 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
111 if self.data:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
112 self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
113 self._flush(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
114 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
115 del self.ofp
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
116
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
117 class _Rlecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
118 """Write data to the RLE-coder in suitably large chunks"""
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
119
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
120 def __init__(self, ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
121 self.ofp = ofp
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
122 self.data = b''
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
123
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
124 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
125 self.data = self.data + data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
126 if len(self.data) < REASONABLY_LARGE:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
127 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
128 rledata = binascii.rlecode_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
129 self.ofp.write(rledata)
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
130 self.data = b''
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
131
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
132 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
133 if self.data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
134 rledata = binascii.rlecode_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
135 self.ofp.write(rledata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
136 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
137 del self.ofp
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
138
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
139 class BinHex:
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
140 def __init__(self, name_finfo_dlen_rlen, ofp):
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
141 name, finfo, dlen, rlen = name_finfo_dlen_rlen
68633
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
142 close_on_error = False
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42992
diff changeset
143 if isinstance(ofp, str):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
144 ofname = ofp
41864
99e33068c334 This is the last time I fix binhex. If it breaks again it goes in the dustbin.
Guido van Rossum <guido@python.org>
parents: 41828
diff changeset
145 ofp = io.open(ofname, 'wb')
68633
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
146 close_on_error = True
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
147 try:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
148 ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
149 hqxer = _Hqxcoderengine(ofp)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
150 self.ofp = _Rlecoderengine(hqxer)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
151 self.crc = 0
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
152 if finfo is None:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
153 finfo = FInfo()
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
154 self.dlen = dlen
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
155 self.rlen = rlen
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
156 self._writeinfo(name, finfo)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
157 self.state = _DID_HEADER
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
158 except:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
159 if close_on_error:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
160 ofp.close()
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
161 raise
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
162
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
163 def _writeinfo(self, name, finfo):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
164 nl = len(name)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
165 if nl > 63:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
166 raise Error('Filename too long')
42911
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
167 d = bytes([nl]) + name.encode("latin-1") + b'\0'
42933
b8465f02fcb1 Make this work on Mac as well (where Type and Creator are bytes instead of str).
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
168 tp, cr = finfo.Type, finfo.Creator
b8465f02fcb1 Make this work on Mac as well (where Type and Creator are bytes instead of str).
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
169 if isinstance(tp, str):
b8465f02fcb1 Make this work on Mac as well (where Type and Creator are bytes instead of str).
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
170 tp = tp.encode("latin-1")
b8465f02fcb1 Make this work on Mac as well (where Type and Creator are bytes instead of str).
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
171 if isinstance(cr, str):
b8465f02fcb1 Make this work on Mac as well (where Type and Creator are bytes instead of str).
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
172 cr = cr.encode("latin-1")
b8465f02fcb1 Make this work on Mac as well (where Type and Creator are bytes instead of str).
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
173 d2 = tp + cr
4972
6ece5cfb49ff As per GvR recomendations, remove the restriction that binhex only run on
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 3479
diff changeset
174
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
175 # Force all structs to be packed with big-endian
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
176 d3 = struct.pack('>h', finfo.Flags)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
177 d4 = struct.pack('>ii', self.dlen, self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
178 info = d + d2 + d3 + d4
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
179 self._write(info)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
180 self._writecrc()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
181
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
182 def _write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
183 self.crc = binascii.crc_hqx(data, self.crc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
184 self.ofp.write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
185
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
186 def _writecrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
187 # XXXX Should this be here??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
188 # self.crc = binascii.crc_hqx('\0\0', self.crc)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 32289
diff changeset
189 if self.crc < 0:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 32289
diff changeset
190 fmt = '>h'
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 32289
diff changeset
191 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 32289
diff changeset
192 fmt = '>H'
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 32289
diff changeset
193 self.ofp.write(struct.pack(fmt, self.crc))
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
194 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
195
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
196 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
197 if self.state != _DID_HEADER:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
198 raise Error('Writing data at the wrong time')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
199 self.dlen = self.dlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
200 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
201
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
202 def close_data(self):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
203 if self.dlen != 0:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
204 raise Error('Incorrect data size, diff=%r' % (self.rlen,))
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
205 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
206 self.state = _DID_DATA
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
207
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
208 def write_rsrc(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
209 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
210 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
211 if self.state != _DID_DATA:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
212 raise Error('Writing resource data at the wrong time')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
213 self.rlen = self.rlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
214 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
215
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
216 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
217 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
218 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
219 if self.state != _DID_DATA:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
220 raise Error('Close at the wrong time')
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
221 if self.rlen != 0:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
222 raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
223 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
224 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
225 self.state = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
226 del self.ofp
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
227
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
228 def binhex(inp, out):
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
229 """binhex(infilename, outfilename): create binhex-encoded copy of a file"""
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
230 finfo = getfileinfo(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
231 ofp = BinHex(finfo, out)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
232
41864
99e33068c334 This is the last time I fix binhex. If it breaks again it goes in the dustbin.
Guido van Rossum <guido@python.org>
parents: 41828
diff changeset
233 ifp = io.open(inp, 'rb')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
234 # XXXX Do textfile translation on non-mac systems
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
235 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
236 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
237 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
238 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
239 ofp.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
240 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
241
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
242 ifp = openrsrc(inp, 'rb')
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
243 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
244 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
245 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
246 ofp.write_rsrc(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
247 ofp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
248 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
249
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
250 class _Hqxdecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
251 """Read data via the decoder in 4-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
252
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
253 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
254 self.ifp = ifp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
255 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
256
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
257 def read(self, totalwtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
258 """Read at least wtd bytes (or until EOF)"""
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
259 decdata = b''
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
260 wtd = totalwtd
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
261 #
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
262 # The loop here is convoluted, since we don't really now how
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
263 # much to decode: there may be newlines in the incoming data.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
264 while wtd > 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
265 if self.eof: return decdata
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
266 wtd = ((wtd + 2) // 3) * 4
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
267 data = self.ifp.read(wtd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
268 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
269 # Next problem: there may not be a complete number of
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
270 # bytes in what we pass to a2b. Solve by yet another
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
271 # loop.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
272 #
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
273 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
274 try:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
275 decdatacur, self.eof = binascii.a2b_hqx(data)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
276 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
277 except binascii.Incomplete:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
278 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
279 newdata = self.ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
280 if not newdata:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
281 raise Error('Premature EOF on binhex file')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
282 data = data + newdata
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
283 decdata = decdata + decdatacur
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
284 wtd = totalwtd - len(decdata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
285 if not decdata and not self.eof:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
286 raise Error('Premature EOF on binhex file')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
287 return decdata
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
288
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
289 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
290 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
291
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
292 class _Rledecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
293 """Read data via the RLE-coder"""
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
294
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
295 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
296 self.ifp = ifp
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
297 self.pre_buffer = b''
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
298 self.post_buffer = b''
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
299 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
300
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
301 def read(self, wtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
302 if wtd > len(self.post_buffer):
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
303 self._fill(wtd - len(self.post_buffer))
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
304 rv = self.post_buffer[:wtd]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
305 self.post_buffer = self.post_buffer[wtd:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
306 return rv
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
307
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
308 def _fill(self, wtd):
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
309 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
310 if self.ifp.eof:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
311 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
312 binascii.rledecode_hqx(self.pre_buffer)
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
313 self.pre_buffer = b''
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
314 return
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
315
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
316 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
317 # Obfuscated code ahead. We have to take care that we don't
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
318 # end up with an orphaned RUNCHAR later on. So, we keep a couple
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
319 # of bytes in the buffer, depending on what the end of
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
320 # the buffer looks like:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
321 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
322 # '?\220' - Keep 2 bytes: repeated something-else
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
323 # '\220\0' - Escaped \220: Keep 2 bytes.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
324 # '?\220?' - Complete repeat sequence: decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
325 # otherwise: keep 1 byte.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
326 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
327 mark = len(self.pre_buffer)
42992
ecda33051739 Convert various string literals to bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42934
diff changeset
328 if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
329 mark = mark - 3
55032
0c6fd556149d Merged revisions 73747 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46970
diff changeset
330 elif self.pre_buffer[-1:] == RUNCHAR:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
331 mark = mark - 2
42992
ecda33051739 Convert various string literals to bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42934
diff changeset
332 elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
333 mark = mark - 2
55032
0c6fd556149d Merged revisions 73747 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46970
diff changeset
334 elif self.pre_buffer[-2:-1] == RUNCHAR:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
335 pass # Decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
336 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
337 mark = mark - 1
5090
b67c02bd5e23 Merged in Jack's fixes. This brings some lines beyond 79 chars again;
Guido van Rossum <guido@python.org>
parents: 4972
diff changeset
338
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
339 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
340 binascii.rledecode_hqx(self.pre_buffer[:mark])
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
341 self.pre_buffer = self.pre_buffer[mark:]
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
342
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
343 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
344 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
345
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
346 class HexBin:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
347 def __init__(self, ifp):
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42992
diff changeset
348 if isinstance(ifp, str):
41864
99e33068c334 This is the last time I fix binhex. If it breaks again it goes in the dustbin.
Guido van Rossum <guido@python.org>
parents: 41828
diff changeset
349 ifp = io.open(ifp, 'rb')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
350 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
351 # Find initial colon.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
352 #
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
353 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
354 ch = ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
355 if not ch:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
356 raise Error("No binhex data found")
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
357 # Cater for \r\n terminated lines (which show up as \n\r, hence
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
358 # all lines start with \r)
41864
99e33068c334 This is the last time I fix binhex. If it breaks again it goes in the dustbin.
Guido van Rossum <guido@python.org>
parents: 41828
diff changeset
359 if ch == b'\r':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
360 continue
41864
99e33068c334 This is the last time I fix binhex. If it breaks again it goes in the dustbin.
Guido van Rossum <guido@python.org>
parents: 41828
diff changeset
361 if ch == b':':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
362 break
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
363
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
364 hqxifp = _Hqxdecoderengine(ifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
365 self.ifp = _Rledecoderengine(hqxifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
366 self.crc = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
367 self._readheader()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
368
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
369 def _read(self, len):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
370 data = self.ifp.read(len)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
371 self.crc = binascii.crc_hqx(data, self.crc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
372 return data
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
373
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
374 def _checkcrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
375 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
376 #self.crc = binascii.crc_hqx('\0\0', self.crc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
377 # XXXX Is this needed??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
378 self.crc = self.crc & 0xffff
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
379 if filecrc != self.crc:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
380 raise Error('CRC error, computed %x, read %x'
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
381 % (self.crc, filecrc))
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
382 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
383
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
384 def _readheader(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
385 len = self._read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
386 fname = self._read(ord(len))
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
387 rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
388 self._checkcrc()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
389
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
390 type = rest[1:5]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
391 creator = rest[5:9]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
392 flags = struct.unpack('>h', rest[9:11])[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
393 self.dlen = struct.unpack('>l', rest[11:15])[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
394 self.rlen = struct.unpack('>l', rest[15:19])[0]
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
395
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
396 self.FName = fname
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
397 self.FInfo = FInfo()
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41894
diff changeset
398 self.FInfo.Creator = creator
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41894
diff changeset
399 self.FInfo.Type = type
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
400 self.FInfo.Flags = flags
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
401
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
402 self.state = _DID_HEADER
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
403
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
404 def read(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
405 if self.state != _DID_HEADER:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
406 raise Error('Read data at wrong time')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
407 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
408 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
409 n = min(n, self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
410 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
411 n = self.dlen
41828
a7be148f7c56 Make test_binhex pass. (Do we really want to support it still?)
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
412 rv = b''
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
413 while len(rv) < n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
414 rv = rv + self._read(n-len(rv))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
415 self.dlen = self.dlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
416 return rv
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
417
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
418 def close_data(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
419 if self.state != _DID_HEADER:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
420 raise Error('close_data at wrong time')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
421 if self.dlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
422 dummy = self._read(self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
423 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
424 self.state = _DID_DATA
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
425
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
426 def read_rsrc(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
427 if self.state == _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
428 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
429 if self.state != _DID_DATA:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
430 raise Error('Read resource data at wrong time')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
431 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
432 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
433 n = min(n, self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
434 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
435 n = self.rlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
436 self.rlen = self.rlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
437 return self._read(n)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
438
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
439 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
440 if self.rlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
441 dummy = self.read_rsrc(self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
442 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
443 self.state = _DID_RSRC
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
444 self.ifp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
445
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
446 def hexbin(inp, out):
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
447 """hexbin(infilename, outfilename) - Decode binhexed file"""
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
448 ifp = HexBin(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
449 finfo = ifp.FInfo
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
450 if not out:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
451 out = ifp.FName
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
452
41864
99e33068c334 This is the last time I fix binhex. If it breaks again it goes in the dustbin.
Guido van Rossum <guido@python.org>
parents: 41828
diff changeset
453 ofp = io.open(out, 'wb')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
454 # XXXX Do translation on non-mac systems
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
455 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
456 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
457 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
458 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
459 ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
460 ifp.close_data()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
461
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
462 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
463 if d:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
464 ofp = openrsrc(out, 'wb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
465 ofp.write(d)
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
466 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
467 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
468 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
469 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
470 ofp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
471
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
472 ifp.close()