##// END OF EJS Templates
Merge branch 'ipdir-unicode'...
MinRK -
r3809:3fc6efed merge
parent child Browse files
Show More
@@ -25,12 +25,20 from IPython.utils.importstring import import_item
25 # Code
25 # Code
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
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
28
37
29 def _get_long_path_name(path):
38 def _get_long_path_name(path):
30 """Dummy no-op."""
39 """Dummy no-op."""
31 return path
40 return path
32
41
33
34 if sys.platform == 'win32':
42 if sys.platform == 'win32':
35 def _get_long_path_name(path):
43 def _get_long_path_name(path):
36 """Get a long path name (expand ~) on Windows using ctypes.
44 """Get a long path name (expand ~) on Windows using ctypes.
@@ -170,7 +178,7 def get_home_dir():
170 root=os.path.abspath(root).rstrip('\\')
178 root=os.path.abspath(root).rstrip('\\')
171 if isdir(os.path.join(root, '_ipython')):
179 if isdir(os.path.join(root, '_ipython')):
172 os.environ["IPYKITROOT"] = root
180 os.environ["IPYKITROOT"] = root
173 return root.decode(sys.getfilesystemencoding())
181 return _cast_unicode(root, fs_encoding)
174
182
175 if os.name == 'posix':
183 if os.name == 'posix':
176 # Linux, Unix, AIX, OS X
184 # Linux, Unix, AIX, OS X
@@ -185,11 +193,11 def get_home_dir():
185 homedir = Popen('echo $HOME', shell=True,
193 homedir = Popen('echo $HOME', shell=True,
186 stdout=PIPE).communicate()[0].strip()
194 stdout=PIPE).communicate()[0].strip()
187 if homedir:
195 if homedir:
188 return homedir.decode(sys.getfilesystemencoding())
196 return _cast_unicode(homedir, fs_encoding)
189 else:
197 else:
190 raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
198 raise HomeDirError('Undefined $HOME, IPython cannot proceed.')
191 else:
199 else:
192 return homedir.decode(sys.getfilesystemencoding())
200 return _cast_unicode(homedir, fs_encoding)
193 elif os.name == 'nt':
201 elif os.name == 'nt':
194 # Now for win9x, XP, Vista, 7?
202 # Now for win9x, XP, Vista, 7?
195 # 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.
@@ -203,7 +211,7 def get_home_dir():
203 pass
211 pass
204 else:
212 else:
205 if isdir(homedir):
213 if isdir(homedir):
206 return homedir.decode(sys.getfilesystemencoding())
214 return _cast_unicode(homedir, fs_encoding)
207
215
208 # Now look for a local home directory
216 # Now look for a local home directory
209 try:
217 try:
@@ -212,7 +220,7 def get_home_dir():
212 pass
220 pass
213 else:
221 else:
214 if isdir(homedir):
222 if isdir(homedir):
215 return homedir.decode(sys.getfilesystemencoding())
223 return _cast_unicode(homedir, fs_encoding)
216
224
217 # Now the users profile directory
225 # Now the users profile directory
218 try:
226 try:
@@ -221,7 +229,7 def get_home_dir():
221 pass
229 pass
222 else:
230 else:
223 if isdir(homedir):
231 if isdir(homedir):
224 return homedir.decode(sys.getfilesystemencoding())
232 return _cast_unicode(homedir, fs_encoding)
225
233
226 # Use the registry to get the 'My Documents' folder.
234 # Use the registry to get the 'My Documents' folder.
227 try:
235 try:
@@ -236,7 +244,7 def get_home_dir():
236 pass
244 pass
237 else:
245 else:
238 if isdir(homedir):
246 if isdir(homedir):
239 return homedir.decode(sys.getfilesystemencoding())
247 return _cast_unicode(homedir, fs_encoding)
240
248
241 # 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.
242 # Try this as a last ditch option.
250 # Try this as a last ditch option.
@@ -246,13 +254,13 def get_home_dir():
246 pass
254 pass
247 else:
255 else:
248 if isdir(homedir):
256 if isdir(homedir):
249 return homedir.decode(sys.getfilesystemencoding())
257 return _cast_unicode(homedir, fs_encoding)
250
258
251 # If all else fails, raise HomeDirError
259 # If all else fails, raise HomeDirError
252 raise HomeDirError('No valid home directory could be found')
260 raise HomeDirError('No valid home directory could be found')
253 elif os.name == 'dos':
261 elif os.name == 'dos':
254 # 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.
255 return 'C:\\'.decode(sys.getfilesystemencoding())
263 return u'C:\\'
256 else:
264 else:
257 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')
258
266
@@ -270,7 +278,7 def get_xdg_dir():
270 # use ~/.config if not set OR empty
278 # use ~/.config if not set OR empty
271 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')
272 if xdg and isdir(xdg):
280 if xdg and isdir(xdg):
273 return xdg.decode(sys.getfilesystemencoding())
281 return _cast_unicode(xdg, fs_encoding)
274
282
275 return None
283 return None
276
284
@@ -309,13 +317,13 def get_ipython_dir():
309 # not using XDG
317 # not using XDG
310 ipdir = home_ipdir
318 ipdir = home_ipdir
311
319
312 return ipdir.decode(sys.getfilesystemencoding())
320 return _cast_unicode(ipdir, fs_encoding)
313
321
314
322
315 def get_ipython_package_dir():
323 def get_ipython_package_dir():
316 """Get the base directory where IPython itself is installed."""
324 """Get the base directory where IPython itself is installed."""
317 ipdir = os.path.dirname(IPython.__file__)
325 ipdir = os.path.dirname(IPython.__file__)
318 return ipdir.decode(sys.getfilesystemencoding())
326 return _cast_unicode(ipdir, fs_encoding)
319
327
320
328
321 def get_ipython_module_path(module_str):
329 def get_ipython_module_path(module_str):
@@ -330,7 +338,7 def get_ipython_module_path(module_str):
330 mod = import_item(module_str)
338 mod = import_item(module_str)
331 the_path = mod.__file__.replace('.pyc', '.py')
339 the_path = mod.__file__.replace('.pyc', '.py')
332 the_path = the_path.replace('.pyo', '.py')
340 the_path = the_path.replace('.pyo', '.py')
333 return the_path.decode(sys.getfilesystemencoding())
341 return _cast_unicode(the_path, fs_encoding)
334
342
335
343
336 def expand_path(s):
344 def expand_path(s):
General Comments 0
You need to be logged in to leave comments. Login now