Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b805ee5

Browse files
committed
Merged revisions 86037 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r86037 | antoine.pitrou | 2010-10-31 17:04:14 +0100 (dim., 31 oct. 2010) | 4 lines Issue #10266: uu.decode didn't close in_file explicitly when it was given as a filename. Patch by Brian Brazil. ........
1 parent 27683c9 commit b805ee5

3 files changed

Lines changed: 77 additions & 53 deletions

File tree

Lib/test/test_uu.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@ def test_decode(self):
196196
finally:
197197
self._kill(f)
198198

199+
def test_decode_filename(self):
200+
f = None
201+
try:
202+
support.unlink(self.tmpin)
203+
f = open(self.tmpin, 'wb')
204+
f.write(encodedtextwrapped(0o644, self.tmpout))
205+
f.close()
206+
207+
uu.decode(self.tmpin)
208+
209+
f = open(self.tmpout, 'rb')
210+
s = f.read()
211+
f.close()
212+
self.assertEqual(s, plaintext)
213+
finally:
214+
self._kill(f)
215+
199216
def test_decodetwice(self):
200217
# Verify that decode() will refuse to overwrite an existing file
201218
f = None

Lib/uu.py

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -92,66 +92,70 @@ def decode(in_file, out_file=None, mode=None, quiet=False):
9292
#
9393
# Open the input file, if needed.
9494
#
95+
opened_files = []
9596
if in_file == '-':
9697
in_file = sys.stdin.buffer
9798
elif isinstance(in_file, str):
9899
in_file = open(in_file, 'rb')
99-
#
100-
# Read until a begin is encountered or we've exhausted the file
101-
#
102-
while True:
103-
hdr = in_file.readline()
104-
if not hdr:
105-
raise Error('No valid begin line found in input file')
106-
if not hdr.startswith(b'begin'):
107-
continue
108-
hdrfields = hdr.split(b' ', 2)
109-
if len(hdrfields) == 3 and hdrfields[0] == b'begin':
100+
opened_files.append(in_file)
101+
102+
try:
103+
#
104+
# Read until a begin is encountered or we've exhausted the file
105+
#
106+
while True:
107+
hdr = in_file.readline()
108+
if not hdr:
109+
raise Error('No valid begin line found in input file')
110+
if not hdr.startswith(b'begin'):
111+
continue
112+
hdrfields = hdr.split(b' ', 2)
113+
if len(hdrfields) == 3 and hdrfields[0] == b'begin':
114+
try:
115+
int(hdrfields[1], 8)
116+
break
117+
except ValueError:
118+
pass
119+
if out_file is None:
120+
# If the filename isn't ASCII, what's up with that?!?
121+
out_file = hdrfields[2].rstrip(b' \t\r\n\f').decode("ascii")
122+
if os.path.exists(out_file):
123+
raise Error('Cannot overwrite existing file: %s' % out_file)
124+
if mode is None:
125+
mode = int(hdrfields[1], 8)
126+
#
127+
# Open the output file
128+
#
129+
if out_file == '-':
130+
out_file = sys.stdout.buffer
131+
elif isinstance(out_file, str):
132+
fp = open(out_file, 'wb')
110133
try:
111-
int(hdrfields[1], 8)
112-
break
113-
except ValueError:
134+
os.path.chmod(out_file, mode)
135+
except AttributeError:
114136
pass
115-
if out_file is None:
116-
# If the filename isn't ASCII, what's up with that?!?
117-
out_file = hdrfields[2].rstrip(b' \t\r\n\f').decode("ascii")
118-
if os.path.exists(out_file):
119-
raise Error('Cannot overwrite existing file: %s' % out_file)
120-
if mode is None:
121-
mode = int(hdrfields[1], 8)
122-
#
123-
# Open the output file
124-
#
125-
opened = False
126-
if out_file == '-':
127-
out_file = sys.stdout.buffer
128-
elif isinstance(out_file, str):
129-
fp = open(out_file, 'wb')
130-
try:
131-
os.path.chmod(out_file, mode)
132-
except AttributeError:
133-
pass
134-
out_file = fp
135-
opened = True
136-
#
137-
# Main decoding loop
138-
#
139-
s = in_file.readline()
140-
while s and s.strip(b' \t\r\n\f') != b'end':
141-
try:
142-
data = binascii.a2b_uu(s)
143-
except binascii.Error as v:
144-
# Workaround for broken uuencoders by /Fredrik Lundh
145-
nbytes = (((s[0]-32) & 63) * 4 + 5) // 3
146-
data = binascii.a2b_uu(s[:nbytes])
147-
if not quiet:
148-
sys.stderr.write("Warning: %s\n" % v)
149-
out_file.write(data)
137+
out_file = fp
138+
opened_files.append(out_file)
139+
#
140+
# Main decoding loop
141+
#
150142
s = in_file.readline()
151-
if not s:
152-
raise Error('Truncated input file')
153-
if opened:
154-
out_file.close()
143+
while s and s.strip(b' \t\r\n\f') != b'end':
144+
try:
145+
data = binascii.a2b_uu(s)
146+
except binascii.Error as v:
147+
# Workaround for broken uuencoders by /Fredrik Lundh
148+
nbytes = (((s[0]-32) & 63) * 4 + 5) // 3
149+
data = binascii.a2b_uu(s[:nbytes])
150+
if not quiet:
151+
sys.stderr.write("Warning: %s\n" % v)
152+
out_file.write(data)
153+
s = in_file.readline()
154+
if not s:
155+
raise Error('Truncated input file')
156+
finally:
157+
for f in opened_files:
158+
f.close()
155159

156160
def test():
157161
"""uuencode/uudecode main program"""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ C-API
143143
Library
144144
-------
145145

146+
- Issue #10266: uu.decode didn't close in_file explicitly when it was given
147+
as a filename. Patch by Brian Brazil.
148+
146149
- Issue #10246: uu.encode didn't close file objects explicitly when filenames
147150
were given to it. Patch by Brian Brazil.
148151

0 commit comments

Comments
 (0)