Exemplo n.º 1
0
def strcoll(space, w_s1, w_s2):
    "string,string -> int. Compares two strings according to the locale."

    if (space.isinstance_w(w_s1, space.w_str) and
        space.isinstance_w(w_s2, space.w_str)):

        s1, s2 = space.str_w(w_s1), space.str_w(w_s2)
        s1_c = rffi.str2charp(s1)
        s2_c = rffi.str2charp(s2)
        try:
            return space.wrap(_strcoll(s1_c, s2_c))
        finally:
            rffi.free_charp(s1_c)
            rffi.free_charp(s2_c)

    s1, s2 = space.unicode_w(w_s1), space.unicode_w(w_s2)

    s1_c = rffi.unicode2wcharp(s1)
    s2_c = rffi.unicode2wcharp(s2)
    try:
        result = _wcscoll(s1_c, s2_c)
    finally:
        rffi.free_wcharp(s1_c)
        rffi.free_wcharp(s2_c)

    return space.wrap(result)
Exemplo n.º 2
0
    def test_ascii_codec(self, space):
        s = 'abcdefg'
        data = rffi.str2charp(s)
        NULL = lltype.nullptr(rffi.CCHARP.TO)
        w_u = PyUnicode_DecodeASCII(space, data, len(s), NULL)
        assert space.eq_w(w_u, space.wrap(u"abcdefg"))
        rffi.free_charp(data)

        s = 'abcd\xFF'
        data = rffi.str2charp(s)
        with raises_w(space, UnicodeDecodeError):
            PyUnicode_DecodeASCII(space, data, len(s), NULL)
        rffi.free_charp(data)

        uni = u'abcdefg'
        data = rffi.unicode2wcharp(uni)
        w_s = PyUnicode_EncodeASCII(space, data, len(uni), NULL)
        assert space.eq_w(space.wrap("abcdefg"), w_s)
        rffi.free_wcharp(data)

        u = u'�bcd�fg'
        data = rffi.unicode2wcharp(u)
        with raises_w(space, UnicodeEncodeError):
            PyUnicode_EncodeASCII(space, data, len(u), NULL)
        rffi.free_wcharp(data)
Exemplo n.º 3
0
def strcoll(space, w_s1, w_s2):
    "string,string -> int. Compares two strings according to the locale."

    if (space.isinstance_w(w_s1, space.w_bytes) and
        space.isinstance_w(w_s2, space.w_bytes)):

        s1, s2 = space.bytes_w(w_s1), space.bytes_w(w_s2)
        s1_c = rffi.str2charp(s1)
        s2_c = rffi.str2charp(s2)
        try:
            return space.newint(_strcoll(s1_c, s2_c))
        finally:
            rffi.free_charp(s1_c)
            rffi.free_charp(s2_c)

    s1, s2 = space.unicode_w(w_s1), space.unicode_w(w_s2)

    s1_c = rffi.unicode2wcharp(s1)
    s2_c = rffi.unicode2wcharp(s2)
    try:
        result = _wcscoll(s1_c, s2_c)
    finally:
        rffi.free_wcharp(s1_c)
        rffi.free_wcharp(s2_c)

    return space.newint(result)
Exemplo n.º 4
0
    def test_ascii_codec(self, space, api):
        s = 'abcdefg'
        data = rffi.str2charp(s)
        w_u = api.PyUnicode_DecodeASCII(data, len(s),
                                        lltype.nullptr(rffi.CCHARP.TO))
        assert space.eq_w(w_u, space.wrap(u"abcdefg"))
        rffi.free_charp(data)

        s = 'abcd\xFF'
        data = rffi.str2charp(s)
        self.raises(space, api, UnicodeDecodeError, api.PyUnicode_DecodeASCII,
                    data, len(s), lltype.nullptr(rffi.CCHARP.TO))
        rffi.free_charp(data)

        uni = u'abcdefg'
        data = rffi.unicode2wcharp(uni)
        w_s = api.PyUnicode_EncodeASCII(data, len(uni),
                                        lltype.nullptr(rffi.CCHARP.TO))
        assert space.eq_w(space.wrap("abcdefg"), w_s)
        rffi.free_wcharp(data)

        u = u'äbcdéfg'
        data = rffi.unicode2wcharp(u)
        w_s = api.PyUnicode_EncodeASCII(data, len(u),
                                        lltype.nullptr(rffi.CCHARP.TO))
        self.raises(space, api, UnicodeEncodeError, api.PyUnicode_EncodeASCII,
                    data, len(u), lltype.nullptr(rffi.CCHARP.TO))
        rffi.free_wcharp(data)
