Skip to content

Commit f516f94

Browse files
committed
Fix old python
1 parent 1216de5 commit f516f94

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

src/semiwrap/autowrap/cxxparser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
TrampolineData,
8989
)
9090

91+
from ..util import relpath_walk_up
92+
9193

9294
class HasSubpackage(Protocol):
9395
@property
@@ -2004,7 +2006,7 @@ def parse_header(
20042006
extra_includes_first=user_cfg.extra_includes_first,
20052007
extra_includes=user_cfg.extra_includes,
20062008
inline_code=user_cfg.inline_code,
2007-
rel_fname=header_path.relative_to(header_root, walk_up=True).as_posix(),
2009+
rel_fname=relpath_walk_up(header_path, header_root).as_posix(),
20082010
)
20092011

20102012
# Parse the header using a custom visitor

src/semiwrap/cmd_genmeson.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
LocalDependency,
2121
makeplan,
2222
)
23-
from .util import maybe_write_file
23+
from .util import maybe_write_file, relpath_walk_up
2424

2525
# String escaping stolen from meson source code, Apache 2.0 license
2626
# This is the regex for the supported escape sequences of a regular string
@@ -150,12 +150,8 @@ def _render_include_directories(
150150
# meson wants these to be relative to meson.build
151151
# - only can do that if we're writing an output file
152152
if meson_build_path:
153-
meson_build_parent = str(meson_build_path.parent)
154-
155-
# walk_up=True was introduced in Python 3.12
156-
# incs = [p.relative_to(meson_build_parent, walk_up=True) for p in incs]
157-
158-
incs = [pathlib.Path(os.path.relpath(str(p), meson_build_parent)) for p in incs]
153+
meson_build_parent = meson_build_path.parent
154+
incs = [relpath_walk_up(p, meson_build_parent) for p in incs]
159155

160156
_render_meson_args(
161157
r, "include_directories", [_make_string(inc.as_posix()) for inc in incs]

src/semiwrap/makeplan.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .config.pyproject_toml import ExtensionModuleConfig, TypeCasterConfig
1515
from .pkgconf_cache import PkgconfCache
1616
from .pyproject import PyProject
17+
from .util import relpath_walk_up
1718

1819
import toposort
1920

@@ -608,7 +609,7 @@ def _locate_header(self, hdr: str, search_path: T.List[pathlib.Path]):
608609
for p in search_path:
609610
h_path = p / phdr
610611
if h_path.exists():
611-
return InputFile(h_path.relative_to(self.project_root, walk_up=True)), p
612+
return InputFile(relpath_walk_up(h_path, self.project_root)), p
612613
raise FileNotFoundError(
613614
f"cannot locate {phdr} in {', '.join(map(str, search_path))}"
614615
)

src/semiwrap/mkpc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import typing as T
33

44
from .pkgconf_cache import INITPY_VARNAME
5+
from .util import relpath_walk_up
56

67

78
def make_pc_file(
@@ -29,7 +30,7 @@ def make_pc_file(
2930

3031
for i, inc in enumerate(includes):
3132
includedir = project_root / pathlib.PurePosixPath(inc)
32-
rel = includedir.relative_to(pc_install_path, walk_up=True)
33+
rel = relpath_walk_up(includedir, pc_install_path)
3334
pc_content.append(f"inc{i}=${{prefix}}/{rel.as_posix()}")
3435
cflags.append(f"-I${{inc{i}}}")
3536

src/semiwrap/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os.path
12
import pathlib
23
import typing as T
34

@@ -18,3 +19,9 @@ def maybe_write_file(
1819
fp.write(content)
1920

2021
return True
22+
23+
24+
def relpath_walk_up(p: pathlib.Path, other: pathlib.Path) -> pathlib.Path:
25+
# walk_up=True was introduced in Python 3.12 so can't use that
26+
# p.relative_to(other, walk_up=True)
27+
return pathlib.Path(os.path.relpath(p, other))

0 commit comments

Comments
 (0)