commit 51b8778c73f111772d178192bf41bb0bf0550d31
Author: Enrico Forestieri <[email protected]>
Date:   Sat Mar 14 17:03:16 2020 +0100

    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 bd6d09fc98b4bae208499008446d4bb7738111e2)
---
 lib/scripts/legacy_lyxpreview2ppm.py |   16 ++++++++--------
 lib/scripts/lyxpreview_tools.py      |   12 ++++++------
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/lib/scripts/legacy_lyxpreview2ppm.py 
b/lib/scripts/legacy_lyxpreview2ppm.py
index a5eb05a..9b99796 100644
--- a/lib/scripts/legacy_lyxpreview2ppm.py
+++ b/lib/scripts/legacy_lyxpreview2ppm.py
@@ -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
 
diff --git a/lib/scripts/lyxpreview_tools.py b/lib/scripts/lyxpreview_tools.py
index 91cc4d6..65806a4 100644
--- a/lib/scripts/lyxpreview_tools.py
+++ b/lib/scripts/lyxpreview_tools.py
@@ -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:
-- 
lyx-cvs mailing list
[email protected]
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to