Beispiel #1
0
def test_unquote_bad():
    testv = (
        # in            error
        ('x"zzz"', 'no starting "'),
        ('"zzz', 'no closing "'),
        ('"\\', 'unexpected EOL after \\'),
        ('"\\x', 'unexpected EOL after \\x'),
        ('"\\x0', 'unexpected EOL after \\x'),
        ('"\\z"', 'invalid escape \\z'),
    )

    for tin, err in testv:
        with raises(ValueError) as exc:
            unquote(tin)
        assert exc.value.args == (err, )
Beispiel #2
0
def test_unquote_noncanon():
    testv = (
        # quoted w/o "      unquoted
        (r'\a', "\x07"),
        (r'\b', "\x08"),
        (r'\v', "\x0b"),
        (r'\f', "\x0c"),
    )

    for tquoted, tunquoted in testv:
        q = '"' + tquoted + '"'
        assert unquote(q) == tunquoted
Beispiel #3
0
def test_quote():
    testv = (
        # in                quoted without leading/trailing "
        ('', r""),
        (byterange(0, 32),
         br'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'
         ),
        ('\'', r"'"),
        ('"', r"\""),
        ('ab c\ndef', r"ab c\ndef"),
        ('a\'c\ndef', r"a'c\ndef"),
        ('a\"c\ndef', r"a\"c\ndef"),
        (u'a\"c\ndef', u"a\\\"c\\ndef"),
        (b'a\"c\ndef', br'a\"c\ndef'),
        ('привет\nмир', r"привет\nмир"),
        (u'привет\nмир', u"привет\\nмир"),

        # invalid utf-8
        (b"\xd0a", br"\xd0a"),

        # non-printable utf-8
        (u"\u007f\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087",
         u"\\x7f\\xc2\\x80\\xc2\\x81\\xc2\\x82\\xc2\\x83\\xc2\\x84\\xc2\\x85\\xc2\\x86\\xc2\\x87"
         ),
    )

    for tin, tquoted in testv:
        # quote(in) == quoted
        # in = unquote(quoted)
        q = b'"' if isinstance(tquoted, bytes) else '"'
        tail = b'123' if isinstance(tquoted, bytes) else '123'
        tquoted = q + tquoted + q  # add lead/trail "

        assert quote(tin) == tquoted
        assert unquote(tquoted) == tin
        assert unquote_next(tquoted) == (tin, type(tin)())
        assert unquote_next(tquoted + tail) == (tin, tail)
        with raises(ValueError):
            unquote(tquoted + tail)

        # qq always gives str
        assert qq(tin) == asstr(tquoted)

        # also check how it works on complementary unicode/bytes input type
        if isinstance(tin, bytes):
            try:
                tin = tin.decode('utf-8')
            except UnicodeDecodeError:
                # some inputs are not valid UTF-8
                continue
            tquoted = tquoted.decode('utf-8')
            tail = tail.decode('utf-8')
        else:
            # tin was unicode
            tin = tin.encode('utf-8')
            tquoted = tquoted.encode('utf-8')
            tail = tail.encode('utf-8')

        assert quote(tin) == tquoted
        assert unquote(tquoted) == tin
        assert unquote_next(tquoted) == (tin, type(tin)())
        assert unquote_next(tquoted + tail) == (tin, tail)
        with raises(ValueError):
            unquote(tquoted + tail)

        # qq always gives str
        assert qq(tin) == asstr(tquoted)
Beispiel #4
0
        def get(name):
            l = self._readline()
            if l is None or not l.startswith(b'%s ' % name):
                self._badline('no %s' % name)

            return strconv.unquote(l[len(name) + 1:])