Author: Armin Rigo <ar...@tunes.org> Branch: ffi-backend Changeset: r56572:2b97229b0b44 Date: 2012-08-04 16:08 +0200 http://bitbucket.org/pypy/pypy/changeset/2b97229b0b44/
Log: Update to the latest revision: str(), unicode() => ffi.string() diff --git a/pypy/module/_cffi_backend/__init__.py b/pypy/module/_cffi_backend/__init__.py --- a/pypy/module/_cffi_backend/__init__.py +++ b/pypy/module/_cffi_backend/__init__.py @@ -29,6 +29,7 @@ '_getfields': 'func._getfields', 'getcname': 'func.getcname', + 'string': 'func.string', 'buffer': 'cbuffer.buffer', 'get_errno': 'cerrno.get_errno', diff --git a/pypy/module/_cffi_backend/cdataobj.py b/pypy/module/_cffi_backend/cdataobj.py --- a/pypy/module/_cffi_backend/cdataobj.py +++ b/pypy/module/_cffi_backend/cdataobj.py @@ -71,12 +71,6 @@ "cdata of type '%s' has no len()", self.ctype.name) - def str(self): - return self.ctype.str(self) - - def unicode(self): - return self.ctype.unicode(self) - def _make_comparison(name): op = getattr(operator, name) requires_ordering = name not in ('eq', 'ne') @@ -302,8 +296,6 @@ __long__ = interp2app(W_CData.long), __float__ = interp2app(W_CData.float), __len__ = interp2app(W_CData.len), - __str__ = interp2app(W_CData.str), - __unicode__ = interp2app(W_CData.unicode), __lt__ = interp2app(W_CData.lt), __le__ = interp2app(W_CData.le), __eq__ = interp2app(W_CData.eq), diff --git a/pypy/module/_cffi_backend/ctypearray.py b/pypy/module/_cffi_backend/ctypearray.py --- a/pypy/module/_cffi_backend/ctypearray.py +++ b/pypy/module/_cffi_backend/ctypearray.py @@ -26,21 +26,6 @@ self.length = length self.ctptr = ctptr - def str(self, cdataobj): - if isinstance(self.ctitem, W_CTypePrimitiveChar): - s = rffi.charp2strn(cdataobj._cdata, cdataobj.get_array_length()) - keepalive_until_here(cdataobj) - return self.space.wrap(s) - return W_CTypePtrOrArray.str(self, cdataobj) - - def unicode(self, cdataobj): - if isinstance(self.ctitem, W_CTypePrimitiveUniChar): - s = rffi.wcharp2unicoden(rffi.cast(rffi.CWCHARP, cdataobj._cdata), - cdataobj.get_array_length()) - keepalive_until_here(cdataobj) - return self.space.wrap(s) - return W_CTypePtrOrArray.unicode(self, cdataobj) - def _alignof(self): return self.ctitem.alignof() diff --git a/pypy/module/_cffi_backend/ctypeenum.py b/pypy/module/_cffi_backend/ctypeenum.py --- a/pypy/module/_cffi_backend/ctypeenum.py +++ b/pypy/module/_cffi_backend/ctypeenum.py @@ -39,10 +39,10 @@ space.call_method(w_lst, 'sort') return w_lst - def str(self, cdataobj): - w_res = self.convert_to_object(cdataobj._cdata) + def string(self, cdataobj, maxlen): + w_result = self.convert_to_object(cdataobj._cdata) keepalive_until_here(cdataobj) - return w_res + return w_result def convert_to_object(self, cdata): value = intmask(misc.read_raw_signed_data(cdata, self.size)) diff --git a/pypy/module/_cffi_backend/ctypeobj.py b/pypy/module/_cffi_backend/ctypeobj.py --- a/pypy/module/_cffi_backend/ctypeobj.py +++ b/pypy/module/_cffi_backend/ctypeobj.py @@ -97,11 +97,11 @@ "cdata of type '%s' cannot be indexed", self.name) - def str(self, cdataobj): - return cdataobj.repr() - - def unicode(self, cdataobj): - return cdataobj.repr() + def string(self, cdataobj, maxlen): + space = self.space + raise operationerrfmt(space.w_TypeError, + "string(): unexpected cdata '%s' argument", + self.name) def add(self, cdata, i): space = self.space diff --git a/pypy/module/_cffi_backend/ctypeprim.py b/pypy/module/_cffi_backend/ctypeprim.py --- a/pypy/module/_cffi_backend/ctypeprim.py +++ b/pypy/module/_cffi_backend/ctypeprim.py @@ -71,6 +71,13 @@ raise operationerrfmt(space.w_OverflowError, "integer %s does not fit '%s'", s, self.name) + def string(self, cdataobj, maxlen): + if self.size == 1: + s = cdataobj._cdata[0] + keepalive_until_here(cdataobj) + return self.space.wrap(s) + return W_CType.string(self, cdataobj, maxlen) + class W_CTypePrimitiveCharOrUniChar(W_CTypePrimitive): _attrs_ = [] @@ -91,11 +98,6 @@ def convert_to_object(self, cdata): return self.space.wrap(cdata[0]) - def str(self, cdataobj): - w_res = self.convert_to_object(cdataobj._cdata) - keepalive_until_here(cdataobj) - return w_res - def _convert_to_char(self, w_ob): space = self.space if space.isinstance_w(w_ob, space.w_str): @@ -125,7 +127,7 @@ s = rffi.wcharpsize2unicode(unichardata, 1) return self.space.wrap(s) - def unicode(self, cdataobj): + def string(self, cdataobj, maxlen): w_res = self.convert_to_object(cdataobj._cdata) keepalive_until_here(cdataobj) return w_res diff --git a/pypy/module/_cffi_backend/ctypeptr.py b/pypy/module/_cffi_backend/ctypeptr.py --- a/pypy/module/_cffi_backend/ctypeptr.py +++ b/pypy/module/_cffi_backend/ctypeptr.py @@ -108,6 +108,41 @@ else: raise self._convert_error("list or tuple", w_ob) + def string(self, cdataobj, maxlen): + space = self.space + if isinstance(self.ctitem, ctypeprim.W_CTypePrimitive): + cdata = cdataobj._cdata + if not cdata: + raise operationerrfmt(space.w_RuntimeError, + "cannot use string() on %s", + space.str_w(cdataobj.repr())) + # + from pypy.module._cffi_backend import ctypearray + length = maxlen + if length < 0 and isinstance(self, ctypearray.W_CTypeArray): + length = cdataobj.get_array_length() + # + # pointer to a primitive type of size 1: builds and returns a str + if self.ctitem.size == rffi.sizeof(lltype.Char): + if length < 0: + s = rffi.charp2str(cdata) + else: + s = rffi.charp2strn(cdata, length) + keepalive_until_here(cdataobj) + return space.wrap(s) + # + # pointer to a wchar_t: builds and returns a unicode + if self.is_unichar_ptr_or_array(): + cdata = rffi.cast(rffi.CWCHARP, cdata) + if length < 0: + u = rffi.wcharp2unicode(cdata) + else: + u = rffi.wcharp2unicoden(cdata, length) + keepalive_until_here(cdataobj) + return space.wrap(u) + # + return W_CType.string(self, cdataobj, maxlen) + class W_CTypePtrBase(W_CTypePtrOrArray): # base class for both pointers and pointers-to-functions @@ -152,30 +187,6 @@ extra = " *" W_CTypePtrBase.__init__(self, space, size, extra, 2, ctitem) - def str(self, cdataobj): - if self.is_char_ptr_or_array(): - if not cdataobj._cdata: - space = self.space - raise operationerrfmt(space.w_RuntimeError, - "cannot use str() on %s", - space.str_w(cdataobj.repr())) - s = rffi.charp2str(cdataobj._cdata) - keepalive_until_here(cdataobj) - return self.space.wrap(s) - return W_CTypePtrOrArray.str(self, cdataobj) - - def unicode(self, cdataobj): - if self.is_unichar_ptr_or_array(): - if not cdataobj._cdata: - space = self.space - raise operationerrfmt(space.w_RuntimeError, - "cannot use unicode() on %s", - space.str_w(cdataobj.repr())) - s = rffi.wcharp2unicode(rffi.cast(rffi.CWCHARP, cdataobj._cdata)) - keepalive_until_here(cdataobj) - return self.space.wrap(s) - return W_CTypePtrOrArray.unicode(self, cdataobj) - def newp(self, w_init): space = self.space ctitem = self.ctitem diff --git a/pypy/module/_cffi_backend/func.py b/pypy/module/_cffi_backend/func.py --- a/pypy/module/_cffi_backend/func.py +++ b/pypy/module/_cffi_backend/func.py @@ -69,3 +69,9 @@ p = ctype.name_position s = '%s%s%s' % (ctype.name[:p], replace_with, ctype.name[p:]) return space.wrap(s) + +# ____________________________________________________________ + +@unwrap_spec(cdata=cdataobj.W_CData, maxlen=int) +def string(space, cdata, maxlen=-1): + return cdata.ctype.string(cdata, maxlen) diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py --- a/pypy/module/_cffi_backend/test/_backend_test_c.py +++ b/pypy/module/_cffi_backend/test/_backend_test_c.py @@ -120,7 +120,7 @@ assert long(cast(p, 'A')) == 65L assert type(int(cast(p, 'A'))) is int assert type(long(cast(p, 'A'))) is long - assert str(cast(p, 'A')) == 'A' + assert str(cast(p, 'A')) == repr(cast(p, 'A')) assert repr(cast(p, 'A')) == "<cdata 'char' 'A'>" assert repr(cast(p, 255)) == r"<cdata 'char' '\xff'>" assert repr(cast(p, 0)) == r"<cdata 'char' '\x00'>" @@ -225,7 +225,9 @@ assert p[0] == 'A' py.test.raises(TypeError, newp, BPtr, 65) py.test.raises(TypeError, newp, BPtr, "foo") - assert str(cast(BChar, 'A')) == 'A' + c = cast(BChar, 'A') + assert str(c) == repr(c) + assert int(c) == ord('A') py.test.raises(TypeError, cast, BChar, 'foo') def test_reading_pointer_to_pointer(): @@ -285,6 +287,9 @@ py.test.raises(TypeError, "p[0]") def test_default_str(): + BChar = new_primitive_type("char") + x = cast(BChar, 42) + assert str(x) == repr(x) BInt = new_primitive_type("int") x = cast(BInt, 42) assert str(x) == repr(x) @@ -310,7 +315,7 @@ y = cast(BInt, x) assert int(y) == 42 y = cast(new_primitive_type("char"), x) - assert str(y) == chr(42) + assert int(y) == 42 y = cast(new_primitive_type("float"), x) assert float(y) == 42.0 # @@ -451,7 +456,7 @@ # p = new_primitive_type("char") n = cast(p, cast(p, "A")) - assert str(n) == "A" + assert int(n) == ord("A") def test_new_primitive_from_cdata(): p = new_primitive_type("int") @@ -949,14 +954,14 @@ BEnum = new_enum_type("foo", ('def', 'c', 'ab'), (0, 1, -20)) e = cast(BEnum, 0) assert repr(e) == "<cdata 'enum foo' 'def'>" - assert str(e) == 'def' - assert str(cast(BEnum, -20)) == 'ab' - assert str(cast(BEnum, 'c')) == 'c' + assert string(e) == 'def' + assert string(cast(BEnum, -20)) == 'ab' + assert string(cast(BEnum, 'c')) == 'c' assert int(cast(BEnum, 'c')) == 1 assert int(cast(BEnum, 'def')) == 0 assert int(cast(BEnum, -242 + 2**128)) == -242 - assert str(cast(BEnum, -242 + 2**128)) == '#-242' - assert str(cast(BEnum, '#-20')) == 'ab' + assert string(cast(BEnum, -242 + 2**128)) == '#-242' + assert string(cast(BEnum, '#-20')) == 'ab' assert repr(cast(BEnum, '#-20')) == "<cdata 'enum foo' 'ab'>" assert repr(cast(BEnum, '#-21')) == "<cdata 'enum foo' '#-21'>" @@ -1106,11 +1111,12 @@ BArray1 = new_array_type(new_pointer_type(BChar), 5) BArray2 = new_array_type(new_pointer_type(BArray1), 5) a = newp(BArray2, ["abc", "de", "ghij"]) - assert str(a[2]) == "ghij" + assert string(a[1]) == "de" + assert string(a[2]) == "ghij" a[2] = "." - assert str(a[2]) == "." + assert string(a[2]) == "." a[2] = "12345" - assert str(a[2]) == "12345" + assert string(a[2]) == "12345" e = py.test.raises(IndexError, 'a[2] = "123456"') assert 'char[5]' in str(e.value) assert 'got 6 characters' in str(e.value) @@ -1203,16 +1209,53 @@ p2 = newp(new_pointer_type(BFunc), p1) assert p2[0] == p1 -def test_str(): +def test_string(): BChar = new_primitive_type("char") + assert string(cast(BChar, 42)) == '*' + assert string(cast(BChar, 0)) == '\x00' BCharP = new_pointer_type(BChar) BArray = new_array_type(BCharP, 10) a = newp(BArray, "hello") assert len(a) == 10 - assert str(a) == "hello" + assert string(a) == "hello" p = a + 2 - assert str(p) == "llo" - py.test.raises(RuntimeError, str, cast(BCharP, 0)) + assert string(p) == "llo" + assert string(newp(new_array_type(BCharP, 4), "abcd")) == "abcd" + py.test.raises(RuntimeError, string, cast(BCharP, 0)) + assert string(a, 4) == "hell" + assert string(a, 5) == "hello" + assert string(a, 6) == "hello" + +def test_string_byte(): + BByte = new_primitive_type("signed char") + assert string(cast(BByte, 42)) == '*' + assert string(cast(BByte, 0)) == '\x00' + BArray = new_array_type(new_pointer_type(BByte), None) + a = newp(BArray, [65, 66, 67]) + assert type(string(a)) is str and string(a) == 'ABC' + # + BByte = new_primitive_type("unsigned char") + assert string(cast(BByte, 42)) == '*' + assert string(cast(BByte, 0)) == '\x00' + BArray = new_array_type(new_pointer_type(BByte), None) + a = newp(BArray, [65, 66, 67]) + assert type(string(a)) is str and string(a) == 'ABC' + +def test_string_wchar(): + BWChar = new_primitive_type("wchar_t") + assert string(cast(BWChar, 42)) == u'*' + assert string(cast(BWChar, 0x4253)) == u'\u4253' + assert string(cast(BWChar, 0)) == u'\x00' + BArray = new_array_type(new_pointer_type(BWChar), None) + a = newp(BArray, [u'A', u'B', u'C']) + assert type(string(a)) is unicode and string(a) == u'ABC' + assert string(a, 10) == u'ABC' + +def test_string_typeerror(): + BShort = new_primitive_type("short") + BArray = new_array_type(new_pointer_type(BShort), None) + a = newp(BArray, [65, 66, 67]) + py.test.raises(TypeError, string, a) def test_bug_convert_to_ptr(): BChar = new_primitive_type("char") @@ -1229,12 +1272,12 @@ BStructPtr = new_pointer_type(BStruct) complete_struct_or_union(BStruct, [('a1', BCharArray10, -1)]) p = newp(BStructPtr, None) - assert str(p.a1) == '' + assert string(p.a1) == '' p.a1 = 'foo' - assert str(p.a1) == 'foo' + assert string(p.a1) == 'foo' assert list(p.a1) == ['f', 'o', 'o'] + ['\x00'] * 7 p.a1 = ['x', 'y'] - assert str(p.a1) == 'xyo' + assert string(p.a1) == 'xyo' def test_invalid_function_result_types(): BFunc = new_function_type((), new_void_type()) @@ -1364,7 +1407,7 @@ if wchar4: x = cast(BWChar, 0x12345) assert str(x) == "<cdata 'wchar_t' u'\U00012345'>" - assert unicode(x) == u'\U00012345' + assert int(x) == 0x12345 else: assert not pyuni4 # @@ -1395,20 +1438,20 @@ BWCharArray = new_array_type(BWCharP, None) a = newp(BWCharArray, u'hello \u1234 world') assert len(a) == 14 # including the final null - assert unicode(a) == u'hello \u1234 world' + assert string(a) == u'hello \u1234 world' a[13] = u'!' - assert unicode(a) == u'hello \u1234 world!' + assert string(a) == u'hello \u1234 world!' assert str(a) == repr(a) assert a[6] == u'\u1234' a[6] = u'-' - assert unicode(a) == 'hello - world!' + assert string(a) == u'hello - world!' assert str(a) == repr(a) # if wchar4: u = u'\U00012345\U00012346\U00012347' a = newp(BWCharArray, u) assert len(a) == 4 - assert unicode(a) == u + assert string(a) == u assert len(list(a)) == 4 expected = [u'\U00012345', u'\U00012346', u'\U00012347', unichr(0)] assert list(a) == expected @@ -1419,17 +1462,17 @@ w = cast(BWChar, 'a') assert repr(w) == "<cdata 'wchar_t' u'a'>" assert str(w) == repr(w) - assert unicode(w) == u'a' + assert string(w) == u'a' assert int(w) == ord('a') w = cast(BWChar, 0x1234) assert repr(w) == "<cdata 'wchar_t' u'\u1234'>" assert str(w) == repr(w) - assert unicode(w) == u'\u1234' + assert string(w) == u'\u1234' assert int(w) == 0x1234 w = cast(BWChar, u'\u8234') assert repr(w) == "<cdata 'wchar_t' u'\u8234'>" assert str(w) == repr(w) - assert unicode(w) == u'\u8234' + assert string(w) == u'\u8234' assert int(w) == 0x8234 w = cast(BInt, u'\u1234') assert repr(w) == "<cdata 'int' 4660>" @@ -1437,7 +1480,7 @@ w = cast(BWChar, u'\U00012345') assert repr(w) == "<cdata 'wchar_t' u'\U00012345'>" assert str(w) == repr(w) - assert unicode(w) == u'\U00012345' + assert string(w) == u'\U00012345' assert int(w) == 0x12345 w = cast(BInt, u'\U00012345') assert repr(w) == "<cdata 'int' 74565>" @@ -1447,23 +1490,23 @@ # a = newp(BWCharArray, u'hello - world') p = cast(BWCharP, a) - assert unicode(p) == u'hello - world' + assert string(p) == u'hello - world' p[6] = u'\u2345' - assert unicode(p) == u'hello \u2345 world' + assert string(p) == u'hello \u2345 world' # s = newp(BStructPtr, [u'\u1234', p]) assert s.a1 == u'\u1234' assert s.a2 == p assert str(s.a2) == repr(s.a2) - assert unicode(s.a2) == u'hello \u2345 world' + assert string(s.a2) == u'hello \u2345 world' # q = cast(BWCharP, 0) assert str(q) == repr(q) - py.test.raises(RuntimeError, unicode, q) + py.test.raises(RuntimeError, string, q) # def cb(p): assert repr(p).startswith("<cdata 'wchar_t *' 0x") - return len(unicode(p)) + return len(string(p)) BFunc = new_function_type((BWCharP,), BInt, False) f = callback(BFunc, cb, -42) assert f(u'a\u1234b') == 3 @@ -1471,9 +1514,9 @@ if wchar4 and not pyuni4: # try out-of-range wchar_t values x = cast(BWChar, 1114112) - py.test.raises(ValueError, unicode, x) + py.test.raises(ValueError, string, x) x = cast(BWChar, -1) - py.test.raises(ValueError, unicode, x) + py.test.raises(ValueError, string, x) def test_keepalive_struct(): # exception to the no-keepalive rule: p=newp(BStructPtr) returns a @@ -1585,12 +1628,12 @@ assert c[2] == '-' assert str(buf) == "hi-there\x00" buf[:2] = 'HI' - assert str(c) == 'HI-there' + assert string(c) == 'HI-there' assert buf[:4:2] == 'H-' if '__pypy__' not in sys.builtin_module_names: # XXX pypy doesn't support the following assignment so far buf[:4:2] = 'XY' - assert str(c) == 'XIYthere' + assert string(c) == 'XIYthere' def test_getcname(): BUChar = new_primitive_type("unsigned char") _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit