Project

General

Profile

« Previous | Next » 

Revision 1448

cat_csv: Abort if output stream closed instead of exiting with an IOError

View differences:

cat_csv
20 20
        
21 21
        # Get dialect and process first line
22 22
        info = csvs.stream_info(stream)
23
        def write_header(): sys.stdout.write(info.header_line)
24
        if first_info == None:
25
            first_path = path
26
            first_info = info
27
            write_header()
28
        else:
29
            if not util.classes_eq(info.dialect, first_info.dialect):
30
                raise SystemExit('Spreadsheet error: "'+path
31
                    +'" dialect doesn\'t match "'+first_path+'" dialect')
32
            if info.header_line != first_info.header_line: write_header()
33
                # not a duplicated header
23
        try:
24
            def write_header(): sys.stdout.write(info.header_line)
25
            if first_info == None:
26
                first_path = path
27
                first_info = info
28
                write_header()
29
            else:
30
                if not util.classes_eq(info.dialect, first_info.dialect):
31
                    raise SystemExit('Spreadsheet error: "'+path
32
                        +'" dialect doesn\'t match "'+first_path+'" dialect')
33
                if info.header_line != first_info.header_line: write_header()
34
                    # not a duplicated header
35
            
36
            # Copy remaining lines
37
            while True:
38
                line = stream.readline()
39
                if line == '': break
40
                sys.stdout.write(line)
41
        except IOError, e: # abort if output stream closed
42
            if str(e) != '[Errno 32] Broken pipe': raise # other IOErrors
34 43
        
35
        # Copy remaining lines
36
        while True:
37
            line = stream.readline()
38
            if line == '': break
39
            sys.stdout.write(line)
40
        
41 44
        stream.close()
42 45

  
43 46
main()

Also available in: Unified diff