Exemplo n.º 5
0
    def test_ascii_codec(self, space, api):
        s = 'abcdefg'
        data = rffi.str2charp(s)
        w_u = api.PyUnicode_DecodeASCII(data, len(s), lltype.nullptr(rffi.CCHARP.TO))
        assert space.eq_w(w_u, space.wrap(u"abcdefg"))
        rffi.free_charp(data)

        s = 'abcd\xFF'
        data = rffi.str2charp(s)
        self.raises(space, api, UnicodeDecodeError, api.PyUnicode_DecodeASCII,
                    data, len(s), lltype.nullptr(rffi.CCHARP.TO))
        rffi.free_charp(data)

        uni = u'abcdefg'
        data = rffi.unicode2wcharp(uni)
        w_s = api.PyUnicode_EncodeASCII(data, len(uni), lltype.nullptr(rffi.CCHARP.TO))
        assert space.eq_w(space.wrap("abcdefg"), w_s)
        rffi.free_wcharp(data)

        u = u'äbcdéfg'
        data = rffi.unicode2wcharp(u)
        w_s = api.PyUnicode_EncodeASCII(data, len(u), lltype.nullptr(rffi.CCHARP.TO))
        self.raises(space, api, UnicodeEncodeError, api.PyUnicode_EncodeASCII,
                    data, len(u), lltype.nullptr(rffi.CCHARP.TO))
        rffi.free_wcharp(data)
Exemplo n.º 6
0
 def test_mbcs(self, space, api):
     if sys.platform != 'win32':
         py.test.skip("mcbs encoding only exists on Windows")
     # unfortunately, mbcs is locale-dependent.
     # This tests works at least on a Western Windows.
     unichars = u"abc" + unichr(12345)
     wbuf = rffi.unicode2wcharp(unichars)
     w_str = api.PyUnicode_EncodeMBCS(wbuf, 4, None)
     rffi.free_wcharp(wbuf)
     assert space.type(w_str) is space.w_str
     assert space.str_w(w_str) == "abc?"
Exemplo n.º 7
0
 def test_mbcs(self, space, api):
     if sys.platform != 'win32':
         py.test.skip("mcbs encoding only exists on Windows")
     # unfortunately, mbcs is locale-dependent.
     # This tests works at least on a Western Windows.
     unichars = u"abc" + unichr(12345)
     wbuf = rffi.unicode2wcharp(unichars)
     w_str = api.PyUnicode_EncodeMBCS(wbuf, 4, None)
     rffi.free_wcharp(wbuf)
     assert space.type(w_str) is space.w_str
     assert space.str_w(w_str) == "abc?"
Exemplo n.º 8
0
        def stop_error_capture(self, w_done):
            if w_done is None:
                return

            w_text = self.space.call_function(w_done)
            p = rffi.unicode2wcharp(self.space.unicode_w(w_text),
                                    track_allocation=False)
            if self.text_p:
                rffi.free_wcharp(self.text_p, track_allocation=False)
            self.text_p = p  # keepalive

            cffi_errorbox(p)
Exemplo n.º 9
0
def strcoll(space, w_s1, w_s2):
    "string,string -> int. Compares two strings according to the locale."

    s1, s2 = space.unicode_w(w_s1), space.unicode_w(w_s2)

    s1_c = rffi.unicode2wcharp(s1)
    s2_c = rffi.unicode2wcharp(s2)
    try:
        result = _wcscoll(s1_c, s2_c)
    finally:
        rffi.free_wcharp(s1_c)
        rffi.free_wcharp(s2_c)

    return space.wrap(result)
Exemplo n.º 10
0
def strcoll(space, w_s1, w_s2):
    "string,string -> int. Compares two strings according to the locale."

    s1, s2 = space.unicode_w(w_s1), space.unicode_w(w_s2)

    s1_c = rffi.unicode2wcharp(s1)
    s2_c = rffi.unicode2wcharp(s2)
    try:
        result = _wcscoll(s1_c, s2_c)
    finally:
        rffi.free_wcharp(s1_c)
        rffi.free_wcharp(s2_c)

    return space.wrap(result)
Exemplo n.º 11
0
    def test_latin1(self, space, api):
        s = 'abcdefg'
        data = rffi.str2charp(s)
        w_u = api.PyUnicode_DecodeLatin1(data, len(s), lltype.nullptr(rffi.CCHARP.TO))
        assert space.eq_w(w_u, space.wrap(u"abcdefg"))
        rffi.free_charp(data)

        uni = u'abcdefg'
        data = rffi.unicode2wcharp(uni)
        w_s = api.PyUnicode_EncodeLatin1(data, len(uni), lltype.nullptr(rffi.CCHARP.TO))
        assert space.eq_w(space.wrap("abcdefg"), w_s)
        rffi.free_wcharp(data)

        ustr = "abcdef"
        w_ustr = space.wrap(ustr.decode("ascii"))
        result = api.PyUnicode_AsLatin1String(w_ustr)
        assert space.eq_w(space.wrap(ustr), result)
