diff --git a/Lib/test/test_uu.py b/Lib/test/test_uu.py --- a/Lib/test/test_uu.py +++ b/Lib/test/test_uu.py @@ -191,16 +191,65 @@ class UUFileTest(unittest.TestCase): f = open(self.tmpout, 'rb') s = f.read() f.close() self.assertEqual(s, plaintext) # XXX is there an xp way to verify the mode? finally: self._kill(f) + def test_encode_defaults(self): + try: + with open(self.tmpin, 'wb') as f: + f.write(plaintext) + + uu.encode(self.tmpin, self.tmpout) + + with open(self.tmpout, 'rb') as f: + data = f.read() + self.assertEqual(data[26:-7], encodedtext) + finally: + self._kill(f) + + @unittest.skipIf( + not hasattr(os, 'stat'), + "Skip on systems that don't support os.stat" + ) + def test_encode_osstat_assert(self): + try: + with open(self.tmpin, 'wb') as f: + f.write(plaintext) + + _stat = os.stat + try: + del os.stat + uu.encode(self.tmpin, self.tmpout) + finally: + os.stat = _stat + + with open(self.tmpout, 'rb') as f: + data = f.read() + self.assertEqual(int(str(data).split(' ')[1]), 666) + finally: + self._kill(f) + + def test_encode_stdin_name(self): + try: + with open(self.tmpin, 'wb') as f: + f.write(plaintext) + + with open(self.tmpin, 'rb') as f: + uu.encode(f, self.tmpout) + + with open(self.tmpout, 'rb') as f: + data = f.read() + self.assertEqual(str(data).split(' ')[2][0], '-') + finally: + self._kill(f) + def test_decode_filename(self): f = None try: support.unlink(self.tmpin) f = open(self.tmpin, 'wb') f.write(encodedtextwrapped(0o644, self.tmpout)) f.close() diff --git a/Lib/uu.py b/Lib/uu.py --- a/Lib/uu.py +++ b/Lib/uu.py @@ -46,21 +46,24 @@ def encode(in_file, out_file, name=None, # 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) + # Set defaults for mode if mode is None: try: - mode = os.stat(in_file).st_mode + mode_stat = os.stat(in_file) except AttributeError: pass + else: + mode = mode_stat.st_mode 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):