@@ -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
156160def test ():
157161 """uuencode/uudecode main program"""
0 commit comments