Exemplo n.º 12
0
    def test_latin1(self, space, api):
        s = 'abcdefg'
        data = rffi.str2charp(s)
        w_u = api.PyUnicode_DecodeLatin1(data, len(s), lltype.nullptr(rffi.CCHARP.TO))
        assert space.eq_w(w_u, space.wrap(u"abcdefg"))
        rffi.free_charp(data)

        uni = u'abcdefg'
        data = rffi.unicode2wcharp(uni)
        w_s = api.PyUnicode_EncodeLatin1(data, len(uni), lltype.nullptr(rffi.CCHARP.TO))
        assert space.eq_w(space.wrapbytes("abcdefg"), w_s)
        rffi.free_wcharp(data)

        ustr = "abcdef"
        w_ustr = space.wrap(ustr.decode("ascii"))
        result = api.PyUnicode_AsLatin1String(w_ustr)
        assert space.eq_w(space.wrapbytes(ustr), result)
Exemplo n.º 13
0
def strcoll(space, w_s1, w_s2):
    "string,string -> int. Compares two strings according to the locale."

    s1, l1 = space.utf8_len_w(w_s1)
    s2, l2 = space.utf8_len_w(w_s2)
    if '\x00' in s1 or '\x00' in s2:
        raise oefmt(space.w_ValueError, "embedded null character")

    s1_c = rffi.utf82wcharp(s1, l1)
    s2_c = rffi.utf82wcharp(s2, l2)
    try:
        result = _wcscoll(s1_c, s2_c)
    finally:
        rffi.free_wcharp(s1_c)
        rffi.free_wcharp(s2_c)

    return space.newint(result)
Exemplo n.º 14
0
    def test_AS(self, space):
        word = space.wrap(u'spam')
        array = rffi.cast(rffi.CWCHARP, PyUnicode_AS_DATA(space, word))
        array2 = PyUnicode_AS_UNICODE(space, word)
        array3 = PyUnicode_AsUnicode(space, word)
        for (i, char) in enumerate(space.utf8_w(word)):
            assert array[i] == char
            assert array2[i] == char
            assert array3[i] == char
        with raises_w(space, TypeError):
            PyUnicode_AsUnicode(space, space.newbytes('spam'))

        utf_8 = rffi.str2charp('utf-8')
        encoded = PyUnicode_AsEncodedString(space, space.wrap(u'sp�m'),
                                                utf_8, None)
        assert space.unwrap(encoded) == 'sp\xef\xbf\xbdm'
        encoded_obj = PyUnicode_AsEncodedObject(space, space.wrap(u'sp�m'),
                                                utf_8, None)
        assert space.eq_w(encoded, encoded_obj)
        one = space.newint(1)
        with raises_w(space, TypeError):
            PyUnicode_AsEncodedString(
                space, space.newtuple([one, one, one]), None, None)
        with raises_w(space, TypeError):
            PyUnicode_AsEncodedString(space, space.wrap(''), None, None)
        ascii = rffi.str2charp('ascii')
        replace = rffi.str2charp('replace')
        encoded = PyUnicode_AsEncodedString(space, space.wrap(u'sp�m'),
                                                ascii, replace)
        assert space.unwrap(encoded) == 'sp?m'
        rffi.free_charp(utf_8)
        rffi.free_charp(replace)
        rffi.free_charp(ascii)

        buf = rffi.unicode2wcharp(u"12345")
        PyUnicode_AsWideChar(space, space.wrap(u'longword'), buf, 5)
        assert rffi.wcharp2unicode(buf) == 'longw'
        PyUnicode_AsWideChar(space, space.wrap(u'a'), buf, 5)
        assert rffi.wcharp2unicode(buf) == 'a'
        rffi.free_wcharp(buf)
