util.py 828 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import pprint
  2. import six
  3. from six import text_type
  4. def utf8_replace(s):
  5. try:
  6. return text_type(s, "utf-8", "replace")
  7. except TypeError:
  8. return s
  9. def direct_type_mismatch(lhs, rhs):
  10. return type(lhs) is not type(rhs)
  11. def display_op_for(pytest_op):
  12. return "==" if pytest_op == "equal" else pytest_op
  13. def possibly_missing_eq(lhs, rhs):
  14. try:
  15. left_dict, right_dict = vars(lhs), vars(rhs)
  16. return (type(lhs) is type(rhs)) and lhs != rhs and left_dict == right_dict
  17. except TypeError:
  18. return False
  19. def has_differing_len(lhs, rhs):
  20. try:
  21. return len(lhs) != len(rhs)
  22. except TypeError:
  23. return False
  24. def pformat_no_color(s, width):
  25. if isinstance(s, six.string_types):
  26. return '"' + s + '"'
  27. return pprint.pformat(s, width=width)