7258: Remove all instances of sys.getfilesystemencoding()

This function reliably returns a useful value only on MacOS X. On Linux it's incorrectly determined by the
locale's encoding and on Windows it always returns 'mbcs' which is correct only on FAT file systems.
(cherry picked from commit c1dcd6f59e)
This commit is contained in:
John Ralls 2014-04-20 14:23:26 -07:00
parent 583acbd438
commit 02e43f971f
7 changed files with 30 additions and 133 deletions

View File

@ -447,7 +447,6 @@ def make_dbdir(dbdir):
if not os.path.isdir(dbdir):
os.makedirs(dbdir)
except (IOError, OSError) as msg:
msg = conv_to_unicode(str(msg), glocale.getfilesystemencoding())
LOG.error(_("\nERROR: Wrong database path in Edit Menu->Preferences.\n"
"Open preferences and set correct database path.\n\n"
"Details: Could not make database directory:\n %s\n\n") % msg)

View File

@ -44,7 +44,7 @@ import uuid
#
#-------------------------------------------------------------------------
from .git_revision import get_git_revision
from .constfunc import get_env_var
from .constfunc import get_env_var, conv_to_unicode
#-------------------------------------------------------------------------
#
# Gramps Version
@ -124,17 +124,15 @@ USER_DIRLIST = (USER_HOME, HOME_DIR, VERSION_DIR, ENV_DIR, TEMP_DIR, THUMB_DIR,
# above this one, and that the plugins directory is below the root directory.
#
#-------------------------------------------------------------------------
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(
__file__), os.pardir))
if sys.version_info[0] < 3:
# test for sys.frozen to detect a py2exe executable on Windows
if hasattr(sys, "frozen"):
ROOT_DIR = os.path.abspath(os.path.dirname(
unicode(sys.executable, sys.getfilesystemencoding())))
else:
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(
unicode(__file__, sys.getfilesystemencoding())), os.pardir))
# test for sys.frozen to detect a py2exe executable on Windows
if sys.version_info[0] < 3 and hasattr(sys, "frozen"):
ROOT_DIR = os.path.abspath(os.path.dirname(unicode(sys.executable)))
else:
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(
conv_to_unicode(__file__)), os.pardir))
sys.path.insert(0, ROOT_DIR)
git_revision = get_git_revision(ROOT_DIR)
if sys.platform == 'win32' and git_revision == "":
git_revision = get_git_revision(os.path.split(ROOT_DIR)[1])

View File

@ -203,16 +203,6 @@ def find_referenced_handle(key, data):
val = val.encode('utf-8')
return val
def _encode(path):
"""
Conditionally return the unicode string encoded to sys.filesystem.encoding
"""
if not (isinstance(path, UNITYPE) and sys.version_info[0] < 3):
_LOG.debug("Didn't Encode %s", repr(path))
return path
_LOG.debug("Encoding %s", repr(path))
return path.encode(sys.getfilesystemencoding())
#-------------------------------------------------------------------------
#
# BsddbWriteCursor

View File

