summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-10-31 13:07:00 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-10-31 13:07:00 (GMT)
commite305425b86a5896cd3afce0859f4762b1fe62917 (patch)
treebb10af8c20a74e4b4569039054ab38b8b4cf125e /Lib
parent735e3b195a492d7480572f3c29fd4ef7d8723266 (diff)
downloadcpython-e305425b86a5896cd3afce0859f4762b1fe62917.zip
cpython-e305425b86a5896cd3afce0859f4762b1fe62917.tar.gz
cpython-e305425b86a5896cd3afce0859f4762b1fe62917.tar.bz2
Merged revisions 85975 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r85975 | antoine.pitrou | 2010-10-30 15:03:56 +0200 (sam., 30 oct. 2010) | 4 lines Issue #10246: uu.encode didn't close file objects explicitly when filenames were given to it. Patch by Brian Brazil. ........
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/uu.py69
1 files changed, 38 insertions, 31 deletions
diff --git a/Lib/uu.py b/Lib/uu.py
index d70f0e6..829f292 100755
--- a/Lib/uu.py
+++ b/Lib/uu.py
@@ -44,40 +44,47 @@ def encode(in_file, out_file, name=None, mode=None):
#
# If in_file is a pathname open it and change defaults
#
- if in_file == '-':
- in_file = sys.stdin.buffer
- elif isinstance(in_file, str):
+ opened_files = []
+ try:
+ if in_file == '-':
+ in_file = sys.stdin.buffer
+ elif isinstance(in_file, str):
+ if name is None:
+ name = os.path.basename(in_file)
+ if mode is None:
+ try:
+ mode = os.stat(in_file).st_mode
+ except AttributeError:
+ pass
+ in_file = open(in_file, 'rb')
+ opened_files.append(in_file)
+ #
+ # Open out_file if it is a pathname
+ #
+ if out_file == '-':
+ out_file = sys.stdout.buffer
+ elif isinstance(out_file, str):
+ out_file = open(out_file, 'wb')
+ opened_files.append(out_file)
+ #
+ # Set defaults for name and mode
+ #
if name is None:
- name = os.path.basename(in_file)
+ name = '-'
if mode is None:
- try:
- mode = os.stat(in_file).st_mode
- except AttributeError:
- pass
- in_file = open(in_file, 'rb')
- #
- # Open out_file if it is a pathname
- #
- if out_file == '-':
- out_file = sys.stdout.buffer
- elif isinstance(out_file, str):
- out_file = open(out_file, 'wb')
- #
- # Set defaults for name and mode
- #
- if name is None:
- name = '-'
- if mode is None:
- mode = 0o666
- #
- # Write the data
- #
- out_file.write(('begin %o %s\n' % ((mode & 0o777), name)).encode("ascii"))
- data = in_file.read(45)
- while len(data) > 0:
- out_file.write(binascii.b2a_uu(data))
+ mode = 0o666
+ #
+ # Write the data
+ #
+ out_file.write(('begin %o %s\n' % ((mode & 0o777), name)).encode("ascii"))
data = in_file.read(45)
- out_file.write(b' \nend\n')
+ while len(data) > 0:
+ out_file.write(binascii.b2a_uu(data))
+ data = in_file.read(45)
+ out_file.write(b' \nend\n')
+ finally:
+ for f in opened_files:
+ f.close()
def decode(in_file, out_file=None, mode=None, quiet=False):