diff --git a/gramps/gen/constfunc.py b/gramps/gen/constfunc.py index 28e50b703..df2e6972d 100644 --- a/gramps/gen/constfunc.py +++ b/gramps/gen/constfunc.py @@ -178,6 +178,11 @@ def mod_key(): def get_env_var(name, default=None): + ''' + Python2 on Windows can't directly read unicode values from + environment variables. This routine does so using the native C + wide-character function. + ''' if not name or not name in os.environ: return default @@ -192,4 +197,22 @@ def get_env_var(name, default=None): return buf.value return os.environ[name] - + +def get_curr_dir(): + ''' + In Python2 on Windows, os.getcwd() returns a string encoded with + the current code page, which may not be able to correctly handle + an arbitrary unicode character in a path. This function uses the + native GetCurrentDirectory function to return a unicode cwd. + ''' + if not sys.version_info[0] < 3 and win(): + return os.getcwd() + + n = ctypes.windll.kernel32.GetCurrentDirectoryW(0, None) + if n == 0: + return None + buf = ctypes.create_unicode_buffer(n+1) + ctypes.windll.kernel32.GetCurrentDirectoryW(n, buf) + return buf.value + + diff --git a/gramps/gui/plug/_guioptions.py b/gramps/gui/plug/_guioptions.py index 01266d2a7..1413e98d1 100644 --- a/gramps/gui/plug/_guioptions.py +++ b/gramps/gui/plug/_guioptions.py @@ -35,8 +35,6 @@ from __future__ import unicode_literals # python modules # #------------------------------------------------------------------------ -from gramps.gen.const import GRAMPS_LOCALE as glocale -_ = glocale.translation.gettext import os import sys @@ -63,7 +61,9 @@ from ..dialog import OptionDialog from ..selectors import SelectorFactory from gramps.gen.display.name import displayer as _nd from gramps.gen.filters import GenericFilterFactory, GenericFilter, rules -from gramps.gen.constfunc import cuni, STRTYPE +from gramps.gen.constfunc import conv_to_unicode, uni_to_gui, get_curr_dir +from gramps.gen.const import GRAMPS_LOCALE as glocale +_ = glocale.translation.gettext #------------------------------------------------------------------------ # @@ -1695,7 +1695,7 @@ class GuiDestinationOption(Gtk.HBox): name, tail = os.path.split(name) if not name: # Avoid infinite loops - name = os.getcwd() + name = get_curr_dir fcd.set_current_folder(name) else: fcd.set_current_name(name) diff --git a/gramps/gui/plug/report/_fileentry.py b/gramps/gui/plug/report/_fileentry.py index 9c85d1080..1007d287b 100644 --- a/gramps/gui/plug/report/_fileentry.py +++ b/gramps/gui/plug/report/_fileentry.py @@ -25,8 +25,7 @@ import os from gi.repository import Gtk from gi.repository import GObject -from gramps.gen.utils.file import get_unicode_path_from_file_chooser - +from gramps.gen.constfunc import conv_to_unicode, get_curr_dir, uni_to_gui class FileEntry(Gtk.HBox): """ A widget that allows the user to select a file from the file system """ def __init__(self, defname, title): @@ -86,7 +85,7 @@ class FileEntry(Gtk.HBox): self.__base_path = os.path.dirname(path) self.__file_name = os.path.basename(path) else: - self.__base_path = os.getcwd() + self.__base_path = get_curr_dir() self.__file_name = path self.entry.set_text(os.path.join(self.__base_path, self.__file_name)) diff --git a/gramps/plugins/tool/eventcmp.py b/gramps/plugins/tool/eventcmp.py index 6c3d1e5b9..e6c71595b 100644 --- a/gramps/plugins/tool/eventcmp.py +++ b/gramps/plugins/tool/eventcmp.py @@ -62,6 +62,7 @@ from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.sgettext from gramps.gui.glade import Glade from gramps.gui.editors import FilterEditor +from gramps.gen.constfunc import conv_to_unicode, uni_to_gui, get_curr_dir #------------------------------------------------------------------------- # @@ -397,7 +398,7 @@ class DisplayChart(ManagedWindow): Gtk.STOCK_SAVE, Gtk.ResponseType.OK)) - f.set_current_folder(os.getcwd()) + f.set_current_folder(get_curr_dir()) status = f.run() f.hide() diff --git a/gramps/plugins/webreport/narrativeweb.py b/gramps/plugins/webreport/narrativeweb.py index cb4ac28fa..0838b1cfa 100644 --- a/gramps/plugins/webreport/narrativeweb.py +++ b/gramps/plugins/webreport/narrativeweb.py @@ -126,7 +126,7 @@ from gramps.gen.utils.string import conf_strings from gramps.gen.utils.file import media_path_full from gramps.gen.utils.alive import probably_alive from gramps.gen.utils.db import get_source_and_citation_referents -from gramps.gen.constfunc import win, cuni, conv_to_unicode, UNITYPE +from gramps.gen.constfunc import win, cuni, conv_to_unicode, UNITYPE, get_curr_dir from gramps.gen.config import config from gramps.gui.thumbnails import get_thumbnail_path, run_thumbnailer from gramps.gen.utils.image import image_size, resize_to_jpeg_buffer @@ -6965,7 +6965,7 @@ class NavWebReport(Report): if not self.use_archive: dir_name = self.target_path if dir_name is None: - dir_name = os.getcwd() + dir_name = get_curr_dir() elif not os.path.isdir(dir_name): parent_dir = os.path.dirname(dir_name) if not os.path.isdir(parent_dir):