Expand ~ and ~user constructs. If user or $HOME is unknown, do nothing.
(path)
| 341 | # variable expansion.) |
| 342 | |
| 343 | def expanduser(path): |
| 344 | """Expand ~ and ~user constructs. |
| 345 | |
| 346 | If user or $HOME is unknown, do nothing.""" |
| 347 | path = os.fspath(path) |
| 348 | if isinstance(path, bytes): |
| 349 | seps = b'\\/' |
| 350 | tilde = b'~' |
| 351 | else: |
| 352 | seps = '\\/' |
| 353 | tilde = '~' |
| 354 | if not path.startswith(tilde): |
| 355 | return path |
| 356 | i, n = 1, len(path) |
| 357 | while i < n and path[i] not in seps: |
| 358 | i += 1 |
| 359 | |
| 360 | if 'USERPROFILE' in os.environ: |
| 361 | userhome = os.environ['USERPROFILE'] |
| 362 | elif 'HOMEPATH' not in os.environ: |
| 363 | return path |
| 364 | else: |
| 365 | drive = os.environ.get('HOMEDRIVE', '') |
| 366 | userhome = join(drive, os.environ['HOMEPATH']) |
| 367 | |
| 368 | if i != 1: #~user |
| 369 | target_user = path[1:i] |
| 370 | if isinstance(target_user, bytes): |
| 371 | target_user = os.fsdecode(target_user) |
| 372 | current_user = os.environ.get('USERNAME') |
| 373 | |
| 374 | if target_user != current_user: |
| 375 | # Try to guess user home directory. By default all user |
| 376 | # profile directories are located in the same place and are |
| 377 | # named by corresponding usernames. If userhome isn't a |
| 378 | # normal profile directory, this guess is likely wrong, |
| 379 | # so we bail out. |
| 380 | if current_user != basename(userhome): |
| 381 | return path |
| 382 | userhome = join(dirname(userhome), target_user) |
| 383 | |
| 384 | if isinstance(path, bytes): |
| 385 | userhome = os.fsencode(userhome) |
| 386 | |
| 387 | return userhome + path[i:] |
| 388 | |
| 389 | |
| 390 | # Expand paths containing shell variable substitutions. |
no test coverage detected
searching dependent graphs…