[pypy-commit] pypy cpyext-ext: refactor error checking to make sure the strategy is correct

mattip pypy.commits at gmail.com
Fri Apr 1 10:24:52 EDT 2016


Author: mattip <matti.picus at gmail.com>
Branch: cpyext-ext
Changeset: r83493:eab8aff36a9f
Date: 2016-03-31 19:29 +0300
http://bitbucket.org/pypy/pypy/changeset/eab8aff36a9f/

Log:	refactor error checking to make sure the strategy is correct

diff --git a/pypy/module/cpyext/sequence.py b/pypy/module/cpyext/sequence.py
--- a/pypy/module/cpyext/sequence.py
+++ b/pypy/module/cpyext/sequence.py
@@ -59,11 +59,11 @@
     PySequence_Fast(), o is not NULL, and that i is within bounds.
     """
     if isinstance(w_obj, listobject.W_ListObject):
-        w_res = w_obj.getitem(index)
-    else:
-        assert isinstance(w_obj, tupleobject.W_TupleObject)
-        w_res = w_obj.wrappeditems[index]
-    return w_res     # borrowed ref
+        return w_obj.getitem(index)
+    elif isinstance(w_obj, tupleobject.W_TupleObject):
+        return w_obj.wrappeditems[index]
+    raise OperationError(space.w_TypeError, space.wrap(
+        'PySequence_Fast_GET_ITEM called but object is not a list or sequence'))
 
 @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL)
 def PySequence_Fast_GET_SIZE(space, w_obj):
@@ -74,8 +74,10 @@
     or tuple."""
     if isinstance(w_obj, listobject.W_ListObject):
         return w_obj.length()
-    assert isinstance(w_obj, tupleobject.W_TupleObject)
-    return len(w_obj.wrappeditems)
+    elif isinstance(w_obj, tupleobject.W_TupleObject):
+        return len(w_obj.wrappeditems)
+    raise OperationError(space.w_TypeError, space.wrap( 
+        'PySequence_Fast_GET_SIZE called but object is not a list or sequence'))
 
 @cpython_api([PyObject], PyObjectP)
 def PySequence_Fast_ITEMS(space, w_obj):
@@ -86,8 +88,12 @@
     So, only use the underlying array pointer in contexts where the sequence
     cannot change.
     """
-    assert isinstance(w_obj, listobject.W_ListObject)
-    return w_obj.get_raw_items() # asserts it's a cpyext strategy
+    if isinstance(w_obj, listobject.W_ListObject):
+        cpy_strategy = space.fromcache(CPyListStrategy)
+        if w_obj.strategy is not cpy_strategy:
+            raise OperationError(space.w_TypeError, space.wrap( 
+                'PySequence_Fast_ITEMS called but object is not the result of PySequence_Fast'))
+        return w_obj.get_raw_items() # asserts it's a cpyext strategy
 
 @cpython_api([PyObject, Py_ssize_t, Py_ssize_t], PyObject)
 def PySequence_GetSlice(space, w_obj, start, end):


More information about the pypy-commit mailing list