]> git.lyx.org Git - lyx.git/commitdiff
Fix Python 3 issues when generating preview snippets
authorEnrico Forestieri <forenr@lyx.org>
Sat, 14 Mar 2020 16:03:16 +0000 (17:03 +0100)
committerEnrico Forestieri <forenr@lyx.org>
Sat, 14 Mar 2020 16:19:08 +0000 (17:19 +0100)
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.

lib/scripts/legacy_lyxpreview2ppm.py
lib/scripts/lyxpreview_tools.py

index a5eb05a12963ee5bacd1afa2ca15b8302b788240..9b99796e138c1b2e0d351cbf7af01da363282a73 100644 (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
 
index 93964083a5d8f604ea0b2f7586b409fe199d71aa..7783fe2b7ddde61529d3d5307642ebb828887870 100644 (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: