decode(): Raise a uu.Error if no out_file is given but the file

specified in the uu header already exists.  No additional
    workaround is provided since out_file=pathname is a deprecated
    interface, so it is better to simply pass a file-like object into
    out_file anyway.  This closes SF bug #438083.

Use isinstance() tests instead of type comparisons.
This commit is contained in:
Barry Warsaw 2001-08-17 19:59:34 +00:00
parent de642bdc5d
commit 59dae8ad36
1 changed files with 13 additions and 9 deletions

View File

@ -33,6 +33,7 @@ decode(in_file [, out_file, mode])
import binascii import binascii
import os import os
import sys import sys
from types import StringType
__all__ = ["Error", "encode", "decode"] __all__ = ["Error", "encode", "decode"]
@ -46,7 +47,7 @@ def encode(in_file, out_file, name=None, mode=None):
# #
if in_file == '-': if in_file == '-':
in_file = sys.stdin in_file = sys.stdin
elif type(in_file) == type(''): elif isinstance(in_file, StringType):
if name is None: if name is None:
name = os.path.basename(in_file) name = os.path.basename(in_file)
if mode is None: if mode is None:
@ -60,7 +61,7 @@ def encode(in_file, out_file, name=None, mode=None):
# #
if out_file == '-': if out_file == '-':
out_file = sys.stdout out_file = sys.stdout
elif type(out_file) == type(''): elif isinstance(out_file, StringType):
out_file = open(out_file, 'w') out_file = open(out_file, 'w')
# #
# Set defaults for name and mode # Set defaults for name and mode
@ -80,14 +81,14 @@ def encode(in_file, out_file, name=None, mode=None):
out_file.write(' \nend\n') out_file.write(' \nend\n')
def decode(in_file, out_file=None, mode=None): def decode(in_file, out_file=None, mode=None, quiet=0):
"""Decode uuencoded file""" """Decode uuencoded file"""
# #
# Open the input file, if needed. # Open the input file, if needed.
# #
if in_file == '-': if in_file == '-':
in_file = sys.stdin in_file = sys.stdin
elif type(in_file) == type(''): elif isinstance(in_file, StringType):
in_file = open(in_file) in_file = open(in_file)
# #
# Read until a begin is encountered or we've exhausted the file # Read until a begin is encountered or we've exhausted the file
@ -107,6 +108,8 @@ def decode(in_file, out_file=None, mode=None):
pass pass
if out_file is None: if out_file is None:
out_file = hdrfields[2].rstrip() out_file = hdrfields[2].rstrip()
if os.path.exists(out_file):
raise Error, 'Cannot overwrite existing file: %s' % out_file
if mode is None: if mode is None:
mode = int(hdrfields[1], 8) mode = int(hdrfields[1], 8)
# #
@ -114,7 +117,7 @@ def decode(in_file, out_file=None, mode=None):
# #
if out_file == '-': if out_file == '-':
out_file = sys.stdout out_file = sys.stdout
elif type(out_file) == type(''): elif isinstance(out_file, StringType):
fp = open(out_file, 'wb') fp = open(out_file, 'wb')
try: try:
os.path.chmod(out_file, mode) os.path.chmod(out_file, mode)
@ -125,14 +128,15 @@ def decode(in_file, out_file=None, mode=None):
# Main decoding loop # Main decoding loop
# #
s = in_file.readline() s = in_file.readline()
while s and s != 'end\n': while s and s.strip() != 'end':
try: try:
data = binascii.a2b_uu(s) data = binascii.a2b_uu(s)
except binascii.Error, v: except binascii.Error, v:
# Workaround for broken uuencoders by /Fredrik Lundh # Workaround for broken uuencoders by /Fredrik Lundh
nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3 nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3
data = binascii.a2b_uu(s[:nbytes]) data = binascii.a2b_uu(s[:nbytes])
sys.stderr.write("Warning: %s\n" % str(v)) if not quiet:
sys.stderr.write("Warning: %s\n" % str(v))
out_file.write(data) out_file.write(data)
s = in_file.readline() s = in_file.readline()
if not s: if not s:
@ -168,7 +172,7 @@ def test():
if dopt: if dopt:
if topt: if topt:
if type(output) == type(''): if isinstance(output, StringType):
output = open(output, 'w') output = open(output, 'w')
else: else:
print sys.argv[0], ': cannot do -t to stdout' print sys.argv[0], ': cannot do -t to stdout'
@ -176,7 +180,7 @@ def test():
decode(input, output) decode(input, output)
else: else:
if topt: if topt:
if type(input) == type(''): if isinstance(input, StringType):
input = open(input, 'r') input = open(input, 'r')
else: else:
print sys.argv[0], ': cannot do -t from stdin' print sys.argv[0], ': cannot do -t from stdin'