@ -64,12 +64,10 @@ def find_file( filename):
if os.path.isfile(filename):
return(filename)
except UnicodeError:
try:
fname = filename.encode(glocale.getfilesystemencoding())
if os.path.isfile(fname):
return fname
except UnicodeError:
return ''
LOG.error("Filename %s raised a Unicode Error %s.", repr(filename), err)
LOG.debug("Filename %s not found.", repr(filename))
return ''
def find_folder( filename):
# try the filename we got
@ -78,12 +76,11 @@ def find_folder( filename):
if os.path.isdir(filename):
return(filename)
except UnicodeError:
try:
fname = filename.encode(glocale.getfilesystemencoding())
if os.path.isdir(fname):
return fname
except UnicodeError:
return ''
LOG.error("Filename %s raised a Unicode Error %s", repr(filename), err)
LOG.debug("Filename %s either not found or not a directory.",
repr(filename))
return ''
def get_unicode_path_from_file_chooser(path):
"""
@ -95,75 +92,15 @@ def get_unicode_path_from_file_chooser(path):
:returns: The Unicode version of path.
"""
# make only unicode of path of type 'str'
if isinstance(path, UNITYPE):
return path
if not (isinstance(path, str)):
return path
## ONLY PYTHON 2 code shoulr reach this !
if win():
# in windows filechooser returns officially utf-8, not filesystemencoding
try:
return cuni(path)
except:
LOG.warn("Problem encountered converting string: %s." % path)
if sys.version_info[0] < 3:
return unicode(path, sys.getfilesystemencoding(),
errors='replace')
else:
#no idea, need to know what path is to know what to do
raise NotImplementedError("Path of type", type(path))
else:
try:
return cuni(path, sys.getfilesystemencoding())
except:
LOG.warn("Problem encountered converting string: %s." % path)
if sys.version_info[0] < 3:
return unicode(path, sys.getfilesystemencoding(), errors='replace')
else:
#no idea, need to know what path is to know what to do
raise NotImplementedError("Path of type", type(path))
return conv_to_unicode(path)
def get_unicode_path_from_env_var(path):
"""
Return the Unicode version of a path string.
:type path: str
:param path: The path to be converted to Unicode
:rtype: unicode
:returns: The Unicode version of path.
"""
#No need to do anything if it's already unicode
if isinstance(path, UNITYPE):
return path
# make only unicode of path of type 'str'
if not (isinstance(path, str)):
raise TypeError("path %s isn't a str" % str(path))
if win():
# In Windows path/filename returned from a environment variable is in filesystemencoding
try:
new_path = conv_to_unicode(path, sys.getfilesystemencoding())
return new_path
except:
LOG.warn("Problem encountered converting string: %s." % path)
if sys.version_info[0] < 3:
return unicode(path, sys.getfilesystemencoding(), errors='replace')
else:
#no idea, need to know what path is to know what to do
raise NotImplementedError("Path of type", type(path))
else:
try:
return str(path)
except:
LOG.warn("Problem encountered converting string: %s." % path)
if sys.version_info[0] < 3:
return unicode(path, sys.getfilesystemencoding(), errors='replace')
else:
#no idea, need to know what path is to know what to do
raise NotImplementedError("Path of type", type(path))
'''
Environment variables should always return unicodes.
'''
assert isinstance(path, UNITYPE)
return path
def get_new_filename(ext, folder='~/'):
ix = 1

View File

@ -364,8 +364,7 @@ class GrampsLocale(object):
from ctypes import cdll
try:
libintl = cdll.LoadLibrary('libintl-8')
libintl.bindtextdomain(localedomain,
localedir.encode(sys.getfilesystemencoding()))
libintl.bindtextdomain(localedomain, localedir)
libintl.textdomain(localedomain)
libintl.bind_textdomain_codeset(localedomain, "UTF-8")
@ -844,21 +843,6 @@ class GrampsLocale(object):
else:
return _("See details")
def getfilesystemencoding(self):
"""
If the locale isn't configured correctly, this will return
'ascii' or 'ANSI_X3.4-1968' or some other unfortunate
result. Current unix systems all encode filenames in utf-8,
and Microsoft Windows uses utf-16 (which they call mbcs). Make
sure we return the right value.
"""
encoding = sys.getfilesystemencoding()
if encoding in ("utf-8", "UTF-8", "utf8", "UTF8", "mbcs", "MBCS"):
return encoding
return "utf-8"
def sort_key(self, string):
"""
Return a value suitable to pass to the "key" parameter of sorted()

View File

@ -200,11 +200,7 @@ class ErrorDialog(Gtk.MessageDialog):
class RunDatabaseRepair(ErrorDialog):
def __init__(self, msg, parent=None):
if sys.version_info[0] < 3:
msg = cuni(str(msg).decode(glocale.getfilesystemencoding()))
else:
msg = str(msg)
ErrorDialog.__init__(
ErrorDialog.__init__(
self,
_('Error detected in database'),
_('Gramps has detected an error in the database. This can '
@ -215,11 +211,7 @@ class RunDatabaseRepair(ErrorDialog):
class DBErrorDialog(ErrorDialog):
def __init__(self, msg, parent=None):
if sys.version_info[0] < 3:
msg = cuni(str(msg).decode(glocale.getfilesystemencoding()))
else:
msg = str(msg)
ErrorDialog.__init__(
ErrorDialog.__init__(
self,
_("Low level database corruption detected"),
_("Gramps has detected a problem in the underlying "

View File

@ -209,11 +209,8 @@ class PersonBoxWidgetCairo(_PersonWidgetBase):
if image:
image_path = self.get_image(dbstate, person)
if image_path and os.path.exists(image_path):
# Work around a bug in pycairo 1.8.10 and earlier. OSX and
# Win32 AIOs use pycairo 1.10.0, in which the bug is fixed.
if (sys.version_info[0] < 3 and lin()):
image_path = image_path.encode(glocale.getfilesystemencoding())
self.img_surf = cairo.ImageSurface.create_from_png(image_path)
with open(image_path, 'rb') as image:
self.img_surf = cairo.ImageSurface.create_from_png(image)
# enable mouse-over
self.connect("enter-notify-event", self.cb_on_enter)