def test_tailmatch(self, space): w_str = space.wrap(u"abcdef") # prefix match assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 2, 9, -1) == 1 assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 2, 4, -1) == 0 # ends at 'd' assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 1, 6, -1) == 0 # starts at 'b' assert PyUnicode_Tailmatch(space, w_str, space.wrap("cdf"), 2, 6, -1) == 0 # suffix match assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 1, 5, 1) == 1 assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 3, 5, 1) == 0 # starts at 'd' assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 1, 6, 1) == 0 # ends at 'f' assert PyUnicode_Tailmatch(space, w_str, space.wrap("bde"), 1, 5, 1) == 0 # type checks with raises_w(space, TypeError): PyUnicode_Tailmatch(space, w_str, space.wrap(3), 2, 10, 1) with raises_w(space, TypeError): PyUnicode_Tailmatch(space, space.wrap(3), space.wrap("abc"), 2, 10, 1)
def test_AsEncodedObject(self, space): ptr = space.wrap('abc') errors = rffi.str2charp("strict") encoding = rffi.str2charp("hex") res = PyString_AsEncodedObject(space, ptr, encoding, errors) assert space.unwrap(res) == "616263" res = PyString_AsEncodedObject(space, ptr, encoding, lltype.nullptr(rffi.CCHARP.TO)) assert space.unwrap(res) == "616263" rffi.free_charp(encoding) encoding = rffi.str2charp("unknown_encoding") with raises_w(space, LookupError): PyString_AsEncodedObject(space, ptr, encoding, errors) rffi.free_charp(encoding) rffi.free_charp(errors) NULL = lltype.nullptr(rffi.CCHARP.TO) res = PyString_AsEncodedObject(space, ptr, NULL, NULL) assert space.unwrap(res) == "abc" with raises_w(space, TypeError): PyString_AsEncodedObject(space, space.wrap(2), NULL, NULL)
def test_unicodeobject(self, space): assert PyUnicode_GET_SIZE(space, space.wrap(u'sp�m')) == 4 assert PyUnicode_GetSize(space, space.wrap(u'sp�m')) == 4 unichar = rffi.sizeof(Py_UNICODE) assert PyUnicode_GET_DATA_SIZE(space, space.wrap(u'sp�m')) == 4 * unichar encoding = rffi.charp2str(PyUnicode_GetDefaultEncoding(space, )) w_default_encoding = space.call_function( space.sys.get('getdefaultencoding')) assert encoding == space.unwrap(w_default_encoding) invalid = rffi.str2charp('invalid') utf_8 = rffi.str2charp('utf-8') prev_encoding = rffi.str2charp(space.unwrap(w_default_encoding)) with raises_w(space, TypeError): PyUnicode_SetDefaultEncoding(space, lltype.nullptr(rffi.CCHARP.TO)) with raises_w(space, LookupError): PyUnicode_SetDefaultEncoding(space, invalid) assert PyUnicode_SetDefaultEncoding(space, utf_8) == 0 assert rffi.charp2str(PyUnicode_GetDefaultEncoding(space, )) == 'utf-8' assert PyUnicode_SetDefaultEncoding(space, prev_encoding) == 0 rffi.free_charp(invalid) rffi.free_charp(utf_8) rffi.free_charp(prev_encoding)
def test_fromobject(self, space): w_u = space.wrap(u'a') assert PyUnicode_FromObject(space, w_u) is w_u with raises_w(space, TypeError): PyUnicode_FromObject(space, space.newbytes('test')) with raises_w(space, TypeError): PyUnicode_FromObject(space, space.newint(42))
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)
def test_intobject(self, space): assert PyInt_Check(space, space.wrap(3)) assert PyInt_Check(space, space.w_True) assert not PyInt_Check(space, space.wrap((1, 2, 3))) for i in [3, -5, -1, -sys.maxint, sys.maxint - 1]: x = PyInt_AsLong(space, space.wrap(i)) y = PyInt_AS_LONG(space, space.wrap(i)) assert x == i assert y == i w_x = PyInt_FromLong(space, x + 1) assert space.type(w_x) is space.w_int assert space.eq_w(w_x, space.wrap(i + 1)) with raises_w(space, TypeError): PyInt_AsLong(space, space.w_None) with raises_w(space, TypeError): PyInt_AsLong(space, None) assert PyInt_AsUnsignedLong(space, space.wrap(sys.maxint)) == sys.maxint with raises_w(space, ValueError): PyInt_AsUnsignedLong(space, space.wrap(-5)) assert (PyInt_AsUnsignedLongMask(space, space.wrap(sys.maxint)) == sys.maxint) assert (PyInt_AsUnsignedLongMask(space, space.wrap(10**30)) == 10**30 % ((sys.maxint + 1) * 2)) assert (PyInt_AsUnsignedLongLongMask(space, space.wrap( sys.maxint)) == sys.maxint) assert (PyInt_AsUnsignedLongLongMask(space, space.wrap( 10**30)) == 10**30 % (2**64))
def test_tupleobject(self, space): assert not PyTuple_Check(space, space.w_None) with raises_w(space, SystemError): PyTuple_SetItem(space, space.w_None, 0, space.w_None) atuple = space.newtuple( [space.wrap(0), space.wrap(1), space.wrap('yay')]) assert PyTuple_Size(space, atuple) == 3 with raises_w(space, SystemError): PyTuple_Size(space, space.newlist([]))
def test_exception(self, space, api): w_obj = space.appexec([], """(): class C: def __nonzero__(self): raise ValueError return C()""") with raises_w(space, ValueError): PyObject_IsTrue(space, w_obj) with raises_w(space, ValueError): PyObject_Not(space, w_obj)
def test_setobj(self, space): assert not PySet_Check(space, space.w_None) assert not PyFrozenSet_Check(space, space.w_None) with raises_w(space, SystemError): PySet_Add(space, space.w_None, space.w_None) w_set = space.call_function(space.w_set) assert not PyFrozenSet_CheckExact(space, w_set) space.call_method(w_set, 'update', space.wrap([1, 2, 3, 4])) assert PySet_Size(space, w_set) == 4 assert PySet_GET_SIZE(space, w_set) == 4 with raises_w(space, TypeError): PySet_Size(space, space.newlist([]))
def test_getattr(self, space): charp1 = rffi.str2charp("__len__") charp2 = rffi.str2charp("not_real") assert PyObject_GetAttrString(space, space.wrap(""), charp1) with raises_w(space, AttributeError): PyObject_GetAttrString(space, space.wrap(""), charp2) with raises_w(space, AttributeError): PyObject_DelAttrString(space, space.wrap(""), charp1) rffi.free_charp(charp1) rffi.free_charp(charp2) assert PyObject_GetAttr(space, space.wrap(""), space.wrap("__len__")) with raises_w(space, AttributeError): PyObject_DelAttr(space, space.wrap(""), space.wrap("__len__"))
def test_ascii(self, space): ustr = "abcdef" w_ustr = space.wrap(ustr.decode("ascii")) result = PyUnicode_AsASCIIString(space, w_ustr) assert space.eq_w(space.wrap(ustr), result) with raises_w(space, UnicodeEncodeError): PyUnicode_AsASCIIString(space, space.wrap(u"abcd\xe9f"))
def test_setitem(self, space, api): w_value = space.wrap(42) l = api.PyList_New(1) result = api.PySequence_SetItem(l, 0, w_value) assert result != -1 assert space.eq_w(space.getitem(l, space.wrap(0)), w_value) with raises_w(space, IndexError): PySequence_SetItem(space, l, 3, w_value) t = api.PyTuple_New(1) api.PyTuple_SetItem(t, 0, l) with raises_w(space, TypeError): PySequence_SetItem(space, t, 0, w_value) with raises_w(space, TypeError): PySequence_SetItem(space, space.newdict(), 0, w_value)
def test_delitem(self, space, api): w_l = space.wrap([1, 2, 3, 4]) result = api.PySequence_DelItem(w_l, 2) assert result == 0 assert space.eq_w(w_l, space.wrap([1, 2, 4])) with raises_w(space, IndexError): PySequence_DelItem(space, w_l, 3)
def test_FromObject(self, space, api): w_obj = space.newbytes("test") assert space.eq_w(w_obj, api.PyBytes_FromObject(w_obj)) w_obj = space.call_function(space.w_bytearray, w_obj) assert space.eq_w(w_obj, api.PyBytes_FromObject(w_obj)) w_obj = space.wrap(u"test") with raises_w(space, TypeError): api.PyBytes_FromObject(w_obj)
def test_run_file(self, space): filepath = udir / "cpyext_test_runfile.py" filepath.write("raise ZeroDivisionError") fp = c_fopen(str(filepath), "rb") filename = rffi.str2charp(str(filepath)) w_globals = w_locals = space.newdict() with raises_w(space, ZeroDivisionError): PyRun_File(space, fp, filename, Py_file_input, w_globals, w_locals) c_fclose(fp) # try again, but with a closed file fp = c_fopen(str(filepath), "rb") os.close(c_fileno(fp)) with raises_w(space, IOError): PyRun_File(space, fp, filename, Py_file_input, w_globals, w_locals) c_fclose(fp) rffi.free_charp(filename)
def test_IsSubclass(self, space, api): assert api.PyObject_IsSubclass(space.w_type, space.w_type) == 1 assert api.PyObject_IsSubclass(space.w_type, space.w_object) == 1 assert api.PyObject_IsSubclass(space.w_object, space.w_type) == 0 assert api.PyObject_IsSubclass( space.w_type, space.newtuple([space.w_int, space.w_type])) == 1 with raises_w(space, TypeError): PyObject_IsSubclass(space, space.wrap(1), space.w_type)
def test_frozenset_add(self, space, api): w_set = api.PyFrozenSet_New(None) api.PySet_Add(w_set, space.wrap(4)) assert api.PySet_Size(w_set) == 1 api.PySet_Add(w_set, space.wrap(5)) assert api.PySet_Size(w_set) == 2 assert space.hash_w(w_set) != 0 # makes the set really frozen with raises_w(space, SystemError): api.PySet_Add(w_set, space.wrap(6))
def test_cmp(self, space, api): w = space.wrap with lltype.scoped_alloc(rffi.INTP.TO, 1) as ptr: assert api.PyObject_Cmp(w(42), w(72), ptr) == 0 assert ptr[0] == -1 assert api.PyObject_Cmp(w("a"), w("a"), ptr) == 0 assert ptr[0] == 0 with raises_w(space, UnicodeDecodeError): PyObject_Cmp(space, w(u"\xe9"), w("\xe9"), ptr)
def test_update_doesnt_accept_list_of_tuples(self, space): w_d = space.newdict() space.setitem(w_d, space.wrap("a"), space.wrap("b")) w_d2 = space.wrap([("c", "d"), ("e", "f")]) with raises_w(space, AttributeError): PyDict_Update(space, w_d, w_d2) assert space.unwrap(w_d) == dict(a='b') # unchanged
def test_decode(self, space): b_text = rffi.str2charp('caf\x82xx') b_encoding = rffi.str2charp('cp437') assert space.utf8_w( PyUnicode_Decode(space, b_text, 4, b_encoding, None)) == u'caf\xe9'.encode('utf8') w_text = PyUnicode_FromEncodedObject(space, space.wrap("test"), b_encoding, None) assert space.isinstance_w(w_text, space.w_unicode) assert space.utf8_w(w_text) == "test" with raises_w(space, TypeError): PyUnicode_FromEncodedObject(space, space.wrap(u"test"), b_encoding, None) with raises_w(space, TypeError): PyUnicode_FromEncodedObject(space, space.wrap(1), b_encoding, None) rffi.free_charp(b_text) rffi.free_charp(b_encoding)
def test_IsInstance(self, space, api): assert api.PyObject_IsInstance(space.wrap(1), space.w_int) == 1 assert api.PyObject_IsInstance(space.wrap(1), space.w_float) == 0 assert api.PyObject_IsInstance(space.w_True, space.w_int) == 1 assert api.PyObject_IsInstance( space.wrap(1), space.newtuple([space.w_int, space.w_float])) == 1 assert api.PyObject_IsInstance(space.w_type, space.w_type) == 1 with raises_w(space, TypeError): PyObject_IsInstance(space, space.wrap(1), space.w_None)
def test_getitem(self, space, api): thelist = [8, 7, 6, 5, 4, 3, 2, 1] w_l = space.wrap(thelist) result = api.PySequence_GetItem(w_l, 4) assert space.is_true(space.eq(result, space.wrap(4))) result = api.PySequence_ITEM(w_l, 4) assert space.is_true(space.eq(result, space.wrap(4))) with raises_w(space, IndexError): PySequence_GetItem(space, w_l, 9000)
def test_endptr_no_conversion(self, space): s = rffi.str2constcharp('foo') endp = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw') with raises_w(space, ValueError): PyOS_string_to_double(space, s, endp, None) endp_addr = rffi.cast(rffi.LONG, endp[0]) s_addr = rffi.cast(rffi.LONG, s) assert endp_addr == s_addr rffi.free_charp(s) lltype.free(endp, flavor='raw')
def test_weakref(self, space, api): w_obj = space.w_Exception w_ref = api.PyWeakref_NewRef(w_obj, space.w_None) assert w_ref is not None assert space.is_w(api.PyWeakref_GetObject(w_ref), w_obj) assert space.is_w(api.PyWeakref_LockObject(w_ref), w_obj) w_obj = space.newtuple([]) with raises_w(space, TypeError): PyWeakref_NewRef(space, w_obj, space.w_None)
def test_run_file(self, space): filepath = udir / "cpyext_test_runfile.py" filepath.write("raise ZeroDivisionError") fp = c_fopen(str(filepath), "rb") filename = rffi.str2charp(str(filepath)) w_globals = w_locals = space.newdict() with raises_w(space, ZeroDivisionError): PyRun_File(space, fp, filename, Py_file_input, w_globals, w_locals) c_fclose(fp) # try again, but with a closed file if self.runappdirect: # according to man 2 fclose, any access of fp is undefined # behaviour. This crashes on some linux systems untranslated fp = c_fopen(str(filepath), "rb") c_fclose(fp) with raises_w(space, IOError): PyRun_File(space, fp, filename, Py_file_input, w_globals, w_locals) rffi.free_charp(filename)
def test_dict(self, space): d = PyDict_New(space) assert space.eq_w(d, space.newdict()) assert space.eq_w(PyDict_GetItem(space, space.wrap({"a": 72}), space.wrap("a")), space.wrap(72)) PyDict_SetItem(space, d, space.wrap("c"), space.wrap(42)) assert space.eq_w(space.getitem(d, space.wrap("c")), space.wrap(42)) space.setitem(d, space.wrap("name"), space.wrap(3)) assert space.eq_w(PyDict_GetItem(space, d, space.wrap("name")), space.wrap(3)) space.delitem(d, space.wrap("name")) assert not PyDict_GetItem(space, d, space.wrap("name")) buf = rffi.str2charp("name") assert not PyDict_GetItemString(space, d, buf) rffi.free_charp(buf) assert PyDict_Contains(space, d, space.wrap("c")) assert not PyDict_Contains(space, d, space.wrap("z")) PyDict_DelItem(space, d, space.wrap("c")) with raises_w(space, KeyError): PyDict_DelItem(space, d, space.wrap("name")) assert PyDict_Size(space, d) == 0 space.setitem(d, space.wrap("some_key"), space.wrap(3)) buf = rffi.str2charp("some_key") PyDict_DelItemString(space, d, buf) assert PyDict_Size(space, d) == 0 with raises_w(space, KeyError): PyDict_DelItemString(space, d, buf) rffi.free_charp(buf) d = space.wrap({'a': 'b'}) PyDict_Clear(space, d) assert PyDict_Size(space, d) == 0
def test_decode_null_encoding(self, space): null_charp = lltype.nullptr(rffi.CCHARP.TO) u_text = u'abcdefg' s_text = space.str_w(PyUnicode_AsEncodedString(space, space.wrap(u_text), null_charp, null_charp)) b_text = rffi.str2charp(s_text) assert space.unicode_w(PyUnicode_Decode( space, b_text, len(s_text), null_charp, null_charp)) == u_text with raises_w(space, TypeError): PyUnicode_FromEncodedObject( space, space.wrap(u_text), null_charp, None) rffi.free_charp(b_text)
def test_fileno(self, space, api): assert api.PyObject_AsFileDescriptor(space.wrap(1)) == 1 with raises_w(space, ValueError): PyObject_AsFileDescriptor(space, space.wrap(-20)) w_File = space.appexec([], """(): class File: def fileno(self): return 42 return File""") w_f = space.call_function(w_File) assert api.PyObject_AsFileDescriptor(w_f) == 42
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)
def test_complexobject(self, space): w_value = PyComplex_FromDoubles(space, 1.2, 3.4) assert space.unwrap(w_value) == 1.2 + 3.4j assert PyComplex_RealAsDouble(space, w_value) == 1.2 assert PyComplex_ImagAsDouble(space, w_value) == 3.4 assert PyComplex_RealAsDouble(space, space.wrap(42)) == 42 assert PyComplex_RealAsDouble(space, space.wrap(1.5)) == 1.5 assert PyComplex_ImagAsDouble(space, space.wrap(1.5)) == 0.0 # cpython accepts anything for PyComplex_ImagAsDouble assert PyComplex_ImagAsDouble(space, space.w_None) == 0.0 with raises_w(space, TypeError): PyComplex_RealAsDouble(space, space.w_None)