Fix Python 3 issues when generating preview snippets

The log file generated by latex can contain strings encoded in
whatever supported encoding. Instead of guessing the encoding,
it is better to open it in binary mode and then performing the
necessary comparisons as "bytes". In order to do this, the
strings are encoded in utf8, so that, for example, b"pythön" is
encoded as "pyth\xc3\xb6n" (7 bytes). Of course, this means that
we can only successfully perform comparisons with ascii strings.
However, this is what we actually do, as we only search for
ascii strings in the log file.

(cherry picked from commit bd6d09fc98)
This commit is contained in:
Enrico Forestieri 2020-03-14 17:03:16 +01:00
parent 36bb71c4c5
commit 51b8778c73
2 changed files with 14 additions and 14 deletions

View File

@ -97,8 +97,8 @@ def usage(prog_name):
# Use write_metrics_info to create the .metrics file with this info
def legacy_extract_metrics_info(log_file):
log_re = re.compile("Preview: ([ST])")
data_re = re.compile("(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
log_re = re.compile(b"Preview: ([ST])")
data_re = re.compile(b"(-?[0-9]+) (-?[0-9]+) (-?[0-9]+) (-?[0-9]+)")
tp_ascent = 0.0
tp_descent = 0.0
@ -106,7 +106,7 @@ def legacy_extract_metrics_info(log_file):
success = 0
results = []
try:
for line in open(log_file, 'r').readlines():
for line in open(log_file, 'rb').readlines():
match = log_re.match(line)
if match == None:
continue
@ -154,10 +154,10 @@ def legacy_extract_metrics_info(log_file):
return results
def extract_resolution(log_file, dpi):
fontsize_re = re.compile("Preview: Fontsize")
magnification_re = re.compile("Preview: Magnification")
extract_decimal_re = re.compile("([0-9\.]+)")
extract_integer_re = re.compile("([0-9]+)")
fontsize_re = re.compile(b"Preview: Fontsize")
magnification_re = re.compile(b"Preview: Magnification")
extract_decimal_re = re.compile(b"([0-9\.]+)")
extract_integer_re = re.compile(b"([0-9]+)")
found_fontsize = 0
found_magnification = 0
@ -167,7 +167,7 @@ def extract_resolution(log_file, dpi):
fontsize = 10.0
try:
for line in open(log_file, 'r').readlines():
for line in open(log_file, 'rb').readlines():
if found_fontsize and found_magnification:
break

View File

@ -313,9 +313,9 @@ def run_tex(tex, tex_file):
def string_in_file(string, infile):
if not os.path.isfile(infile):
return False
f = open(infile, 'r')
f = open(infile, 'rb')
for line in f.readlines():
if string in line:
if string.encode() in line:
f.close()
return True
f.close()
@ -325,15 +325,15 @@ def string_in_file(string, infile):
# Returns a list of indexes of pages giving errors extracted from the latex log
def check_latex_log(log_file):
error_re = re.compile("^! ")
snippet_re = re.compile("^Preview: Snippet ")
data_re = re.compile("([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)")
error_re = re.compile(b"^! ")
snippet_re = re.compile(b"^Preview: Snippet ")
data_re = re.compile(b"([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)")
found_error = False
error_pages = []
try:
for line in open(log_file, 'r').readlines():
for line in open(log_file, 'rb').readlines():
if not found_error:
match = error_re.match(line)
if match != None: