annotate Lib/binhex.py @ 84007:dbfbdf2b5c19 3.3

Fix #17967: For ftp urls CWD to target instead of hopping to each directory towards target. This fixes a bug where target is accessible, but parent directories are restricted.
author Senthil Kumaran <senthil@uthcode.com>
date Sat, 01 Jun 2013 08:27:06 -0700
parents 8c17e898e0e8
children
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 struct
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27 import binascii
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
28
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15860
diff changeset
29 __all__ = ["binhex","hexbin","Error"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15860
diff changeset
30
14022
d44603f2b928 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
31 class Error(Exception):
d44603f2b928 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
32 pass
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
33
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
34 # States (what have we written)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
35 [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37 # Various constants
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
38 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
39 LINELEN = 64
42992
ecda33051739 Convert various string literals to bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42934
diff changeset
40 RUNCHAR = b"\x90"
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
41
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
42 #
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
43 # This code is no longer byte-order dependent
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
44
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
45
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
46 class FInfo:
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
47 def __init__(self):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
48 self.Type = '????'
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
49 self.Creator = '????'
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
50 self.Flags = 0
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
51
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
52 def getfileinfo(name):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
53 finfo = FInfo()
68633
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
54 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
55 # Quick check for textfile
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
56 data = fp.read(512)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
57 if 0 not in data:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
58 finfo.Type = 'TEXT'
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
59 fp.seek(0, 2)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
60 dsize = fp.tell()
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
61 dir, file = os.path.split(name)
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
62 file = file.replace(':', '-', 1)
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
63 return file, finfo, dsize, 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
64
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
65 class openrsrc:
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
66 def __init__(self, *args):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
67 pass
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
68
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
69 def read(self, *args):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
70 return b''
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
71
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
72 def write(self, *args):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
73 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
74
46970
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
75 def close(self):
eead08b8e1fd Remove the Mac modules
Benjamin Peterson <benjamin@python.org>
parents: 43577
diff changeset
76 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
77
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
78 class _Hqxcoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
79 """Write data to the coder in 3-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
80
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
81 def __init__(self, ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
82 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
83 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
84 self.hqxdata = b''
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
85 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
86
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
87 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
88 self.data = self.data + data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
89 datalen = len(self.data)
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
90 todo = (datalen // 3) * 3
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
91 data = self.data[:todo]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
92 self.data = self.data[todo:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
93 if not data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
94 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
95 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
96 self._flush(0)
3479
aec8bbcf8b0e new binhex from Jack (much faster)
Guido van Rossum <guido@python.org>
parents: 3474
diff changeset
97
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
98 def _flush(self, force):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
99 first = 0
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
100 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
101 last = first + self.linelen
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
102 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
103 self.linelen = LINELEN
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
104 first = last
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
105 self.hqxdata = self.hqxdata[first:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
106 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
107 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
108
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
109 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
110 if self.data:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
111 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
112 self._flush(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
113 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
114 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
115
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
116 class _Rlecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
117 """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
118
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
119 def __init__(self, ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
120 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
121 self.data = b''
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
122
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
123 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
124 self.data = self.data + data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
125 if len(self.data) < REASONABLY_LARGE:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
126 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
127 rledata = binascii.rlecode_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
128 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
129 self.data = b''
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
130
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
131 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
132 if self.data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
133 rledata = binascii.rlecode_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
134 self.ofp.write(rledata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
135 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
136 del self.ofp
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
137
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
138 class BinHex:
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
139 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
140 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
141 close_on_error = False
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42992
diff changeset
142 if isinstance(ofp, str):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
143 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
144 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
145 close_on_error = True
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
146 try:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
147 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
148 hqxer = _Hqxcoderengine(ofp)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
149 self.ofp = _Rlecoderengine(hqxer)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
150 self.crc = 0
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
151 if finfo is None:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
152 finfo = FInfo()
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
153 self.dlen = dlen
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
154 self.rlen = rlen
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
155 self._writeinfo(name, finfo)
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
156 self.state = _DID_HEADER
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
157 except:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
158 if close_on_error:
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
159 ofp.close()
5c2d15c6007e Close #11577: Improve binhex test coverage and fix ResourceWarning
Nick Coghlan <ncoghlan@gmail.com>
parents: 55527
diff changeset
160 raise
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
161
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
162 def _writeinfo(self, name, finfo):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
163 nl = len(name)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
164 if nl > 63:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
165 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
166 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
167 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
168 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
169 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
170 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
171 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
172 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
173
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
174 # 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
175 d3 = struct.pack('>h', finfo.Flags)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
176 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
177 info = d + d2 + d3 + d4
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
178 self._write(info)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
179 self._writecrc()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
180
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
181 def _write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
182 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
183 self.ofp.write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
184
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
185 def _writecrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
186 # XXXX Should this be here??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
187 # 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
188 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
189 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
190 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 32289
diff changeset
191 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
192 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
193 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
194
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
195 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
196 if self.state != _DID_HEADER:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
197 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
198 self.dlen = self.dlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
199 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
200
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
201 def close_data(self):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
202 if self.dlen != 0:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
203 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
204 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
205 self.state = _DID_DATA
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
206
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
207 def write_rsrc(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
208 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
209 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
210 if self.state != _DID_DATA:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
211 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
212 self.rlen = self.rlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
213 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
214
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
215 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
216 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
217 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
218 if self.state != _DID_DATA:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
219 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
220 if self.rlen != 0:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
221 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
222 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
223 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
224 self.state = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
225 del self.ofp
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
226
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
227 def binhex(inp, out):
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
228 """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
229 finfo = getfileinfo(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
230 ofp = BinHex(finfo, out)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
231
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
232 ifp = io.open(inp, 'rb')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
233 # 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
234 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
235 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
236 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
237 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
238 ofp.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
239 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
240
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
241 ifp = openrsrc(inp, 'rb')
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
242 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
243 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
244 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
245 ofp.write_rsrc(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
246 ofp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
247 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
248
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
249 class _Hqxdecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
250 """Read data via the decoder in 4-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
251
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
252 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
253 self.ifp = ifp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
254 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
255
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
256 def read(self, totalwtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
257 """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
258 decdata = b''
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
259 wtd = totalwtd
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
260 #
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
261 # 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
262 # 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
263 while wtd > 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
264 if self.eof: return decdata
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
265 wtd = ((wtd + 2) // 3) * 4
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
266 data = self.ifp.read(wtd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
267 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
268 # 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
269 # 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
270 # loop.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
271 #
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
272 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
273 try:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
274 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
275 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
276 except binascii.Incomplete:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
277 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
278 newdata = self.ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
279 if not newdata:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
280 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
281 data = data + newdata
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
282 decdata = decdata + decdatacur
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
283 wtd = totalwtd - len(decdata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
284 if not decdata and not self.eof:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
285 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
286 return decdata
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
287
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
288 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
289 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
290
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
291 class _Rledecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
292 """Read data via the RLE-coder"""
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
293
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
294 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
295 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
296 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
297 self.post_buffer = b''
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
298 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
299
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
300 def read(self, wtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
301 if wtd > len(self.post_buffer):
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
302 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
303 rv = self.post_buffer[:wtd]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
304 self.post_buffer = self.post_buffer[wtd:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
305 return rv
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
306
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
307 def _fill(self, wtd):
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
308 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
309 if self.ifp.eof:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
310 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
311 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
312 self.pre_buffer = b''
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
313 return
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
314
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
315 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
316 # 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
317 # 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
318 # 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
319 # the buffer looks like:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
320 # '\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
321 # '?\220' - Keep 2 bytes: repeated something-else
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
322 # '\220\0' - Escaped \220: Keep 2 bytes.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
323 # '?\220?' - Complete repeat sequence: decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
324 # otherwise: keep 1 byte.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
325 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
326 mark = len(self.pre_buffer)
42992
ecda33051739 Convert various string literals to bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42934
diff changeset
327 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
328 mark = mark - 3
55032
0c6fd556149d Merged revisions 73747 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46970
diff changeset
329 elif self.pre_buffer[-1:] == RUNCHAR:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
330 mark = mark - 2
42992
ecda33051739 Convert various string literals to bytes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42934
diff changeset
331 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
332 mark = mark - 2
55032
0c6fd556149d Merged revisions 73747 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46970
diff changeset
333 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
334 pass # Decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
335 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
336 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
337
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
338 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
339 binascii.rledecode_hqx(self.pre_buffer[:mark])
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
340 self.pre_buffer = self.pre_buffer[mark:]
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
341
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
342 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
343 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
344
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
345 class HexBin:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
346 def __init__(self, ifp):
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42992
diff changeset
347 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
348 ifp = io.open(ifp, 'rb')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
349 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
350 # Find initial colon.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
351 #
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
352 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
353 ch = ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
354 if not ch:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
355 raise Error("No binhex data found")
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
356 # 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
357 # 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
358 if ch == b'\r':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
359 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
360 if ch == b':':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
361 break
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
362
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
363 hqxifp = _Hqxdecoderengine(ifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
364 self.ifp = _Rledecoderengine(hqxifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
365 self.crc = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
366 self._readheader()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
367
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
368 def _read(self, len):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
369 data = self.ifp.read(len)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
370 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
371 return data
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
372
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
373 def _checkcrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
374 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
375 #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
376 # XXXX Is this needed??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
377 self.crc = self.crc & 0xffff
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
378 if filecrc != self.crc:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
379 raise Error('CRC error, computed %x, read %x'
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
380 % (self.crc, filecrc))
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
381 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
382
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
383 def _readheader(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
384 len = self._read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
385 fname = self._read(ord(len))
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
386 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
387 self._checkcrc()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
388
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
389 type = rest[1:5]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
390 creator = rest[5:9]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
391 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
392 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
393 self.rlen = struct.unpack('>l', rest[15:19])[0]
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
394
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
395 self.FName = fname
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
396 self.FInfo = FInfo()
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41894
diff changeset
397 self.FInfo.Creator = creator
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41894
diff changeset
398 self.FInfo.Type = type
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
399 self.FInfo.Flags = flags
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
400
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
401 self.state = _DID_HEADER
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
402
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
403 def read(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
404 if self.state != _DID_HEADER:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
405 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
406 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
407 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
408 n = min(n, self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
409 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
410 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
411 rv = b''
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
412 while len(rv) < n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
413 rv = rv + self._read(n-len(rv))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
414 self.dlen = self.dlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
415 return rv
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
416
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
417 def close_data(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
418 if self.state != _DID_HEADER:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
419 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
420 if self.dlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
421 dummy = self._read(self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
422 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
423 self.state = _DID_DATA
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
424
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
425 def read_rsrc(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
426 if self.state == _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
427 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
428 if self.state != _DID_DATA:
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
429 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
430 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
431 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
432 n = min(n, self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
433 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
434 n = self.rlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
435 self.rlen = self.rlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
436 return self._read(n)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
437
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
438 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
439 if self.rlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
440 dummy = self.read_rsrc(self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
441 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
442 self.state = _DID_RSRC
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
443 self.ifp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
444
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
445 def hexbin(inp, out):
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
446 """hexbin(infilename, outfilename) - Decode binhexed file"""
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
447 ifp = HexBin(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
448 finfo = ifp.FInfo
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
449 if not out:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
450 out = ifp.FName
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
451
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
452 ofp = io.open(out, 'wb')
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
453 # XXXX Do translation on non-mac systems
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
454 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
455 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
456 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
457 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
458 ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
459 ifp.close_data()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
460
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
461 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
462 if d:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
463 ofp = openrsrc(out, 'wb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
464 ofp.write(d)
42934
fdef32bbbcb8 Style/idiom cleanup of binhex module.
Collin Winter <collinw@gmail.com>
parents: 42933
diff changeset
465 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
466 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
467 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
468 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
469 ofp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
470
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
471 ifp.close()