Skip to content

Commit 0e38993

Browse files
Refactor shared control-character sanitization helper
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent bf86f77 commit 0e38993

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

hyperbrowser/client/file_utils.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@
1010
_DEFAULT_OPEN_ERROR_MESSAGE_PREFIX = "Failed to open file at path"
1111

1212

13+
def _sanitize_control_characters(value: str) -> str:
14+
return "".join(
15+
"?" if ord(character) < 32 or ord(character) == 127 else character
16+
for character in value
17+
)
18+
19+
1320
def _normalize_error_prefix(prefix: object, *, default_prefix: str) -> str:
1421
normalized_default_prefix = default_prefix
1522
if not is_plain_string(normalized_default_prefix):
1623
normalized_default_prefix = _DEFAULT_OPEN_ERROR_MESSAGE_PREFIX
1724
else:
1825
try:
19-
sanitized_default_prefix = "".join(
20-
"?" if ord(character) < 32 or ord(character) == 127 else character
21-
for character in normalized_default_prefix
26+
sanitized_default_prefix = _sanitize_control_characters(
27+
normalized_default_prefix
2228
)
2329
stripped_default_prefix = sanitized_default_prefix.strip()
2430
except Exception:
@@ -30,10 +36,7 @@ def _normalize_error_prefix(prefix: object, *, default_prefix: str) -> str:
3036
if not is_plain_string(prefix):
3137
return normalized_default_prefix
3238
try:
33-
sanitized_prefix = "".join(
34-
"?" if ord(character) < 32 or ord(character) == 127 else character
35-
for character in prefix
36-
)
39+
sanitized_prefix = _sanitize_control_characters(prefix)
3740
stripped_prefix = sanitized_prefix.strip()
3841
except Exception:
3942
stripped_prefix = normalized_default_prefix
@@ -60,10 +63,7 @@ def format_file_path_for_error(
6063
if not is_plain_string(path_value):
6164
return "<provided path>"
6265
try:
63-
sanitized_path = "".join(
64-
"?" if ord(character) < 32 or ord(character) == 127 else character
65-
for character in path_value
66-
)
66+
sanitized_path = _sanitize_control_characters(path_value)
6767
except Exception:
6868
return "<provided path>"
6969
if not is_plain_string(sanitized_path):

0 commit comments

Comments
 (0)