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.
This commit is contained in:
John Ralls 2014-04-20 14:23:26 -07:00
parent efa3741579
commit c1dcd6f59e
7 changed files with 28 additions and 137 deletions

View File

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

View File

@ -42,7 +42,7 @@ import uuid
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from .git_revision import get_git_revision from .git_revision import get_git_revision
from .constfunc import get_env_var from .constfunc import get_env_var, conv_to_unicode
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Gramps Version # Gramps Version
@ -122,17 +122,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. # above this one, and that the plugins directory is below the root directory.
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname( # test for sys.frozen to detect a py2exe executable on Windows
__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))
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) git_revision = get_git_revision(ROOT_DIR)
if sys.platform == 'win32' and git_revision == "": if sys.platform == 'win32' and git_revision == "":
git_revision = get_git_revision(os.path.split(ROOT_DIR)[1]) git_revision = get_git_revision(os.path.split(ROOT_DIR)[1])

View File

@ -220,16 +220,6 @@ def find_referenced_handle(key, data):
val = val.encode('utf-8') val = val.encode('utf-8')
return val 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 # BsddbWriteCursor

View File

@ -64,14 +64,9 @@ def find_file( filename):
if os.path.isfile(filename): if os.path.isfile(filename):
return(filename) return(filename)
except UnicodeError: except UnicodeError:
try: LOG.error("Filename %s raised a Unicode Error %s.", repr(filename), err)
fname = filename.encode(glocale.getfilesystemencoding())
if os.path.isfile(fname):
return fname
except UnicodeError:
pass
# not found LOG.debug("Filename %s not found.", repr(filename))
return '' return ''
def find_folder( filename): def find_folder( filename):
@ -81,14 +76,10 @@ def find_folder( filename):
if os.path.isdir(filename): if os.path.isdir(filename):
return(filename) return(filename)
except UnicodeError: except UnicodeError:
try: LOG.error("Filename %s raised a Unicode Error %s", repr(filename), err)
fname = filename.encode(glocale.getfilesystemencoding())
if os.path.isdir(fname):
return fname
except UnicodeError:
pass
# not found LOG.debug("Filename %s either not found or not a directory.",
repr(filename))
return '' return ''
def get_unicode_path_from_file_chooser(path): def get_unicode_path_from_file_chooser(path):
@ -101,75 +92,15 @@ def get_unicode_path_from_file_chooser(path):
:returns: The Unicode version of path. :returns: The Unicode version of path.
""" """
# make only unicode of path of type 'str' # make only unicode of path of type 'str'
if isinstance(path, UNITYPE): return conv_to_unicode(path)
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))
def get_unicode_path_from_env_var(path): def get_unicode_path_from_env_var(path):
""" '''
Return the Unicode version of a path string. Environment variables should always return unicodes.
'''
:type path: str assert isinstance(path, UNITYPE)
:param path: The path to be converted to Unicode return path
: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))
def get_new_filename(ext, folder='~/'): def get_new_filename(ext, folder='~/'):
ix = 1 ix = 1

View File

@ -368,8 +368,7 @@ class GrampsLocale(object):
from ctypes import cdll from ctypes import cdll
try: try:
libintl = cdll.LoadLibrary('libintl-8') libintl = cdll.LoadLibrary('libintl-8')
libintl.bindtextdomain(localedomain, libintl.bindtextdomain(localedomain, localedir)
localedir.encode(sys.getfilesystemencoding()))
libintl.textdomain(localedomain) libintl.textdomain(localedomain)
libintl.bind_textdomain_codeset(localedomain, "UTF-8") libintl.bind_textdomain_codeset(localedomain, "UTF-8")
@ -854,21 +853,6 @@ class GrampsLocale(object):
else: else:
return _("See details") 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): def sort_key(self, string):
""" """
Return a value suitable to pass to the "key" parameter of sorted() Return a value suitable to pass to the "key" parameter of sorted()

View File

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

View File

@ -207,11 +207,8 @@ class PersonBoxWidgetCairo(_PersonWidgetBase):
if image: if image:
image_path = self.get_image(dbstate, person) image_path = self.get_image(dbstate, person)
if image_path and os.path.exists(image_path): if image_path and os.path.exists(image_path):
# Work around a bug in pycairo 1.8.10 and earlier. OSX and with open(image_path, 'rb') as image:
# Win32 AIOs use pycairo 1.10.0, in which the bug is fixed. self.img_surf = cairo.ImageSurface.create_from_png(image)
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)
# enable mouse-over # enable mouse-over
self.connect("enter-notify-event", self.cb_on_enter) self.connect("enter-notify-event", self.cb_on_enter)