@@ -86,22 +86,20 @@ def parse_requirements(
8686 isolated : bool = False ,
8787 comes_from_stdin : bool = False ,
8888) -> Iterator [InstallRequirement ]:
89- # the `comes_from` data will be rewritten based on a number of conditions
90- #
91- # None do not rewrite
92- # callable programmatic rewrite
93- # str fixed rewrite
94- rewrite_comes_from : str | Callable [[str ], str ] | None
89+ # the `comes_from` data will be rewritten in different ways in different conditions
90+ # each rewrite rule is expressible as a str->str function or as a static replacement
91+ rewrite_comes_from : Callable [[str ], str ] | str
9592
9693 if comes_from_stdin :
9794 # if data is coming from stdin, then `comes_from="-r -"`
9895 rewrite_comes_from = "-r -"
9996 elif pathlib .Path (filename ).is_absolute ():
100- rewrite_comes_from = None
97+ # if the input path is absolute, just normalize paths to posix-style
98+ rewrite_comes_from = _normalize_comes_from_location
10199 else :
102100 # if the input was a relative path, set the rewrite rule to rewrite
103101 # absolute paths to be relative
104- rewrite_comes_from = _rewrite_absolute_comes_from_location
102+ rewrite_comes_from = _relativize_comes_from_location
105103
106104 for parsed_req in _parse_requirements (
107105 filename , session , finder = finder , options = options , constraint = constraint
@@ -114,14 +112,16 @@ def parse_requirements(
114112 file_link ._url = parsed_req .requirement
115113 install_req .link = file_link
116114 install_req = copy_install_requirement (install_req )
115+
117116 if isinstance (rewrite_comes_from , str ):
118117 install_req .comes_from = rewrite_comes_from
119- elif rewrite_comes_from is not None :
118+ else :
120119 install_req .comes_from = rewrite_comes_from (install_req .comes_from )
120+
121121 yield install_req
122122
123123
124- def _rewrite_absolute_comes_from_location (original_comes_from : str , / ) -> str :
124+ def _relativize_comes_from_location (original_comes_from : str , / ) -> str :
125125 """
126126 This is the rewrite rule used when ``-r`` or ``-c`` appears in
127127 ``comes_from`` data with an absolute path.
@@ -138,15 +138,34 @@ def _rewrite_absolute_comes_from_location(original_comes_from: str, /) -> str:
138138
139139 file_path = pathlib .Path (suffix )
140140
141- # if the path was not absolute, bail out
141+ # if the path was not absolute, normalize to posix-style and finish processing
142142 if not file_path .is_absolute ():
143- return original_comes_from
143+ return f" { prefix } { file_path . as_posix () } "
144144
145145 # make it relative to the current working dir
146146 suffix = file_path .relative_to (pathlib .Path .cwd ()).as_posix ()
147147 return f"{ prefix } { suffix } "
148148
149149
150+ def _normalize_comes_from_location (original_comes_from : str , / ) -> str :
151+ """
152+ This is the rewrite rule when ``-r`` or ``-c`` appears in ``comes_from``
153+ data and the input path was absolute, meaning we should not relativize the locations.
154+
155+ Instead, we apply minimal normalization, ensuring that posix-style paths are used.
156+ """
157+ # require `-r` or `-c` as the source
158+ if not original_comes_from .startswith (("-r " , "-c " )):
159+ return original_comes_from
160+
161+ # split on the space
162+ prefix , _ , suffix = original_comes_from .partition (" " )
163+
164+ # convert to a posix-style path
165+ suffix = pathlib .Path (suffix ).as_posix ()
166+ return f"{ prefix } { suffix } "
167+
168+
150169def create_wheel_cache (cache_dir : str , format_control : str | None = None ) -> WheelCache :
151170 kwargs : dict [str , str | None ] = {"cache_dir" : cache_dir }
152171 if PIP_VERSION [:2 ] <= (23 , 0 ):
0 commit comments