diff -r aea58e1cae75 Lib/uu.py --- a/Lib/uu.py Sun Sep 08 14:14:38 2013 +0200 +++ b/Lib/uu.py Sun Sep 08 19:01:02 2013 +0300 @@ -171,29 +171,56 @@ sys.exit(1) # Use the binary streams underlying stdin/stdout - input = sys.stdin.buffer - output = sys.stdout.buffer + input = output = '-' if len(args) > 0: input = args[0] if len(args) > 1: output = args[1] - if options.decode: - if options.text: - if isinstance(output, str): - output = open(output, 'wb') - else: - print(sys.argv[0], ': cannot do -t to stdout') - sys.exit(1) - decode(input, output) - else: - if options.text: - if isinstance(input, str): - input = open(input, 'rb') - else: - print(sys.argv[0], ': cannot do -t from stdin') - sys.exit(1) - encode(input, output) + opened_file = None + try: + if options.decode: + if options.text: + if output == '-': + raw = sys.stdout.buffer + else: + opened_file = raw = open(output, 'wb') + linesep = os.linesep.encode() + class Writer: + def write(self, data): + raw.write(data.replace(b'\n', linesep)) + return len(data) + output = Writer() + decode(input, output) + else: + if options.text: + if input == '-': + raw = sys.stdin.buffer + else: + opened_file = raw = open(input, 'rb') + class Reader: + prev_r = False + def read1(self, size=-1): + data = raw.read(size) + if data: + if self.prev_r and data[:1] == b'\n': + data = data[1:] + self.prev_r = (data[-1:] == b'\r') + data = data.replace(b'\r\n', b'\n') + data = data.replace(b'\r', b'\n') + return data + def read(self, size=-1): + result = data = self.read1(size) + if size is not None: + while data and len(result) < size: + data = self.read1(size - len(result)) + result += data + return result + input = Reader() + encode(input, output) + finally: + if opened_file is not None: + opened_file.close() if __name__ == '__main__': test()