test_saferepr.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import pytest
  4. import re
  5. from case import skip
  6. from decimal import Decimal
  7. from pprint import pprint
  8. from celery.five import (
  9. items, long_t, python_2_unicode_compatible, text_t, values,
  10. )
  11. from celery.utils.saferepr import saferepr
  12. D_NUMBERS = {
  13. b'integer': 1,
  14. b'float': 1.3,
  15. b'decimal': Decimal('1.3'),
  16. b'long': long_t(4),
  17. b'complex': complex(13.3),
  18. }
  19. D_INT_KEYS = {v: k for k, v in items(D_NUMBERS)}
  20. QUICK_BROWN_FOX = 'The quick brown fox jumps over the lazy dog.'
  21. B_QUICK_BROWN_FOX = b'The quick brown fox jumps over the lazy dog.'
  22. D_TEXT = {
  23. b'foo': QUICK_BROWN_FOX,
  24. b'bar': B_QUICK_BROWN_FOX,
  25. b'baz': B_QUICK_BROWN_FOX,
  26. b'xuzzy': B_QUICK_BROWN_FOX,
  27. }
  28. L_NUMBERS = list(values(D_NUMBERS))
  29. D_TEXT_LARGE = {
  30. b'bazxuzzyfoobarlongverylonglong': QUICK_BROWN_FOX * 30,
  31. }
  32. D_ALL = {
  33. b'numbers': D_NUMBERS,
  34. b'intkeys': D_INT_KEYS,
  35. b'text': D_TEXT,
  36. b'largetext': D_TEXT_LARGE,
  37. }
  38. D_D_TEXT = {b'rest': D_TEXT}
  39. RE_OLD_SET_REPR = re.compile(r'(?<!frozen)set\([\[|\{](.+?)[\}\]]\)')
  40. RE_OLD_SET_REPR_REPLACE = r'{\1}'
  41. RE_OLD_SET_CUSTOM_REPR = re.compile(r'((?:frozen)?set\d?\()\[(.+?)\](\))')
  42. RE_OLD_SET_CUSTOM_REPR_REPLACE = r'\1{\2}\3'
  43. RE_EMPTY_SET_REPR = re.compile(r'((?:frozen)?set\d?)\(\[\]\)')
  44. RE_EMPTY_SET_REPR_REPLACE = r'\1()'
  45. RE_LONG_SUFFIX = re.compile(r'(\d)+L')
  46. def old_repr(s):
  47. return text_t(RE_LONG_SUFFIX.sub(
  48. r'\1',
  49. RE_EMPTY_SET_REPR.sub(
  50. RE_EMPTY_SET_REPR_REPLACE,
  51. RE_OLD_SET_REPR.sub(
  52. RE_OLD_SET_REPR_REPLACE,
  53. RE_OLD_SET_CUSTOM_REPR.sub(
  54. RE_OLD_SET_CUSTOM_REPR_REPLACE, repr(s).replace("u'", "'"),
  55. )
  56. ),
  57. ),
  58. )).replace('set([])', 'set()')
  59. class list2(list):
  60. pass
  61. @python_2_unicode_compatible
  62. class list3(list):
  63. def __repr__(self):
  64. return list.__repr__(self)
  65. class tuple2(tuple):
  66. pass
  67. @python_2_unicode_compatible
  68. class tuple3(tuple):
  69. def __repr__(self):
  70. return tuple.__repr__(self)
  71. class set2(set):
  72. pass
  73. @python_2_unicode_compatible
  74. class set3(set):
  75. def __repr__(self):
  76. return set.__repr__(self)
  77. class frozenset2(frozenset):
  78. pass
  79. @python_2_unicode_compatible
  80. class frozenset3(frozenset):
  81. def __repr__(self):
  82. return frozenset.__repr__(self)
  83. class dict2(dict):
  84. pass
  85. @python_2_unicode_compatible
  86. class dict3(dict):
  87. def __repr__(self):
  88. return dict.__repr__(self)
  89. class test_saferepr:
  90. @pytest.mark.parametrize('value', list(values(D_NUMBERS)))
  91. def test_safe_types(self, value):
  92. assert saferepr(value) == old_repr(value)
  93. def test_numbers_dict(self):
  94. assert saferepr(D_NUMBERS) == old_repr(D_NUMBERS)
  95. def test_numbers_list(self):
  96. assert saferepr(L_NUMBERS) == old_repr(L_NUMBERS)
  97. def test_numbers_keys(self):
  98. assert saferepr(D_INT_KEYS) == old_repr(D_INT_KEYS)
  99. def test_text(self):
  100. assert saferepr(D_TEXT) == old_repr(D_TEXT).replace("u'", "'")
  101. def test_text_maxlen(self):
  102. assert saferepr(D_D_TEXT, 100).endswith("...', ...}}")
  103. def test_maxlevels(self):
  104. saferepr(D_ALL, maxlevels=1)
  105. def test_recursion(self):
  106. d = {1: 2, 3: {4: 5}}
  107. d[3][6] = d
  108. res = saferepr(d)
  109. assert 'Recursion on' in res
  110. @pytest.mark.parametrize('value', [
  111. 0, 0, 0 + 0j, 0.0, '', b'',
  112. (), tuple2(), tuple3(),
  113. [], list2(), list3(),
  114. set(), set2(), set3(),
  115. frozenset(), frozenset2(), frozenset3(),
  116. {}, dict2(), dict3(),
  117. test_recursion, pprint,
  118. -6, -6, -6 - 6j, -1.5, 'x', b'x', (3,), [3], {3: 6},
  119. (1, 2), [3, 4], {5: 6},
  120. tuple2((1, 2)), tuple3((1, 2)), tuple3(range(100)),
  121. [3, 4], list2([3, 4]), list3([3, 4]), list3(range(100)),
  122. {7}, set2({7}), set3({7}),
  123. frozenset({8}), frozenset2({8}), frozenset3({8}),
  124. dict2({5: 6}), dict3({5: 6}),
  125. range(10, -11, -1)
  126. ])
  127. def test_same_as_repr(self, value):
  128. # Simple objects, small containers, and classes that overwrite __repr__
  129. # For those the result should be the same as repr().
  130. # Ahem. The docs don't say anything about that -- this appears to
  131. # be testing an implementation quirk. Starting in Python 2.5, it's
  132. # not true for dicts: pprint always sorts dicts by key now; before,
  133. # it sorted a dict display if and only if the display required
  134. # multiple lines. For that reason, dicts with more than one element
  135. # aren't tested here.
  136. native = old_repr(value)
  137. assert saferepr(value) == native
  138. @skip.if_python3()
  139. def test_bytes_with_unicode(self):
  140. class X(object):
  141. def __repr__(self):
  142. return 'æ e i a æ å'.encode(
  143. 'utf-8', errors='backslash replace')
  144. val = X()
  145. assert repr(val)
  146. assert saferepr(val)