lyx2lyx: fix #9006 (python3 with non-utf8 encoding as the system default)

This patch fixes lyx2lyx running on python 3 for those systems
where the default encoding is not UTF-8, since open by default
uses the default system encoding.
This commit is contained in:
José Matos 2017-03-27 11:35:29 +01:00
parent 1269860b45
commit bcf715f398
2 changed files with 18 additions and 9 deletions

View File

@ -23,12 +23,7 @@ import unicodedata
import sys, os import sys, os
from parser_tools import find_token, find_end_of, find_tokens, get_value from parser_tools import find_token, find_end_of, find_tokens, get_value
from unicode_symbols import read_unicodesymbols
# Provide support for both python 2 and 3
PY2 = sys.version_info[0] == 2
if not PY2:
unichr = chr
# End of code to support for both python 2 and 3
#################################################################### ####################################################################
# Private helper functions # Private helper functions
@ -151,9 +146,18 @@ def set_option(document, m, option, value):
return l return l
# FIXME: Use the version in unicode_symbols.py which has some bug fixes # FIXME: Remove this function if the version imported from unicode_symbols works.
def read_unicodesymbols(): # This function was the predecessor from that function, that in the meanwhile got
# new fixes.
def read_unicodesymbols2():
" Read the unicodesymbols list of unicode characters and corresponding commands." " Read the unicodesymbols list of unicode characters and corresponding commands."
# Provide support for both python 2 and 3
PY2 = sys.version_info[0] == 2
if not PY2:
unichr = chr
# End of code to support for both python 2 and 3
pathname = os.path.abspath(os.path.dirname(sys.argv[0])) pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols')) fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols'))
spec_chars = [] spec_chars = []

View File

@ -29,7 +29,12 @@ if not PY2:
def read_unicodesymbols(): def read_unicodesymbols():
" Read the unicodesymbols list of unicode characters and corresponding commands." " Read the unicodesymbols list of unicode characters and corresponding commands."
pathname = os.path.abspath(os.path.dirname(sys.argv[0])) pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols')) filename = os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols')
# For python 3+ we have to specify the encoding for those systems
# where the default is not UTF-8
fp = open(filename, encoding="utf8") if (not PY2) else open(filename)
spec_chars = [] spec_chars = []
# A backslash, followed by some non-word character, and then a character # A backslash, followed by some non-word character, and then a character
# in brackets. The idea is to check for constructs like: \"{u}, which is how # in brackets. The idea is to check for constructs like: \"{u}, which is how