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