Skip to content

Commit 7a9b731

Browse files
committed
Get rid of python 2.7 traces
No manual magic with pseudo-repr for strings anymore
1 parent 45c77c7 commit 7a9b731

File tree

6 files changed

+25
-89
lines changed

6 files changed

+25
-89
lines changed

logwrap/repr_utils.pxd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ cdef:
6363

6464
cpdef str process_element(self, src: typing.Any, unsigned long indent=?, bint no_indent_start=?)
6565

66-
class PrettyRepr(PrettyFormat):
67-
cdef str _strings_repr(self, unsigned long indent, val: typing.Union[bytes, str])
68-
6966
class PrettyStr(PrettyFormat):
7067
cdef str _strings_str(self, unsigned long indent, val: typing.Union[bytes, str])
7168

logwrap/repr_utils.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,6 @@ def _simple(item: typing.Any) -> bool:
3939
return not isinstance(item, (list, set, tuple, dict, frozenset))
4040

4141

42-
SPECIAL_SYMBOLS_ESCAPE: typing.Dict[str, str] = {
43-
"\\": "\\\\",
44-
"\n": "\\n",
45-
"\r": "\\r",
46-
"\f": "\\f",
47-
"\v": "\\v",
48-
"\b": "\\b",
49-
"\t": "\\t",
50-
"\a": "\\a",
51-
}
52-
53-
5442
class ReprParameter:
5543
"""Parameter wrapper wor repr and str operations over signature."""
5644

@@ -353,18 +341,6 @@ def _magic_method_name(self) -> str:
353341
"""
354342
return "__pretty_repr__"
355343

356-
@staticmethod
357-
def _strings_repr(indent: int, val: typing.Union[bytes, str]) -> str:
358-
"""Custom repr for strings and binary strings."""
359-
if isinstance(val, bytes):
360-
string: str = val.decode(encoding="utf-8", errors="backslashreplace")
361-
prefix: str = "b"
362-
else:
363-
prefix = "u"
364-
string = val
365-
escaped: str = "".join(SPECIAL_SYMBOLS_ESCAPE.get(sym, sym) for sym in string)
366-
return f"{'':<{indent}}{prefix}'''{escaped}'''"
367-
368344
def _repr_simple(self, src: typing.Any, indent: int = 0, no_indent_start: bool = False) -> str:
369345
"""Repr object without iteration.
370346
@@ -380,8 +356,6 @@ def _repr_simple(self, src: typing.Any, indent: int = 0, no_indent_start: bool =
380356
indent = 0 if no_indent_start else indent
381357
if isinstance(src, set):
382358
return f"{'':<{indent}}set({' ,'.join(map(repr, src))})"
383-
if isinstance(src, (bytes, str)):
384-
return self._strings_repr(indent=indent, val=src)
385359
return f"{'':<{indent}}{src!r}"
386360

387361
def _repr_dict_items(self, src: typing.Dict[typing.Any, typing.Any], indent: int = 0) -> typing.Iterator[str]:

logwrap/repr_utils.pyx

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ cdef:
3535
"""Check for nested iterations: True, if not."""
3636
return not isinstance(item, (list, set, tuple, dict, frozenset))
3737

38-
dict SPECIAL_SYMBOLS_ESCAPE = {
39-
"\\": "\\\\",
40-
"\n": "\\n",
41-
"\r": "\\r",
42-
"\f": "\\f",
43-
"\v": "\\v",
44-
"\b": "\\b",
45-
"\t": "\\t",
46-
"\a": "\\a"
47-
}
48-
4938

5039
class ReprParameter:
5140
"""Parameter wrapper wor repr and str operations over signature."""
@@ -333,26 +322,6 @@ cdef class PrettyRepr(PrettyFormat):
333322
self._magic_method_name = "__pretty_repr__"
334323

335324
cdef:
336-
str _strings_repr(
337-
self,
338-
unsigned long indent,
339-
val: typing.Union[bytes, str]
340-
):
341-
"""Custom repr for strings and binary strings."""
342-
cdef:
343-
str prefix
344-
str string
345-
str escaped
346-
347-
if isinstance(val, bytes):
348-
string = val.decode(encoding="utf-8", errors="backslashreplace")
349-
prefix = "b"
350-
else:
351-
prefix = "u"
352-
string = val
353-
escaped = "".join(SPECIAL_SYMBOLS_ESCAPE.get(sym, sym) for sym in string)
354-
return f"{'':<{indent}}{prefix}'''{escaped}'''"
355-
356325
str _repr_simple(
357326
self,
358327
src: typing.Any,
@@ -373,8 +342,6 @@ cdef class PrettyRepr(PrettyFormat):
373342
cdef unsigned long real_indent = 0 if no_indent_start else indent
374343
if isinstance(src, set):
375344
return f"{'':<{real_indent}}set({' ,'.join(map(repr, src))})"
376-
if isinstance(src, (bytes, str)):
377-
return self._strings_repr(indent=real_indent, val=src)
378345
return f"{'':<{real_indent}}{src!r}"
379346

380347
str _repr_callable(

test/test_log_wrap.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,16 +767,16 @@ def func(arg, arg_secret, arg2='public', secret_arg=('key')):
767767
msg="Calling: \n"
768768
"'func'(\n"
769769
" # POSITIONAL_OR_KEYWORD:\n"
770-
" 'arg'=u'''data''',\n"
770+
" 'arg'='data',\n"
771771
" 'arg_secret'=None,\n"
772-
" 'arg2'=u'''public''',\n"
772+
" 'arg2'='public',\n"
773773
" 'secret_arg'=None,\n"
774774
")"),
775775
mock.call.log(level=logging.DEBUG, msg="Done: 'func'")
776776
]
777777
)
778778

779-
def test_003_override_change_repr(self):
779+
def test_004_override_change_repr(self):
780780
class ChangeRepr(logwrap.LogWrap):
781781
def post_process_param(
782782
self,
@@ -804,9 +804,9 @@ def func(arg, arg_secret, arg2='public', secret_arg=('key')):
804804
msg="Calling: \n"
805805
"'func'(\n"
806806
" # POSITIONAL_OR_KEYWORD:\n"
807-
" 'arg'=u'''data''',\n"
807+
" 'arg'='data',\n"
808808
" 'arg_secret'=<*hidden*>,\n"
809-
" 'arg2'=u'''public''',\n"
809+
" 'arg2'='public',\n"
810810
" 'secret_arg'=<*hidden*>,\n"
811811
")"),
812812
mock.call.log(level=logging.DEBUG, msg="Done: 'func'")

test/test_repr_utils.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@
2828

2929
# noinspection PyUnusedLocal,PyMissingOrEmptyDocstring
3030
class TestPrettyRepr(unittest.TestCase):
31-
def test_simple(self):
31+
def test_001_simple(self):
3232
self.assertEqual(
3333
logwrap.pretty_repr(True), repr(True)
3434
)
3535

36-
def test_text(self):
36+
def test_002_text(self):
37+
txt = 'Unicode text'
38+
b_txt = b'bytes text\x01'
3739
self.assertEqual(
38-
logwrap.pretty_repr('Unicode text'), "u'''Unicode text'''"
40+
repr(txt), logwrap.pretty_repr(txt)
3941
)
4042
self.assertEqual(
41-
logwrap.pretty_repr(b'bytes text\x01'), "b'''bytes text\x01'''"
43+
repr(b_txt), logwrap.pretty_repr(b_txt)
4244
)
4345

44-
def test_iterable(self):
46+
def test_003_iterable(self):
4547
self.assertEqual(
4648
'list([{nl:<5}1,{nl:<5}2,{nl:<5}3,\n'
4749
'])'.format(nl='\n'),
@@ -61,7 +63,7 @@ def test_iterable(self):
6163
res.startswith('frozenset({') and res.endswith('\n})')
6264
)
6365

64-
def test_dict(self):
66+
def test_004_dict(self):
6567
self.assertEqual(
6668
'dict({\n'
6769
' 1 : 1,\n'
@@ -71,7 +73,7 @@ def test_dict(self):
7173
logwrap.pretty_repr({1: 1, 2: 2, 33: 33}),
7274
)
7375

74-
def test_nested_obj(self):
76+
def test_005_nested_obj(self):
7577
test_obj = [
7678
{1: 2},
7779
{3: {4}},
@@ -111,7 +113,7 @@ def test_nested_obj(self):
111113
)
112114
self.assertEqual(exp_repr, logwrap.pretty_repr(test_obj))
113115

114-
def test_callable(self):
116+
def test_006_callable(self):
115117
fmt = "\n{spc:<{indent}}<{obj!r} with interface ({args})>".format
116118

117119
def empty_func():
@@ -218,7 +220,7 @@ def tst_classmethod(cls, arg, darg=1, *positional, **named):
218220
)
219221
)
220222

221-
def test_indent(self):
223+
def test_007_indent(self):
222224
obj = [[[[[[[[[[123]]]]]]]]]]
223225
self.assertEqual(
224226
"list([\n"
@@ -255,7 +257,7 @@ def test_indent(self):
255257
logwrap.pretty_repr(obj, max_indent=10),
256258
)
257259

258-
def test_magic_override(self):
260+
def test_008_magic_override(self):
259261
# noinspection PyMissingOrEmptyDocstring
260262
class Tst(object):
261263
def __repr__(self):
@@ -268,7 +270,7 @@ def __pretty_repr__(
268270
no_indent_start
269271
):
270272
return parser.process_element(
271-
"<Test Class at 0x{:X}>".format(id(self.__class__)),
273+
f"<Test Class at 0x{id(self.__class__):X}>",
272274
indent=indent,
273275
no_indent_start=no_indent_start
274276
)
@@ -280,13 +282,9 @@ def __pretty_repr__(
280282
)
281283
self.assertEqual(
282284
result,
283-
"u'''<Test Class at 0x{:X}>'''".format(id(Tst))
285+
f"'<Test Class at 0x{id(Tst):X}>'"
284286
)
285287

286-
def test_string_escape(self):
287-
src = "n\n b\b r\r f\f v\v t\t \\ a\a"
288-
self.assertEqual("u'''n\\n b\\b r\\r f\\f v\\v t\\t \\\\ a\\a'''", logwrap.pretty_repr(src))
289-
290288

291289
# noinspection PyUnusedLocal,PyMissingOrEmptyDocstring
292290
class TestAnnotated(unittest.TestCase):

test/test_repr_utils_special.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929

3030
# noinspection PyUnusedLocal,PyMissingOrEmptyDocstring
3131
class TestPrettyRepr(unittest.TestCase):
32-
def test_dict_subclass(self):
32+
def test_001_dict_subclass(self):
3333
class MyDict(dict):
3434
"""Dict subclass."""
3535

3636
val = MyDict(key='value')
3737
self.assertEqual(
3838
"MyDict({\n"
39-
" 'key': u'''value''',\n"
39+
" 'key': 'value',\n"
4040
"})",
4141
logwrap.pretty_repr(val)
4242
)
@@ -48,14 +48,14 @@ class MyDict(dict):
4848
logwrap.pretty_str(val)
4949
)
5050

51-
def test_typing_specific_dict(self):
51+
def test_002_typing_specific_dict(self):
5252
class MyDict(typing.Dict[str, str]):
5353
"""Dict subclass."""
5454

5555
val = MyDict(key='value')
5656
self.assertEqual(
5757
"MyDict({\n"
58-
" 'key': u'''value''',\n"
58+
" 'key': 'value',\n"
5959
"})",
6060
logwrap.pretty_repr(val)
6161
)
@@ -68,14 +68,14 @@ class MyDict(typing.Dict[str, str]):
6868
)
6969

7070
@unittest.skipIf(sys.version_info[:2] < (3, 8), 'pep-0589 is implemented in python 3.8')
71-
def test_typed_dict(self):
71+
def test_003_typed_dict(self):
7272
class MyDict(typing.TypedDict):
7373
key: str
7474

7575
val = MyDict(key='value')
7676
self.assertEqual(
7777
"dict({\n"
78-
" 'key': u'''value''',\n"
78+
" 'key': 'value',\n"
7979
"})",
8080
logwrap.pretty_repr(val)
8181
)

0 commit comments

Comments
 (0)