7258: Replace os.getcwd() with gramps.gen.constfunc.get_curr_dir()

Py2 on Win32 returns a str from os.getcwd(), which doesn't work with unicode in the path.
New function get_curr_dir() uses the Windows GetCurrentDirectoryW to return a Unicode.
This commit is contained in:
John Ralls 2014-04-20 15:13:31 -07:00
parent 10921cee29
commit ecfad157a8
5 changed files with 34 additions and 11 deletions

View File

@ -176,6 +176,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
@ -190,4 +195,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

View File

@ -34,8 +34,6 @@ from __future__ import unicode_literals
# python modules
#
#------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
import os
import sys
@ -62,7 +60,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
#------------------------------------------------------------------------
#
@ -1694,7 +1694,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)

View File

@ -23,8 +23,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):
@ -84,7 +83,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))

View File

@ -60,6 +60,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
#-------------------------------------------------------------------------
#
@ -396,7 +397,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()

View File

@ -125,7 +125,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
@ -6949,7 +6949,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):