(path, *, strict=False)
| 672 | return tail |
| 673 | |
| 674 | def realpath(path, *, strict=False): |
| 675 | path = normpath(path) |
| 676 | if isinstance(path, bytes): |
| 677 | prefix = b'\\\\?\\' |
| 678 | unc_prefix = b'\\\\?\\UNC\\' |
| 679 | new_unc_prefix = b'\\\\' |
| 680 | cwd = os.getcwdb() |
| 681 | # bpo-38081: Special case for realpath(b'nul') |
| 682 | if normcase(path) == normcase(os.fsencode(devnull)): |
| 683 | return b'\\\\.\\NUL' |
| 684 | else: |
| 685 | prefix = '\\\\?\\' |
| 686 | unc_prefix = '\\\\?\\UNC\\' |
| 687 | new_unc_prefix = '\\\\' |
| 688 | cwd = os.getcwd() |
| 689 | # bpo-38081: Special case for realpath('nul') |
| 690 | if normcase(path) == normcase(devnull): |
| 691 | return '\\\\.\\NUL' |
| 692 | had_prefix = path.startswith(prefix) |
| 693 | if not had_prefix and not isabs(path): |
| 694 | path = join(cwd, path) |
| 695 | try: |
| 696 | path = _getfinalpathname(path) |
| 697 | initial_winerror = 0 |
| 698 | except ValueError as ex: |
| 699 | # gh-106242: Raised for embedded null characters |
| 700 | # In strict mode, we convert into an OSError. |
| 701 | # Non-strict mode returns the path as-is, since we've already |
| 702 | # made it absolute. |
| 703 | if strict: |
| 704 | raise OSError(str(ex)) from None |
| 705 | path = normpath(path) |
| 706 | except OSError as ex: |
| 707 | if strict: |
| 708 | raise |
| 709 | initial_winerror = ex.winerror |
| 710 | path = _getfinalpathname_nonstrict(path) |
| 711 | # The path returned by _getfinalpathname will always start with \\?\ - |
| 712 | # strip off that prefix unless it was already provided on the original |
| 713 | # path. |
| 714 | if not had_prefix and path.startswith(prefix): |
| 715 | # For UNC paths, the prefix will actually be \\?\UNC\ |
| 716 | # Handle that case as well. |
| 717 | if path.startswith(unc_prefix): |
| 718 | spath = new_unc_prefix + path[len(unc_prefix):] |
| 719 | else: |
| 720 | spath = path[len(prefix):] |
| 721 | # Ensure that the non-prefixed path resolves to the same path |
| 722 | try: |
| 723 | if _getfinalpathname(spath) == path: |
| 724 | path = spath |
| 725 | except ValueError as ex: |
| 726 | # Unexpected, as an invalid path should not have gained a prefix |
| 727 | # at any point, but we ignore this error just in case. |
| 728 | pass |
| 729 | except OSError as ex: |
| 730 | # If the path does not exist and originally did not exist, then |
| 731 | # strip the prefix anyway. |
no test coverage detected