##// END OF EJS Templates
Check return value of all directory-finding functions is unicode....
Thomas Kluyver -
Show More
@@ -25,14 +25,20 b' from IPython.utils.importstring import import_item'
25 25 # Code
26 26 #-----------------------------------------------------------------------------
27 27
28 # in case filesystemencoding() returns None:
29 fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
28 fs_encoding = sys.getfilesystemencoding()
29
30 def _cast_unicode(s, enc=None):
31 """Turn 8-bit strings into unicode."""
32 if isinstance(s, bytes):
33 enc = enc or sys.getdefaultencoding()
34 return s.decode(enc)
35 return s
36
30 37
31 38 def _get_long_path_name(path):
32 39 """Dummy no-op."""
33 40 return path
34 41
35
36 42 if sys.platform == 'win32':
37 43 def _get_long_path_name(path):
38 44 """Get a long path name (expand ~) on Windows using ctypes.
@@ -172,7 +178,7 b' def get_home_dir():'
172 178 root=os.path.abspath(root).rstrip('\\')
173 179 if isdir(os.path.join(root, '_ipython')):
174 180 os.environ["IPYKITROOT"] = root
175 return root.decode(fs_encoding)
181 return _cast_unicode(root, fs_encoding)
176 182
177 183 if os.name == 'posix':
178 184 # Linux, Unix, AIX, OS X
@@ -187,11 +193,11 b' def get_home_dir():'
187 193 homedir = Popen('echo $HOME', shell=True,
188 194 stdout=PIPE).communicate()[0].strip()
189 195 if homedir:
190 return homedir.decode(fs_encoding)
196 return _cast_unicode(homedir, fs_encoding)
191 197 else:
192 198 raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
193 199 else:
194 return homedir.decode(fs_encoding)
200 return _cast_unicode(homedir, fs_encoding)
195 201 elif os.name == 'nt':
196 202 # Now for win9x, XP, Vista, 7?
197 203 # For some strange reason all of these return 'nt' for os.name.
@@ -205,7 +211,7 b' def get_home_dir():'
205 211 pass
206 212 else:
207 213 if isdir(homedir):
208 return homedir.decode(fs_encoding)
214 return _cast_unicode(homedir, fs_encoding)
209 215
210 216 # Now look for a local home directory
211 217 try:
@@ -214,7 +220,7 b' def get_home_dir():'
214 220 pass
215 221 else:
216 222 if isdir(homedir):
217 return homedir.decode(fs_encoding)
223 return _cast_unicode(homedir, fs_encoding)
218 224
219 225 # Now the users profile directory
220 226 try:
@@ -223,7 +229,7 b' def get_home_dir():'
223 229 pass
224 230 else:
225 231 if isdir(homedir):
226 return homedir.decode(fs_encoding)
232 return _cast_unicode(homedir, fs_encoding)
227 233
228 234 # Use the registry to get the 'My Documents' folder.
229 235 try:
@@ -238,7 +244,7 b' def get_home_dir():'
238 244 pass
239 245 else:
240 246 if isdir(homedir):
241 return homedir.decode(fs_encoding)
247 return _cast_unicode(homedir, fs_encoding)
242 248
243 249 # A user with a lot of unix tools in win32 may have defined $HOME.
244 250 # Try this as a last ditch option.
@@ -248,13 +254,13 b' def get_home_dir():'
248 254 pass
249 255 else:
250 256 if isdir(homedir):
251 return homedir.decode(fs_encoding)
257 return _cast_unicode(homedir, fs_encoding)
252 258
253 259 # If all else fails, raise HomeDirError
254 260 raise HomeDirError('No valid home directory could be found')
255 261 elif os.name == 'dos':
256 262 # Desperate, may do absurd things in classic MacOS. May work under DOS.
257 return 'C:\\'.decode(fs_encoding)
263 return u'C:\\'
258 264 else:
259 265 raise HomeDirError('No valid home directory could be found for your OS')
260 266
@@ -272,7 +278,7 b' def get_xdg_dir():'
272 278 # use ~/.config if not set OR empty
273 279 xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
274 280 if xdg and isdir(xdg):
275 return xdg.decode(fs_encoding)
281 return _cast_unicode(xdg, fs_encoding)
276 282
277 283 return None
278 284
@@ -311,13 +317,13 b' def get_ipython_dir():'
311 317 # not using XDG
312 318 ipdir = home_ipdir
313 319
314 return ipdir.decode(fs_encoding)
320 return _cast_unicode(ipdir, fs_encoding)
315 321
316 322
317 323 def get_ipython_package_dir():
318 324 """Get the base directory where IPython itself is installed."""
319 325 ipdir = os.path.dirname(IPython.__file__)
320 return ipdir.decode(fs_encoding)
326 return _cast_unicode(ipdir, fs_encoding)
321 327
322 328
323 329 def get_ipython_module_path(module_str):
@@ -332,7 +338,7 b' def get_ipython_module_path(module_str):'
332 338 mod = import_item(module_str)
333 339 the_path = mod.__file__.replace('.pyc', '.py')
334 340 the_path = the_path.replace('.pyo', '.py')
335 return the_path.decode(fs_encoding)
341 return _cast_unicode(the_path, fs_encoding)
336 342
337 343
338 344 def expand_path(s):
General Comments 0
You need to be logged in to leave comments. Login now