From 6fc7d3bd60716b1c3f165513b26168046aecd120 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Fri, 21 Nov 2025 14:30:05 +0100 Subject: [PATCH] Fix python 3.13 compatibility for ResourcePath This commit replaces `_file_relative_to` which uses some private pathlib internals, by a normal `path.relative_to()` lookup to convert file paths to resource paths. This is expected to work on all platforms (py33 ... py313) the same way. --- st3/sublime_lib/resource_path.py | 39 ++++++-------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/st3/sublime_lib/resource_path.py b/st3/sublime_lib/resource_path.py index f77ed4d..76f9f54 100644 --- a/st3/sublime_lib/resource_path.py +++ b/st3/sublime_lib/resource_path.py @@ -13,33 +13,6 @@ __all__ = ['ResourcePath'] -def _abs_parts(path: Path) -> Tuple[str, ...]: - return (path.drive, path.root) + path.parts[1:] - - -def _file_relative_to(path: Path, base: Path) -> Optional[Tuple[str, ...]]: - """ - Like Path.relative_to, except: - - - Both paths must be relative. - - `base` must be a single Path object. - - The error message is blank. - - Only a tuple of parts is returned. - - Surprisingly, this is much, much faster. - """ - child_parts = _abs_parts(path) - base_parts = _abs_parts(base) - - n = len(base_parts) - cf = path._flavour.casefold_parts # type: ignore - - if cf(child_parts[:n]) != cf(base_parts): - return None - - return child_parts[n:] - - class ResourceRoot(metaclass=ABCMeta): """ Represents a directory containing packages. @@ -76,13 +49,15 @@ def file_to_resource_path(self, file_path: Union[Path, str]) -> Optional['Resour if not file_path.is_absolute(): raise ValueError("Cannot convert a relative file path to a resource path.") - parts = _file_relative_to(file_path, self.file_root) - if parts is None: + try: + relpath = file_path.relative_to(self.file_root) + except ValueError: return None - elif parts == (): + + if not relpath.parts: return self.resource_root - else: - return self._package_resource_path(*parts) + + return self._package_resource_path(*relpath.parts) @abstractmethod def _package_file_path(