Skip to content

Commit ee20b5c

Browse files
committed
Merge pull request #93 from nowsecure/replacement_character
Use the unicode replacement char for non-printable characters
2 parents 3686a5c + aa2a551 commit ee20b5c

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

datagrid_gtk3/tests/utils/test_stringutils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from datagrid_gtk3.utils.stringutils import (
99
is_printable,
10-
strip_non_printable,
10+
replace_non_printable,
1111
)
1212

1313

@@ -30,15 +30,15 @@ def test_printable(self):
3030
for char in [chr(0), chr(20), chr(30)]:
3131
self.assertFalse(is_printable(char))
3232

33-
def test_strip_non_printable(self):
34-
"""Tests for :func:`datagrid.utils.stringutils.strip_non_printable`."""
33+
def test_replace_non_printable(self):
34+
"""Test :func:`datagrid.utils.stringutils.replace_non_printable`."""
3535
self.assertEqual(
36-
strip_non_printable(
36+
replace_non_printable(
3737
"Some string\nWith\tsome %s non-printable, %s chars" % (
3838
chr(20), chr(30))),
39-
"Some string\nWith\tsome non-printable, chars")
39+
u"Some string\nWith\tsome non-printable, chars")
4040

4141
self.assertEqual(
42-
strip_non_printable(
42+
replace_non_printable(
4343
u"Ração %s para %s búfalos" % (chr(20), chr(30))),
44-
u"Ração para búfalos")
44+
u"Ração para búfalos")

datagrid_gtk3/utils/stringutils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ def is_printable(char):
1414
return (char_code >= 32) or (9 <= char_code <= 13)
1515

1616

17-
def strip_non_printable(string_):
18-
"""Remove non-printable characters from the string.
17+
def replace_non_printable(string_):
18+
"""Replace non-printable characters on the string with a replacement.
1919
20-
:param string_: The string to remove the characters from
20+
Use the unicode replacement character (U+FFFD), instead of the
21+
non-printable ones.
22+
23+
:param string_: The string to replace the characters from
2124
:type string_: str
2225
:rtype: str
2326
"""
24-
return ''.join(c for c in string_ if is_printable(c))
27+
return ''.join(c if is_printable(c) else u"\uFFFD" for c in string_)

datagrid_gtk3/utils/transformations.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,9 @@ def string_transform(value, max_length=None, oneline=True,
9999
raise
100100
value = decode_fallback(value)
101101

102-
# Remove non-printable characters from the string. Only keep those
103-
# considered as whitespace/newline. We cannot use string.printable
104-
# here as it would remove unicode characters too.
105-
value = stringutils.strip_non_printable(value)
102+
# Replace non-printable characters on the string so the user will
103+
# know that there's something there even though it is not printable.
104+
value = stringutils.replace_non_printable(value)
106105

107106
if oneline:
108107
value = u' '.join(v.strip() for v in value.splitlines() if v.strip())

0 commit comments

Comments
 (0)