def string_val_as_escaped_bytes(self): """Get escaped value of the object. Turn the value of a string or bytes object into byte sequence with unknown, control, and \\ characters escaped. This function should be used when looking for a known sequence in a potentially badly encoded string in the code. :return: sequence of printable ascii bytes representing original string """ val = self.string_val if val is not None: # it's any of str or unicode in py2, or str in py3 return val.encode("unicode_escape") val = self.bytes_val if val is not None: return utils.escaped_bytes_representation(val) return None
def string_val_as_escaped_bytes(self): '''Get escaped value of the object. Turn the value of a string or bytes object into byte sequence with unknown, control, and \ characters escaped. This function should be used when looking for a known sequence in a potentially badly encoded string in the code. :return: sequence of printable ascii bytes representing original string ''' val = self.string_val if val is not None: # it's any of str or unicode in py2, or str in py3 return val.encode('unicode_escape') val = self.bytes_val if val is not None: return utils.escaped_bytes_representation(val) return None
def test_escaped_representation_mixed(self): res = b_utils.escaped_bytes_representation(b"ascii\u0000\uffff") self.assertEqual(res, b"ascii\\x00\\uffff")
def test_escaped_representation_invalid(self): res = b_utils.escaped_bytes_representation(b"\uffff") self.assertEqual(res, b"\\uffff")
def test_escaped_representation_valid_not_printable(self): res = b_utils.escaped_bytes_representation(b"\u0000") self.assertEqual(res, b"\\x00")
def test_escaped_representation_simple(self): res = b_utils.escaped_bytes_representation(b"ascii") self.assertEqual(res, b"ascii")
def test_escaped_representation_mixed(self): res = b_utils.escaped_bytes_representation(b"ascii\u0000\uffff") self.assertEqual(res, b"ascii\\x00\\uffff")
def test_escaped_representation_invalid(self): res = b_utils.escaped_bytes_representation(b"\uffff") self.assertEqual(res, b"\\uffff")
def test_escaped_representation_valid_not_printable(self): res = b_utils.escaped_bytes_representation(b"\u0000") self.assertEqual(res, b"\\x00")
def test_escaped_representation_simple(self): res = b_utils.escaped_bytes_representation(b"ascii") self.assertEqual(res, b"ascii")