From: José Matos Date: Wed, 5 Jun 2019 13:06:09 +0000 (+0100) Subject: Make the lyx2lyx code compatible between python2 and python3. X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=728eabb69c3ac3923d67277f052555ff84b5728b;p=features.git 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 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. :-) --- diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py index 245cc6e5b8..ffaf4f0a63 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.")