commit 58734edff688ae81461fffbc1f3de146d4883833
Author: José Matos <[email protected]>
Date:   Wed Jun 5 14:06:09 2019 +0100

    Make the lyx2lyx code compatible between python2 and python3.
    
    FWIW this code is important for very old versions of lyx, older than 1.1.5 
(released 19 years ago - 2000/06/06).
    
    Funny fact of the day, byte strings do not behave as regular strings in 
python3 when taking and index.
    To get a sub-string we need to pass a range, a integer index will not work 
as it happens in a regular string:
    
    $ ipython3
    ...
    In [30]: line
    Out[30]: b'#This file was created by <mike> Tue Jan 25 10:36:51 2000'
    
    In [31]: line[0]
    Out[31]: 35
    
    In [32]: line[0:1]
    Out[32]: b'#'
    
    The range notations works for both byte and regular strings in python 3, 
and it also works in python 2.
    Thus the change is simple and effective. In any case I should confess that 
I was quite surprised by this. :-)
---
 lib/lyx2lyx/LyX.py |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py
index 245cc6e..ffaf4f0 100644
--- a/lib/lyx2lyx/LyX.py
+++ b/lib/lyx2lyx/LyX.py
@@ -71,8 +71,8 @@ def minor_versions(major, last_minor_version):
 # Regular expressions used
 format_re = re.compile(r"(\d)[\.,]?(\d\d)")
 fileformat = re.compile(r"\\lyxformat\s*(\S*)")
-original_version = re.compile(r".*?LyX ([\d.]*)")
-original_tex2lyx_version = re.compile(r".*?tex2lyx ([\d.]*)")
+original_version = re.compile(b".*?LyX ([\\d.]*)")
+original_tex2lyx_version = re.compile(b".*?tex2lyx ([\\d.]*)")
 
 ##
 # file format information:
@@ -519,10 +519,10 @@ class LyX_base:
         file, returns the most likely value, or None otherwise."""
 
         for line in self.header:
-            if line[0] != "#":
+            if line[0:1] != b"#":
                 return None
 
-            line = line.replace("fix",".")
+            line = line.replace(b"fix",b".")
             # need to test original_tex2lyx_version first because tex2lyx
             # writes "#LyX file created by tex2lyx 2.2"
             result = original_tex2lyx_version.match(line)
@@ -530,14 +530,14 @@ class LyX_base:
                 result = original_version.match(line)
                 if result:
                     # Special know cases: reLyX and KLyX
-                    if line.find("reLyX") != -1 or line.find("KLyX") != -1:
+                    if line.find(b"reLyX") != -1 or line.find(b"KLyX") != -1:
                         return "0.12"
             if result:
                 res = result.group(1)
                 if not res:
                     self.warning(line)
                 #self.warning("Version %s" % result.group(1))
-                return res
+                return res.decode('ascii') if not PY2 else res
         self.warning(str(self.header[:2]))
         return None
 
@@ -642,7 +642,7 @@ class LyX_base:
           self.warning("Malformed LyX document: No \\textclass!!")
           return
         i = j = tclass + 1
-      else: 
+      else:
         j = find_token(self.header, "\\end_modules", i)
         if j == -1:
             self.warning("(set_module_list) Malformed LyX document: No 
\\end_modules.")

Reply via email to