Commit e6120abe by Richard Sandiford Committed by Richard Sandiford

dg-extract-results.py: For Python 3, force sys.stdout to handle surrogate escape sequences.

contrib/
	* dg-extract-results.py: For Python 3, force sys.stdout to handle
	surrogate escape sequences.
	(safe_open): New function.
	(output_segment, main): Use it.

From-SVN: r211666
parent 56444a32
2014-06-14 Richard Sandiford <rdsandiford@googlemail.com>
* dg-extract-results.py: For Python 3, force sys.stdout to handle
surrogate escape sequences.
(safe_open): New function.
(output_segment, main): Use it.
2014-05-25 Richard Sandiford <rdsandiford@googlemail.com> 2014-05-25 Richard Sandiford <rdsandiford@googlemail.com>
* dg-extract-results.py (Named): Remove __cmp__ method. * dg-extract-results.py (Named): Remove __cmp__ method.
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
import sys import sys
import getopt import getopt
import re import re
import io
from datetime import datetime from datetime import datetime
from operator import attrgetter from operator import attrgetter
...@@ -21,6 +22,18 @@ strict = False ...@@ -21,6 +22,18 @@ strict = False
# they should keep the original order. # they should keep the original order.
sort_logs = True sort_logs = True
# A version of open() that is safe against whatever binary output
# might be added to the log.
def safe_open (filename):
if sys.version_info >= (3, 0):
return open (filename, 'r', errors = 'surrogateescape')
return open (filename, 'r')
# Force stdout to handle escape sequences from a safe_open file.
if sys.version_info >= (3, 0):
sys.stdout = io.TextIOWrapper (sys.stdout.buffer,
errors = 'surrogateescape')
class Named: class Named:
def __init__ (self, name): def __init__ (self, name):
self.name = name self.name = name
...@@ -457,7 +470,7 @@ class Prog: ...@@ -457,7 +470,7 @@ class Prog:
# Output a segment of text. # Output a segment of text.
def output_segment (self, segment): def output_segment (self, segment):
with open (segment.filename, 'r') as file: with safe_open (segment.filename) as file:
file.seek (segment.start) file.seek (segment.start)
for i in range (segment.lines): for i in range (segment.lines):
sys.stdout.write (file.readline()) sys.stdout.write (file.readline())
...@@ -540,7 +553,7 @@ class Prog: ...@@ -540,7 +553,7 @@ class Prog:
try: try:
# Parse the input files. # Parse the input files.
for filename in self.files: for filename in self.files:
with open (filename, 'r') as file: with safe_open (filename) as file:
self.parse_file (filename, file) self.parse_file (filename, file)
# Decide what to output. # Decide what to output.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment