@@ -160,7 +160,12 @@ def urlparse(url: str = "", **kwargs: str | None) -> ParseResult:
160
160
# If a URL includes any ASCII control characters including \t, \r, \n,
161
161
# then treat it as invalid.
162
162
if any (char .isascii () and not char .isprintable () for char in url ):
163
- raise InvalidURL ("Invalid non-printable ASCII character in URL" )
163
+ char = next (char for char in url if char .isascii () and not char .isprintable ())
164
+ idx = url .find (char )
165
+ error = (
166
+ f"Invalid non-printable ASCII character in URL, { char !r} at position { idx } ."
167
+ )
168
+ raise InvalidURL (error )
164
169
165
170
# Some keyword arguments require special handling.
166
171
# ------------------------------------------------
@@ -205,9 +210,15 @@ def urlparse(url: str = "", **kwargs: str | None) -> ParseResult:
205
210
# If a component includes any ASCII control characters including \t, \r, \n,
206
211
# then treat it as invalid.
207
212
if any (char .isascii () and not char .isprintable () for char in value ):
208
- raise InvalidURL (
209
- f"Invalid non-printable ASCII character in URL component '{ key } '"
213
+ char = next (
214
+ char for char in value if char .isascii () and not char .isprintable ()
215
+ )
216
+ idx = value .find (char )
217
+ error = (
218
+ f"Invalid non-printable ASCII character in URL { key } component, "
219
+ f"{ char !r} at position { idx } ."
210
220
)
221
+ raise InvalidURL (error )
211
222
212
223
# Ensure that keyword arguments match as a valid regex.
213
224
if not COMPONENT_REGEX [key ].fullmatch (value ):
0 commit comments