Skip to content

Commit b16c1de

Browse files
committed
Re-write _make_relative to rely on pathlib for cross-platform and cross-python compatibility.
1 parent 1400152 commit b16c1de

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

distutils/ccompiler.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
for the Distutils compiler abstraction model."""
55

66
import os
7+
import pathlib
78
import re
89
import sys
910
import types
@@ -976,31 +977,20 @@ def _make_out_path(self, output_dir, strip_dir, src_name):
976977
@classmethod
977978
def _make_out_path_exts(cls, output_dir, strip_dir, src_name, extensions):
978979
base, ext = os.path.splitext(src_name)
980+
base = pathlib.PurePath(base)
981+
# Ensure base is relative to honor output_dir (python/cpython#37775).
979982
base = cls._make_relative(base)
980983
try:
981984
new_ext = extensions[ext]
982985
except LookupError:
983986
raise UnknownFileError(f"unknown file type '{ext}' (from '{src_name}')")
984987
if strip_dir:
985-
base = os.path.basename(base)
986-
return os.path.join(output_dir, base + new_ext)
988+
base = base.name
989+
return os.path.join(output_dir, base.with_suffix(new_ext))
987990

988991
@staticmethod
989-
def _make_relative(base):
990-
"""
991-
In order to ensure that a filename always honors the
992-
indicated output_dir, make sure it's relative.
993-
Ref python/cpython#37775.
994-
"""
995-
# Chop off the drive
996-
no_drive = os.path.splitdrive(base)[1]
997-
# If abs, chop off leading /
998-
is_abs = (
999-
os.path.isabs(no_drive)
1000-
or sys.platform == 'win32'
1001-
and no_drive.startswith(('/', "\\"))
1002-
)
1003-
return no_drive[is_abs:]
992+
def _make_relative(base: pathlib.Path):
993+
return base.relative_to(base.anchor)
1004994

1005995
def shared_object_filename(self, basename, strip_dir=False, output_dir=''):
1006996
assert output_dir is not None

0 commit comments

Comments
 (0)