[pypy-svn] r73550 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

jandem at codespeak.net jandem at codespeak.net
Thu Apr 8 16:02:52 CEST 2010


Author: jandem
Date: Thu Apr  8 16:02:50 2010
New Revision: 73550

Added:
   pypy/branch/cpython-extension/pypy/module/cpyext/sliceobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_sliceobject.py
Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py
   pypy/branch/cpython-extension/pypy/module/cpyext/api.py
Log:
Add sliceobject and PySlice_GetIndicesEx


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py	Thu Apr  8 16:02:50 2010
@@ -55,6 +55,7 @@
 import pypy.module.cpyext.pycobject
 import pypy.module.cpyext.sysmodule
 import pypy.module.cpyext.number
+import pypy.module.cpyext.sliceobject
 
 # now that all rffi_platform.Struct types are registered, configure them
 api.configure_types()

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/api.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/api.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/api.py	Thu Apr  8 16:02:50 2010
@@ -32,6 +32,7 @@
 
 # update these for other platforms
 Py_ssize_t = lltype.Signed
+Py_ssize_tP = lltype.Ptr(lltype.Array(Py_ssize_t, hints={'nolength': True}))
 size_t = rffi.ULONG
 ADDR = lltype.Signed
 
@@ -590,7 +591,8 @@
                                source_dir / "getargs.c",
                                source_dir / "stringobject.c",
                                source_dir / "mysnprintf.c",
-                               source_dir / "pythonrun.c"],
+                               source_dir / "pythonrun.c",
+                               ],
         separate_module_sources = [code],
         export_symbols=export_symbols_eci,
         **kwds

Added: pypy/branch/cpython-extension/pypy/module/cpyext/sliceobject.py
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/sliceobject.py	Thu Apr  8 16:02:50 2010
@@ -0,0 +1,27 @@
+from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL, Py_ssize_t,\
+                                    Py_ssize_tP, build_type_checkers
+from pypy.module.cpyext.pyobject import Py_DecRef, PyObject
+from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
+from pypy.interpreter.error import OperationError
+from pypy.objspace.std.sliceobject import W_SliceObject
+
+PySlice_Check, PySlice_CheckExact = build_type_checkers("Slice")
+
+ at cpython_api([PyObject, Py_ssize_t, Py_ssize_tP, Py_ssize_tP, Py_ssize_tP, 
+                Py_ssize_tP], rffi.INT_real, error=-1)
+def PySlice_GetIndicesEx(space, w_slice, length, start_p, stop_p, 
+        step_p, slicelength_p):
+    """Usable replacement for PySlice_GetIndices().  Retrieve the start,
+    stop, and step indices from the slice object slice assuming a sequence of
+    length length, and store the length of the slice in slicelength.  Out
+    of bounds indices are clipped in a manner consistent with the handling of
+    normal slices.
+    
+    Returns 0 on success and -1 on error with exception set."""
+    if not PySlice_Check(space, w_slice):
+        PyErr_BadInternalCall(space)
+    assert isinstance(w_slice, W_SliceObject)
+    start_p[0], stop_p[0], step_p[0], slicelength_p[0] = \
+            w_slice.indices4(space, length)
+    return 0

Added: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_sliceobject.py
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_sliceobject.py	Thu Apr  8 16:02:50 2010
@@ -0,0 +1,31 @@
+from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+from pypy.module.cpyext.api import Py_ssize_t, Py_ssize_tP
+
+class TestSliceObject(BaseApiTest):
+    def test_slice(self, space, api):
+        w_i = space.wrap(10)
+        w_slice = space.newslice(w_i, w_i, w_i)
+        assert api.PySlice_Check(w_slice)
+        assert not api.PySlice_Check(w_i)
+
+    def test_GetIndicesEx(self, space, api):
+        
+        w = space.wrap
+        
+        def get_indices(w_start, w_stop, w_step, length):
+            w_slice = space.newslice(w_start, w_stop, w_step)
+            values = lltype.malloc(Py_ssize_tP.TO, 4, flavor='raw')
+            
+            res = api.PySlice_GetIndicesEx(w_slice, 100, values, 
+                rffi.ptradd(values, 1), 
+                rffi.ptradd(values, 2), 
+                rffi.ptradd(values, 3))
+            assert res == 0
+            
+            rv = values[0], values[1], values[2], values[3]
+            lltype.free(values, flavor='raw')
+            return rv
+        
+        assert get_indices(w(10), w(20), w(1), 200) == (10, 20, 1, 10)



More information about the Pypy-commit mailing list