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. :-)
This commit is contained in:
José Matos 2019-06-05 14:06:09 +01:00
parent 051de65db9
commit 58734edff6

View File

@ -71,8 +71,8 @@ def minor_versions(major, last_minor_version):
# Regular expressions used # Regular expressions used
format_re = re.compile(r"(\d)[\.,]?(\d\d)") format_re = re.compile(r"(\d)[\.,]?(\d\d)")
fileformat = re.compile(r"\\lyxformat\s*(\S*)") fileformat = re.compile(r"\\lyxformat\s*(\S*)")
original_version = re.compile(r".*?LyX ([\d.]*)") original_version = re.compile(b".*?LyX ([\\d.]*)")
original_tex2lyx_version = re.compile(r".*?tex2lyx ([\d.]*)") original_tex2lyx_version = re.compile(b".*?tex2lyx ([\\d.]*)")
## ##
# file format information: # file format information:
@ -519,10 +519,10 @@ class LyX_base:
file, returns the most likely value, or None otherwise.""" file, returns the most likely value, or None otherwise."""
for line in self.header: for line in self.header:
if line[0] != "#": if line[0:1] != b"#":
return None return None
line = line.replace("fix",".") line = line.replace(b"fix",b".")
# need to test original_tex2lyx_version first because tex2lyx # need to test original_tex2lyx_version first because tex2lyx
# writes "#LyX file created by tex2lyx 2.2" # writes "#LyX file created by tex2lyx 2.2"
result = original_tex2lyx_version.match(line) result = original_tex2lyx_version.match(line)
@ -530,14 +530,14 @@ class LyX_base:
result = original_version.match(line) result = original_version.match(line)
if result: if result:
# Special know cases: reLyX and KLyX # 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" return "0.12"
if result: if result:
res = result.group(1) res = result.group(1)
if not res: if not res:
self.warning(line) self.warning(line)
#self.warning("Version %s" % result.group(1)) #self.warning("Version %s" % result.group(1))
return res return res.decode('ascii') if not PY2 else res
self.warning(str(self.header[:2])) self.warning(str(self.header[:2]))
return None return None