[pypy-svn] r74768 - in pypy/trunk/pypy/module/cpyext: . include test

afa at codespeak.net afa at codespeak.net
Wed May 26 11:54:19 CEST 2010


Author: afa
Date: Wed May 26 11:54:17 2010
New Revision: 74768

Modified:
   pypy/trunk/pypy/module/cpyext/include/sliceobject.h
   pypy/trunk/pypy/module/cpyext/sliceobject.py
   pypy/trunk/pypy/module/cpyext/stubs.py
   pypy/trunk/pypy/module/cpyext/test/test_sliceobject.py
Log:
add PySlice_New, and expose at C level the (readonly!) slice attributes.


Modified: pypy/trunk/pypy/module/cpyext/include/sliceobject.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/sliceobject.h	(original)
+++ pypy/trunk/pypy/module/cpyext/include/sliceobject.h	Wed May 26 11:54:17 2010
@@ -12,6 +12,9 @@
 
 typedef struct {
     PyObject_HEAD
+    PyObject *start;
+    PyObject *stop;
+    PyObject *step;
 } PySliceObject;
 
 #ifdef __cplusplus

Modified: pypy/trunk/pypy/module/cpyext/sliceobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/sliceobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/sliceobject.py	Wed May 26 11:54:17 2010
@@ -1,13 +1,63 @@
 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.api import (
+    cpython_api, cpython_struct, bootstrap_function, build_type_checkers,
+    CANNOT_FAIL, Py_ssize_t, Py_ssize_tP, PyObjectFields)
+from pypy.module.cpyext.pyobject import (
+    Py_DecRef, PyObject, make_ref, make_typedescr)
 from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
 from pypy.interpreter.error import OperationError
 from pypy.objspace.std.sliceobject import W_SliceObject
 
+# Slice objects directly expose their members as PyObject.
+# Don't change them!
+
+PySliceObjectStruct = lltype.ForwardReference()
+PySliceObject = lltype.Ptr(PySliceObjectStruct)
+PySliceObjectFields = PyObjectFields + \
+    (("start", PyObject), ("step", PyObject), ("stop", PyObject), )
+cpython_struct("PySliceObject", PySliceObjectFields, PySliceObjectStruct)
+
+ at bootstrap_function
+def init_sliceobject(space):
+    "Type description of PySliceObject"
+    make_typedescr(W_SliceObject.typedef,
+                   basestruct=PySliceObject.TO,
+                   attach=slice_attach,
+                   dealloc=slice_dealloc)
+
+def slice_attach(space, py_obj, w_obj):
+    """
+    Fills a newly allocated PySliceObject with the given slice object. The
+    fields must not be modified.
+    """
+    py_slice = rffi.cast(PySliceObject, py_obj)
+    assert isinstance(w_obj, W_SliceObject)
+    py_slice.c_start = make_ref(space, w_obj.w_start)
+    py_slice.c_stop = make_ref(space, w_obj.w_stop)
+    py_slice.c_step = make_ref(space, w_obj.w_step)
+
+ at cpython_api([PyObject], lltype.Void, external=False)
+def slice_dealloc(space, py_obj):
+    """Frees allocated PyStringObject resources.
+    """
+    py_slice = rffi.cast(PySliceObject, py_obj)
+    Py_DecRef(space, py_slice.c_start)
+    Py_DecRef(space, py_slice.c_stop)
+    Py_DecRef(space, py_slice.c_step)
+    from pypy.module.cpyext.object import PyObject_dealloc
+    PyObject_dealloc(space, py_obj)
+
 PySlice_Check, PySlice_CheckExact = build_type_checkers("Slice")
 
+ at cpython_api([PyObject, PyObject, PyObject], PyObject)
+def PySlice_New(space, w_start, w_stop, w_step):
+    """Return a new slice object with the given values.  The start, stop, and
+    step parameters are used as the values of the slice object attributes of
+    the same names.  Any of the values may be NULL, in which case the
+    None will be used for the corresponding attribute.  Return NULL if
+    the new object could not be allocated."""
+    return W_SliceObject(w_start, w_stop, w_step)
+
 @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, 

Modified: pypy/trunk/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubs.py	Wed May 26 11:54:17 2010
@@ -2631,15 +2631,6 @@
     """Empty an existing set of all elements."""
     raise NotImplementedError
 
- at cpython_api([PyObject, PyObject, PyObject], PyObject)
-def PySlice_New(space, start, stop, step):
-    """Return a new slice object with the given values.  The start, stop, and
-    step parameters are used as the values of the slice object attributes of
-    the same names.  Any of the values may be NULL, in which case the
-    None will be used for the corresponding attribute.  Return NULL if
-    the new object could not be allocated."""
-    raise NotImplementedError
-
 @cpython_api([PyObject, Py_ssize_t, Py_ssize_t, Py_ssize_t, Py_ssize_t], rffi.INT_real, error=-1)
 def PySlice_GetIndices(space, slice, length, start, stop, step):
     """Retrieve the start, stop and step indices from the slice object slice,

Modified: pypy/trunk/pypy/module/cpyext/test/test_sliceobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_sliceobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_sliceobject.py	Wed May 26 11:54:17 2010
@@ -25,3 +25,17 @@
             lltype.free(values, flavor='raw')
             return rv
         assert get_indices(w(10), w(20), w(1), 200) == (10, 20, 1, 10)
+
+class AppTestSliceMembers(AppTestCpythonExtensionBase):
+    def test_members(self):
+        module = self.import_extension('foo', [
+            ("clone", "METH_O",
+             """
+                 PySliceObject *slice = (PySliceObject *)args;
+                 return PySlice_New(slice->start,
+                                    slice->stop,
+                                    slice->step);
+             """),
+            ])
+        s = slice(10, 20, 30)
+        assert module.clone(s) == s



More information about the Pypy-commit mailing list