MCPcopy Index your code
hub / github.com/python/cpython / realpath

Function realpath

Lib/posixpath.py:389–513  ·  view source on GitHub ↗

Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path.

(filename, /, *, strict=False)

Source from the content-addressed store, hash-verified

387# filesystem).
388
389def realpath(filename, /, *, strict=False):
390 """Return the canonical path of the specified filename, eliminating any
391symbolic links encountered in the path."""
392 filename = os.fspath(filename)
393 if isinstance(filename, bytes):
394 sep = b'/'
395 curdir = b'.'
396 pardir = b'..'
397 getcwd = os.getcwdb
398 else:
399 sep = '/'
400 curdir = '.'
401 pardir = '..'
402 getcwd = os.getcwd
403 if strict is ALLOW_MISSING:
404 ignored_error = FileNotFoundError
405 elif strict is ALL_BUT_LAST:
406 ignored_error = FileNotFoundError
407 elif strict:
408 ignored_error = ()
409 else:
410 ignored_error = OSError
411
412 lstat = os.lstat
413 readlink = os.readlink
414 maxlinks = None
415
416 # The stack of unresolved path parts. When popped, a special value of None
417 # indicates that a symlink target has been resolved, and that the original
418 # symlink path can be retrieved by popping again. The [::-1] slice is a
419 # very fast way of spelling list(reversed(...)).
420 rest = filename.rstrip(sep).split(sep)[::-1]
421
422 # Number of unprocessed parts in 'rest'. This can differ from len(rest)
423 # later, because 'rest' might contain markers for unresolved symlinks.
424 part_count = len(rest)
425
426 # The resolved path, which is absolute throughout this function.
427 # Note: getcwd() returns a normalized and symlink-free path.
428 path = sep if filename.startswith(sep) else getcwd()
429 trailing_sep = filename.endswith(sep)
430
431 # Mapping from symlink paths to *fully resolved* symlink targets. If a
432 # symlink is encountered but not yet resolved, the value is None. This is
433 # used both to detect symlink loops and to speed up repeated traversals of
434 # the same links.
435 seen = {}
436
437 # Number of symlinks traversed. When the number of traversals is limited
438 # by *maxlinks*, this is used instead of *seen* to detect symlink loops.
439 link_count = 0
440
441 while part_count:
442 name = rest.pop()
443 if name is None:
444 # resolved symlink target
445 seen[rest.pop()] = path
446 continue

Calls 8

splitMethod · 0.45
rstripMethod · 0.45
startswithMethod · 0.45
endswithMethod · 0.45
popMethod · 0.45
rindexMethod · 0.45
appendMethod · 0.45
extendMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…