From c1dcd6f59e730bf93c9732df9734a7004b47a894 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sun, 20 Apr 2014 14:23:26 -0700 Subject: [PATCH] 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. --- gramps/cli/clidbman.py | 1 - gramps/gen/const.py | 20 +++--- gramps/gen/db/write.py | 10 --- gramps/gen/utils/file.py | 97 +++++------------------------ gramps/gen/utils/grampslocale.py | 18 +----- gramps/gui/dialog.py | 12 +--- gramps/plugins/view/pedigreeview.py | 7 +-- 7 files changed, 28 insertions(+), 137 deletions(-) diff --git a/gramps/cli/clidbman.py b/gramps/cli/clidbman.py index 72401f656..a1bf041b7 100644 --- a/gramps/cli/clidbman.py +++ b/gramps/cli/clidbman.py @@ -449,7 +449,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) diff --git a/gramps/gen/const.py b/gramps/gen/const.py index 4b935c0be..00f32a681 100644 --- a/gramps/gen/const.py +++ b/gramps/gen/const.py @@ -42,7 +42,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 @@ -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. # #------------------------------------------------------------------------- -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]) diff --git a/gramps/gen/db/write.py b/gramps/gen/db/write.py index 88fce65fe..cb404dff2 100644 --- a/gramps/gen/db/write.py +++ b/gramps/gen/db/write.py @@ -220,16 +220,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 diff --git a/gramps/gen/utils/file.py b/gramps/gen/utils/file.py index 050dd1892..5eb438bd4 100644 --- a/gramps/gen/utils/file.py +++ b/gramps/gen/utils/file.py @@ -64,14 +64,9 @@ 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: - pass - - # not found + 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): @@ -81,14 +76,10 @@ 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: - pass - - # not found + 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): @@ -101,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 diff --git a/gramps/gen/utils/grampslocale.py b/gramps/gen/utils/grampslocale.py index 0c0d8ee7e..c55326249 100644 --- a/gramps/gen/utils/grampslocale.py +++ b/gramps/gen/utils/grampslocale.py @@ -368,8 +368,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") @@ -854,21 +853,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() diff --git a/gramps/gui/dialog.py b/gramps/gui/dialog.py index 13c610f3a..6d4fe29eb 100644 --- a/gramps/gui/dialog.py +++ b/gramps/gui/dialog.py @@ -198,11 +198,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 ' @@ -213,11 +209,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 " diff --git a/gramps/plugins/view/pedigreeview.py b/gramps/plugins/view/pedigreeview.py index 050b55c54..3fb3d16a5 100644 --- a/gramps/plugins/view/pedigreeview.py +++ b/gramps/plugins/view/pedigreeview.py @@ -207,11 +207,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)