Exemplo n.º 15
0
    def test_AS(self, space, api):
        word = space.wrap(u'spam')
        array = rffi.cast(rffi.CWCHARP, api.PyUnicode_AS_DATA(word))
        array2 = api.PyUnicode_AS_UNICODE(word)
        array3 = api.PyUnicode_AsUnicode(word)
        for (i, char) in enumerate(space.unwrap(word)):
            assert array[i] == char
            assert array2[i] == char
            assert array3[i] == char
        self.raises(space, api, TypeError, api.PyUnicode_AsUnicode,
                    space.wrap('spam'))

        utf_8 = rffi.str2charp('utf-8')
        encoded = api.PyUnicode_AsEncodedString(space.wrap(u'späm'),
                                                utf_8, None)
        assert space.unwrap(encoded) == 'sp\xc3\xa4m'
        encoded_obj = api.PyUnicode_AsEncodedObject(space.wrap(u'späm'),
                                                utf_8, None)
        assert space.eq_w(encoded, encoded_obj)
        self.raises(space, api, TypeError, api.PyUnicode_AsEncodedString,
               space.newtuple([1, 2, 3]), None, None)
        self.raises(space, api, TypeError, api.PyUnicode_AsEncodedString,
               space.wrap(''), None, None)
        ascii = rffi.str2charp('ascii')
        replace = rffi.str2charp('replace')
        encoded = api.PyUnicode_AsEncodedString(space.wrap(u'späm'),
                                                ascii, replace)
        assert space.unwrap(encoded) == 'sp?m'
        rffi.free_charp(utf_8)
        rffi.free_charp(replace)
        rffi.free_charp(ascii)

        buf = rffi.unicode2wcharp(u"12345")
        api.PyUnicode_AsWideChar(space.wrap(u'longword'), buf, 5)
        assert rffi.wcharp2unicode(buf) == 'longw'
        api.PyUnicode_AsWideChar(space.wrap(u'a'), buf, 5)
        assert rffi.wcharp2unicode(buf) == 'a'
        rffi.free_wcharp(buf)
Exemplo n.º 16
0
    def test_AS(self, space, api):
        word = space.wrap(u'spam')
        array = rffi.cast(rffi.CWCHARP, api.PyUnicode_AS_DATA(word))
        array2 = api.PyUnicode_AS_UNICODE(word)
        array3 = api.PyUnicode_AsUnicode(word)
        for (i, char) in enumerate(space.unwrap(word)):
            assert array[i] == char
            assert array2[i] == char
            assert array3[i] == char
        self.raises(space, api, TypeError, api.PyUnicode_AsUnicode,
                    space.wrap('spam'))

        utf_8 = rffi.str2charp('utf-8')
        encoded = api.PyUnicode_AsEncodedString(space.wrap(u'späm'), utf_8,
                                                None)
        assert space.unwrap(encoded) == 'sp\xc3\xa4m'
        encoded_obj = api.PyUnicode_AsEncodedObject(space.wrap(u'späm'),
                                                    utf_8, None)
        assert space.eq_w(encoded, encoded_obj)
        self.raises(space, api, TypeError, api.PyUnicode_AsEncodedString,
                    space.newtuple([1, 2, 3]), None, None)
        self.raises(space, api, TypeError, api.PyUnicode_AsEncodedString,
                    space.wrap(''), None, None)
        ascii = rffi.str2charp('ascii')
        replace = rffi.str2charp('replace')
        encoded = api.PyUnicode_AsEncodedString(space.wrap(u'späm'), ascii,
                                                replace)
        assert space.unwrap(encoded) == 'sp?m'
        rffi.free_charp(utf_8)
        rffi.free_charp(replace)
        rffi.free_charp(ascii)

        buf = rffi.unicode2wcharp(u"12345")
        api.PyUnicode_AsWideChar(space.wrap(u'longword'), buf, 5)
        assert rffi.wcharp2unicode(buf) == 'longw'
        api.PyUnicode_AsWideChar(space.wrap(u'a'), buf, 5)
        assert rffi.wcharp2unicode(buf) == 'a'
        rffi.free_wcharp(buf)
Exemplo n.º 17
0
 def test_encode_utf8(self, space):
     u = rffi.unicode2wcharp(u'sp\x09m')
     w_s = PyUnicode_EncodeUTF8(space, u, 4, None)
     assert space.unicode_w(w_s) == u'sp\x09m'.encode('utf-8')
     rffi.free_wcharp(u)
Exemplo n.º 18
0
 def test_encode_utf8(self, space):
     u = rffi.unicode2wcharp(u'sp\x09m')
     w_b = PyUnicode_EncodeUTF8(space, u, 4, None)
     assert space.type(w_b) is space.w_bytes
     assert space.bytes_w(w_b) == u'sp\x09m'.encode('utf-8')
     rffi.free_wcharp(u)
Exemplo n.º 19
0
 def test_encode_utf8(self, space, api):
     u = rffi.unicode2wcharp(u'späm')
     w_s = api.PyUnicode_EncodeUTF8(u, 4, None)
     assert space.unwrap(w_s) == u'späm'.encode('utf-8')
     rffi.free_wcharp(u)
Exemplo n.º 20
0
 def test_encode_utf8(self, space, api):
     u = rffi.unicode2wcharp(u'späm')
     w_s = api.PyUnicode_EncodeUTF8(u, 4, None)
     assert space.unwrap(w_s) == u'späm'.encode('utf-8')
     rffi.free_wcharp(u)