##// END OF EJS Templates
Backport PR #8614: Safely encode paths in IPython.utils.path.compress_user...
Min RK -
Show More
@@ -83,7 +83,8 b" def unquote_filename(name, win32=(sys.platform=='win32')):"
83
83
84 def compress_user(path):
84 def compress_user(path):
85 """Reverse of :func:`os.path.expanduser`
85 """Reverse of :func:`os.path.expanduser`
86 """
86 """
87 path = py3compat.unicode_to_str(path, sys.getfilesystemencoding())
87 home = os.path.expanduser('~')
88 home = os.path.expanduser('~')
88 if path.startswith(home):
89 if path.startswith(home):
89 path = "~" + path[len(home):]
90 path = "~" + path[len(home):]
@@ -176,13 +177,13 b' def get_home_dir(require_writable=False):'
176 """Return the 'home' directory, as a unicode string.
177 """Return the 'home' directory, as a unicode string.
177
178
178 Uses os.path.expanduser('~'), and checks for writability.
179 Uses os.path.expanduser('~'), and checks for writability.
179
180
180 See stdlib docs for how this is determined.
181 See stdlib docs for how this is determined.
181 $HOME is first priority on *ALL* platforms.
182 $HOME is first priority on *ALL* platforms.
182
183
183 Parameters
184 Parameters
184 ----------
185 ----------
185
186
186 require_writable : bool [default: False]
187 require_writable : bool [default: False]
187 if True:
188 if True:
188 guarantees the return value is a writable directory, otherwise
189 guarantees the return value is a writable directory, otherwise
@@ -195,7 +196,7 b' def get_home_dir(require_writable=False):'
195 # Next line will make things work even when /home/ is a symlink to
196 # Next line will make things work even when /home/ is a symlink to
196 # /usr/home as it is on FreeBSD, for example
197 # /usr/home as it is on FreeBSD, for example
197 homedir = os.path.realpath(homedir)
198 homedir = os.path.realpath(homedir)
198
199
199 if not _writable_dir(homedir) and os.name == 'nt':
200 if not _writable_dir(homedir) and os.name == 'nt':
200 # expanduser failed, use the registry to get the 'My Documents' folder.
201 # expanduser failed, use the registry to get the 'My Documents' folder.
201 try:
202 try:
@@ -211,7 +212,7 b' def get_home_dir(require_writable=False):'
211 key.Close()
212 key.Close()
212 except:
213 except:
213 pass
214 pass
214
215
215 if (not require_writable) or _writable_dir(homedir):
216 if (not require_writable) or _writable_dir(homedir):
216 return py3compat.cast_unicode(homedir, fs_encoding)
217 return py3compat.cast_unicode(homedir, fs_encoding)
217 else:
218 else:
@@ -577,10 +578,10 b' def link_or_copy(src, dst):'
577
578
578 def ensure_dir_exists(path, mode=0o755):
579 def ensure_dir_exists(path, mode=0o755):
579 """ensure that a directory exists
580 """ensure that a directory exists
580
581
581 If it doesn't exist, try to create it and protect against a race condition
582 If it doesn't exist, try to create it and protect against a race condition
582 if another process is doing the same.
583 if another process is doing the same.
583
584
584 The default permissions are 755, which differ from os.makedirs default of 777.
585 The default permissions are 755, which differ from os.makedirs default of 777.
585 """
586 """
586 if not os.path.exists(path):
587 if not os.path.exists(path):
General Comments 0
You need to be logged in to leave comments. Login now