Skip to content

os.path.relpath() needlessly calls abspath() when given two paths with matching anchors #99199

Closed
@barneygale

Description

@barneygale

relpath() unconditionally calls abspath() to make both its arguments absolute before comparing them. This is necessary if the supplied arguments have different anchors, but it is not necessary when the paths' anchors match. We can save a call to getcwd() / _getfinalpathname() by skipping the abspath() call in this case. This should improve performance a little.

For example, neither of the following should require a call to abspath() internally:

relpath('foo/bar', 'baz/ding')
relpath('/etc/hosts', '/usr/local/bin')

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions