Skip to content

Commit c80344c

Browse files
committed
Fix `--relative-paths
* KeyError is not raised for defaultdict * Fix relative paths on different drives on Windows
1 parent dbe1cbd commit c80344c

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

Tools/build/generate-build-details.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def generate_data(schema_version: str) -> collections.defaultdict[str, Any]:
6161
if '_multiarch' in data['implementation']:
6262
data['implementation']['_multiarch'] = sysconfig.get_config_var('MULTIARCH')
6363

64-
if sys.platform != 'win32':
64+
if os.name != 'nt':
6565
data['abi']['flags'] = list(sys.abiflags)
6666
else:
6767
data['abi']['flags'] = []
@@ -107,7 +107,7 @@ def generate_data(schema_version: str) -> collections.defaultdict[str, Any]:
107107
data['abi']['extension_suffix'] = sysconfig.get_config_var('EXT_SUFFIX')
108108

109109
# EXTENSION_SUFFIXES has been constant for a long time, and currently we
110-
# don't have a better information source to find the stable ABI suffix.
110+
# don't have a better information source to find the stable ABI suffix.
111111
for suffix in importlib.machinery.EXTENSION_SUFFIXES:
112112
if suffix.startswith('.abi'):
113113
data['abi']['stable_abi_suffix'] = suffix
@@ -136,33 +136,51 @@ def generate_data(schema_version: str) -> collections.defaultdict[str, Any]:
136136
def make_paths_relative(data: dict[str, Any], config_path: str | None = None) -> None:
137137
# Make base_prefix relative to the config_path directory
138138
if config_path:
139-
data['base_prefix'] = os.path.relpath(data['base_prefix'], os.path.dirname(config_path))
139+
data['base_prefix'] = relative_path(data['base_prefix'],
140+
os.path.dirname(config_path))
141+
base_prefix = data['base_prefix']
142+
140143
# Update path values to make them relative to base_prefix
141-
PATH_KEYS = [
144+
PATH_KEYS = (
142145
'base_interpreter',
143146
'libpython.dynamic',
144147
'libpython.dynamic_stableabi',
145148
'libpython.static',
146149
'c_api.headers',
147150
'c_api.pkgconfig_path',
148-
]
151+
)
149152
for entry in PATH_KEYS:
150-
parent, _, child = entry.rpartition('.')
153+
*parents, child = entry.split('.')
151154
# Get the key container object
152155
try:
153156
container = data
154-
for part in parent.split('.'):
157+
for part in parents:
155158
container = container[part]
159+
if child not in container:
160+
raise KeyError
156161
current_path = container[child]
157162
except KeyError:
158163
continue
159164
# Get the relative path
160-
new_path = os.path.relpath(current_path, data['base_prefix'])
165+
new_path = relative_path(current_path, base_prefix)
161166
# Join '.' so that the path is formated as './path' instead of 'path'
162167
new_path = os.path.join('.', new_path)
163168
container[child] = new_path
164169

165170

171+
def relative_path(path: str, base: str) -> str:
172+
if os.name != 'nt':
173+
return os.path.relpath(path, base)
174+
175+
# There are no relative paths between drives on Windows.
176+
path_drv, path_root, _ = os.path.splitroot(path)
177+
base_drv, base_root, _ = os.path.splitroot(base)
178+
if path_drv.lower() == base_drv.lower() and path_root == base_root:
179+
return os.path.relpath(path, base)
180+
181+
return path
182+
183+
166184
def main() -> None:
167185
parser = argparse.ArgumentParser(exit_on_error=False)
168186
parser.add_argument('location')

0 commit comments

Comments
 (0)