7258: transcode os.path.join args from the fs enc to prevent a crash
Fix 3rd party plugin loading for Py2/Py3 in Windows when sys.path is in Unicode or/and is not encodable in 'mbcs' encodings
This commit is contained in:
parent
2155ae381f
commit
7fdf0f9225
@ -1307,7 +1307,7 @@ class DbBsddb(DbBsddbRead, DbWriteBase, UpdateCallback):
|
||||
if sys.version_info[0] >= 3:
|
||||
key= str(key)
|
||||
else:
|
||||
key = str(tuple(str(k) for k in key))
|
||||
key = str(tuple(k.encode('utf-8') for k in key))
|
||||
if isinstance(key, UNITYPE):
|
||||
key = key.encode('utf-8')
|
||||
if not self.readonly:
|
||||
|
@ -54,7 +54,7 @@ _ = glocale.translation.gettext
|
||||
#-------------------------------------------------------------------------
|
||||
from ..config import config
|
||||
from . import PluginRegister, ImportPlugin, ExportPlugin, DocGenPlugin
|
||||
from ..constfunc import STRTYPE
|
||||
from ..constfunc import STRTYPE, win
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
@ -258,9 +258,36 @@ class BasePluginManager(object):
|
||||
try:
|
||||
module = __import__(pdata.mod_name)
|
||||
except ValueError as err:
|
||||
LOG.warning('Plugin error: %s', err)
|
||||
# Python3 on Windows work with unicode in sys.path
|
||||
# but they are mbcs encode for checking validity
|
||||
if (sys.version_info[0] >= 3) and win():
|
||||
# we don't want to load Gramps core plugin like this
|
||||
# only 3rd party plugins
|
||||
if "gramps" in pdata.fpath:
|
||||
try:
|
||||
sys.path.insert(0, ".")
|
||||
oldwd = os.getcwd()
|
||||
os.chdir(pdata.fpath)
|
||||
module = __import__(pdata.mod_name)
|
||||
os.chdir(oldwd)
|
||||
sys.path.pop(0)
|
||||
except ValueError as err:
|
||||
LOG.warning('Plugin error: %s', err)
|
||||
else:
|
||||
LOG.warning('Plugin error: %s', err)
|
||||
except ImportError as err:
|
||||
LOG.warning('Plugin error: %s', err)
|
||||
# Python2 on Windows not work with unicode in sys.path
|
||||
# but module can be loaded from current directory
|
||||
if (sys.version_info[0] < 3) and win():
|
||||
try:
|
||||
oldwd = os.getcwd()
|
||||
os.chdir(pdata.fpath)
|
||||
module = __import__(pdata.mod_name)
|
||||
os.chdir(oldwd)
|
||||
except ImportError as err:
|
||||
LOG.warning('Plugin error: %s', err)
|
||||
else:
|
||||
LOG.warning('Plugin error: %s', err)
|
||||
sys.path.pop(0)
|
||||
else:
|
||||
print("WARNING: module cannot be loaded")
|
||||
|
@ -1102,10 +1102,9 @@ class PluginRegister(object):
|
||||
lenpd = len(self.__plugindata)
|
||||
full_filename = os.path.join(dir, filename)
|
||||
if sys.version_info[0] < 3:
|
||||
full_filename = full_filename.encode(glocale.getfilesystemencoding())
|
||||
fd = open(full_filename, "r")
|
||||
else:
|
||||
fd = io.open(full_filename, 'r', encoding = 'utf-8')
|
||||
fd = io.open(full_filename, "r")
|
||||
stream = fd.read()
|
||||
fd.close()
|
||||
if os.path.exists(os.path.join(os.path.dirname(full_filename),
|
||||
|
Loading…
x
Reference in New Issue
Block a user