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