Ejemplo n.º 1
0
    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
Ejemplo n.º 3
0
 def test_escaped_representation_mixed(self):
     res = b_utils.escaped_bytes_representation(b"ascii\u0000\uffff")
     self.assertEqual(res, b"ascii\\x00\\uffff")
Ejemplo n.º 4
0
 def test_escaped_representation_invalid(self):
     res = b_utils.escaped_bytes_representation(b"\uffff")
     self.assertEqual(res, b"\\uffff")
Ejemplo n.º 5
0
 def test_escaped_representation_valid_not_printable(self):
     res = b_utils.escaped_bytes_representation(b"\u0000")
     self.assertEqual(res, b"\\x00")
Ejemplo n.º 6
0
 def test_escaped_representation_simple(self):
     res = b_utils.escaped_bytes_representation(b"ascii")
     self.assertEqual(res, b"ascii")
Ejemplo n.º 7
0
 def test_escaped_representation_mixed(self):
     res = b_utils.escaped_bytes_representation(b"ascii\u0000\uffff")
     self.assertEqual(res, b"ascii\\x00\\uffff")
Ejemplo n.º 8
0
 def test_escaped_representation_invalid(self):
     res = b_utils.escaped_bytes_representation(b"\uffff")
     self.assertEqual(res, b"\\uffff")
Ejemplo n.º 9
0
 def test_escaped_representation_valid_not_printable(self):
     res = b_utils.escaped_bytes_representation(b"\u0000")
     self.assertEqual(res, b"\\x00")
Ejemplo n.º 10
0
 def test_escaped_representation_simple(self):
     res = b_utils.escaped_bytes_representation(b"ascii")
     self.assertEqual(res, b"ascii")