Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r57112:175a3fda455a
Date: 2012-09-03 22:36 +0200
http://bitbucket.org/pypy/pypy/changeset/175a3fda455a/
Log: Adapt _cffi_backend to python3.
diff --git a/pypy/module/_cffi_backend/cbuffer.py
b/pypy/module/_cffi_backend/cbuffer.py
--- a/pypy/module/_cffi_backend/cbuffer.py
+++ b/pypy/module/_cffi_backend/cbuffer.py
@@ -3,6 +3,7 @@
from pypy.interpreter.gateway import unwrap_spec
from pypy.rpython.lltypesystem import rffi
from pypy.module._cffi_backend import cdataobj, ctypeptr, ctypearray
+from pypy.module.__builtin__.interp_memoryview import W_MemoryView
class LLBuffer(RWBuffer):
@@ -52,4 +53,5 @@
raise operationerrfmt(space.w_TypeError,
"don't know the size pointed to by '%s'",
ctype.name)
- return space.wrap(LLBuffer(cdata._cdata, size))
+ buffer = LLBuffer(cdata._cdata, size)
+ return space.wrap(W_MemoryView(buffer))
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
@@ -51,7 +51,7 @@
return self.space.wrap("<cdata '%s%s' %s>" % (
self.ctype.name, extra1, extra2))
- def nonzero(self):
+ def bool(self):
return self.space.wrap(bool(self._cdata))
def int(self):
@@ -284,7 +284,7 @@
'CData',
__module__ = '_cffi_backend',
__repr__ = interp2app(W_CData.repr),
- __nonzero__ = interp2app(W_CData.nonzero),
+ __bool__ = interp2app(W_CData.bool),
__int__ = interp2app(W_CData.int),
__long__ = interp2app(W_CData.long),
__float__ = interp2app(W_CData.float),
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
@@ -37,8 +37,9 @@
if (space.isinstance_w(w_init, space.w_list) or
space.isinstance_w(w_init, space.w_tuple)):
length = space.int_w(space.len(w_init))
- elif space.isinstance_w(w_init, space.w_basestring):
- # from a string, we add the null terminator
+ elif (space.isinstance_w(w_init, space.w_unicode) or
+ space.isinstance_w(w_init, space.w_bytes)):
+ # from a string, we add the null terminatorc
length = space.int_w(space.len(w_init)) + 1
else:
length = space.getindex_w(w_init, space.w_OverflowError)
@@ -123,6 +124,6 @@
'CDataIter',
__module__ = '_cffi_backend',
__iter__ = interp2app(W_CDataIter.iter_w),
- next = interp2app(W_CDataIter.next_w),
+ __next__ = interp2app(W_CDataIter.next_w),
)
W_CDataIter.typedef.acceptable_as_base_class = False
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
@@ -60,14 +60,14 @@
except OperationError, e:
if not e.match(space, space.w_TypeError):
raise
- if space.isinstance_w(w_ob, space.w_str):
+ if space.isinstance_w(w_ob, space.w_unicode):
value = self.convert_enum_string_to_int(space.str_w(w_ob))
value = r_ulonglong(value)
misc.write_raw_integer_data(cdata, value, self.size)
else:
raise self._convert_error("str or int", w_ob)
- def cast_str(self, w_ob):
+ def cast_unicode(self, w_ob):
space = self.space
return self.convert_enum_string_to_int(space.str_w(w_ob))
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
@@ -75,7 +75,7 @@
if self.size == 1:
s = cdataobj._cdata[0]
keepalive_until_here(cdataobj)
- return self.space.wrap(s)
+ return self.space.wrapbytes(s)
return W_CType.string(self, cdataobj, maxlen)
@@ -96,12 +96,12 @@
return self.space.wrap(ord(cdata[0]))
def convert_to_object(self, cdata):
- return self.space.wrap(cdata[0])
+ return self.space.wrapbytes(cdata[0])
def _convert_to_char(self, w_ob):
space = self.space
- if space.isinstance_w(w_ob, space.w_str):
- s = space.str_w(w_ob)
+ if space.isinstance_w(w_ob, space.w_bytes):
+ s = space.bytes_w(w_ob)
if len(s) == 1:
return s[0]
ob = space.interpclass_w(w_ob)
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
@@ -129,7 +129,7 @@
else:
s = rffi.charp2strn(cdata, length)
keepalive_until_here(cdataobj)
- return space.wrap(s)
+ return space.wrapbytes(s)
#
# pointer to a wchar_t: builds and returns a unicode
if self.is_unichar_ptr_or_array():
@@ -238,7 +238,8 @@
if (space.isinstance_w(w_init, space.w_list) or
space.isinstance_w(w_init, space.w_tuple)):
length = space.int_w(space.len(w_init))
- elif space.isinstance_w(w_init, space.w_basestring):
+ elif (space.isinstance_w(w_init, space.w_unicode) or
+ space.isinstance_w(w_init, space.w_bytes)):
# from a string, we add the null terminator
length = space.int_w(space.len(w_init)) + 1
else:
diff --git a/pypy/module/_cffi_backend/misc.py
b/pypy/module/_cffi_backend/misc.py
--- a/pypy/module/_cffi_backend/misc.py
+++ b/pypy/module/_cffi_backend/misc.py
@@ -125,8 +125,14 @@
# (possibly) convert and cast a Python object to a long long.
# This version accepts a Python int too, and does convertions from
# other types of objects. It refuses floats.
- if space.is_w(space.type(w_ob), space.w_int): # shortcut
- return space.int_w(w_ob)
+ try:
+ value = space.int_w(w_ob)
+ except OperationError, e:
+ if not (e.match(space, space.w_OverflowError) or
+ e.match(space, space.w_TypeError)):
+ raise
+ else:
+ return value
try:
bigint = space.bigint_w(w_ob)
except OperationError, e:
@@ -145,8 +151,13 @@
# This accepts a Python int too, and does convertions from other types of
# objects. If 'strict', complains with OverflowError; if 'not strict',
# mask the result and round floats.
- if space.is_w(space.type(w_ob), space.w_int): # shortcut
+ try:
value = space.int_w(w_ob)
+ except OperationError, e:
+ if not (e.match(space, space.w_OverflowError) or
+ e.match(space, space.w_TypeError)):
+ raise
+ else:
if strict and value < 0:
raise OperationError(space.w_OverflowError, space.wrap(neg_msg))
return r_ulonglong(value)
diff --git a/pypy/module/_cffi_backend/test/test_c.py
b/pypy/module/_cffi_backend/test/test_c.py
--- a/pypy/module/_cffi_backend/test/test_c.py
+++ b/pypy/module/_cffi_backend/test/test_c.py
@@ -16,7 +16,6 @@
5. make the test pass in pypy ('py.test test_c.py')
"""
import py, sys, ctypes
-py.test.py3k_skip('not yet supported')
if sys.version_info < (2, 6):
py.test.skip("requires the b'' literal syntax")
diff --git a/pypy/module/_cffi_backend/test/test_ztranslation.py
b/pypy/module/_cffi_backend/test/test_ztranslation.py
--- a/pypy/module/_cffi_backend/test/test_ztranslation.py
+++ b/pypy/module/_cffi_backend/test/test_ztranslation.py
@@ -3,7 +3,5 @@
# side-effect: FORMAT_LONGDOUBLE must be built before test_checkmodule()
from pypy.module._cffi_backend import misc
-import py
def test_checkmodule():
- py.test.py3k_skip('not yet supported')
checkmodule('_cffi_backend')
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit