Skip to content

Commit ebfebc0

Browse files
authored
Merge pull request #302 from adang1345/fix-python313-abs
Fix issue with absolute path with Python 3.13 on Windows
2 parents c97a3db + 36ce8b3 commit ebfebc0

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def monkeysession(request):
9292
mpatch.undo()
9393

9494

95-
@pytest.fixture(autouse=True, scope="session")
95+
@pytest.fixture(scope="module")
9696
def suppress_path_mangle(monkeysession):
9797
"""
9898
Disable the path mangling in CCompiler. Workaround for #169.

distutils/ccompiler.py

Lines changed: 23 additions & 16 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
@@ -969,27 +970,33 @@ def out_extensions(self):
969970
return dict.fromkeys(self.src_extensions, self.obj_extension)
970971

971972
def _make_out_path(self, output_dir, strip_dir, src_name):
972-
base, ext = os.path.splitext(src_name)
973-
base = self._make_relative(base)
973+
return self._make_out_path_exts(
974+
output_dir, strip_dir, src_name, self.out_extensions
975+
)
976+
977+
@classmethod
978+
def _make_out_path_exts(cls, output_dir, strip_dir, src_name, extensions):
979+
r"""
980+
>>> exts = {'.c': '.o'}
981+
>>> CCompiler._make_out_path_exts('.', False, '/foo/bar.c', exts).replace('\\', '/')
982+
'./foo/bar.o'
983+
>>> CCompiler._make_out_path_exts('.', True, '/foo/bar.c', exts).replace('\\', '/')
984+
'./bar.o'
985+
"""
986+
src = pathlib.PurePath(src_name)
987+
# Ensure base is relative to honor output_dir (python/cpython#37775).
988+
base = cls._make_relative(src)
974989
try:
975-
new_ext = self.out_extensions[ext]
990+
new_ext = extensions[src.suffix]
976991
except LookupError:
977-
raise UnknownFileError(f"unknown file type '{ext}' (from '{src_name}')")
992+
raise UnknownFileError(f"unknown file type '{src.suffix}' (from '{src}')")
978993
if strip_dir:
979-
base = os.path.basename(base)
980-
return os.path.join(output_dir, base + new_ext)
994+
base = pathlib.PurePath(base.name)
995+
return os.path.join(output_dir, base.with_suffix(new_ext))
981996

982997
@staticmethod
983-
def _make_relative(base):
984-
"""
985-
In order to ensure that a filename always honors the
986-
indicated output_dir, make sure it's relative.
987-
Ref python/cpython#37775.
988-
"""
989-
# Chop off the drive
990-
no_drive = os.path.splitdrive(base)[1]
991-
# If abs, chop off leading /
992-
return no_drive[os.path.isabs(no_drive) :]
998+
def _make_relative(base: pathlib.Path):
999+
return base.relative_to(base.anchor)
9931000

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

distutils/tests/test_ccompiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import pytest
99

10+
pytestmark = pytest.mark.usefixtures('suppress_path_mangle')
11+
1012

1113
def _make_strs(paths):
1214
"""

0 commit comments

Comments
 (0)