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

afa at codespeak.net afa at codespeak.net
Sat May 22 00:29:50 CEST 2010


Author: afa
Date: Sat May 22 00:29:47 2010
New Revision: 74662

Modified:
   pypy/trunk/pypy/module/cpyext/eval.py
   pypy/trunk/pypy/module/cpyext/test/test_eval.py
Log:
add _PyEval_SliceIndex(), undocumented but still used by Boost.Python...
It's the first time I see this function.


Modified: pypy/trunk/pypy/module/cpyext/eval.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/eval.py	(original)
+++ pypy/trunk/pypy/module/cpyext/eval.py	Sat May 22 00:29:47 2010
@@ -1,7 +1,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import (
-    cpython_api, CANNOT_FAIL, CONST_STRING, FILEP, fread, feof)
+    cpython_api, CANNOT_FAIL, CONST_STRING, FILEP, fread, feof, Py_ssize_tP)
 from pypy.module.cpyext.pyobject import PyObject, borrow_from
 from pypy.module.cpyext.pyerrors import PyErr_SetFromErrno
 from pypy.module.__builtin__ import compiling
@@ -111,3 +111,21 @@
         lltype.free(buf, flavor='raw')
     return run_string(space, source, filename, start, w_globals, w_locals)
 
+# Undocumented function!
+ at cpython_api([PyObject, Py_ssize_tP], rffi.INT_real, error=0)
+def _PyEval_SliceIndex(space, w_obj, pi):
+    """Extract a slice index from a PyInt or PyLong or an object with the
+    nb_index slot defined, and store in *pi.
+    Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
+    and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
+
+    Return 0 on error, 1 on success.
+
+    Note:  If v is NULL, return success without storing into *pi.  This
+    is because_PyEval_SliceIndex() is called by apply_slice(), which can be
+    called by the SLICE opcode with v and/or w equal to NULL.
+    """
+    if w_obj is not None:
+        pi[0] = space.getindex_w(w_obj, None)
+    return 1
+

Modified: pypy/trunk/pypy/module/cpyext/test/test_eval.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_eval.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_eval.py	Sat May 22 00:29:47 2010
@@ -3,9 +3,10 @@
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.eval import (
     Py_single_input, Py_file_input, Py_eval_input)
-from pypy.module.cpyext.api import fopen, fclose
+from pypy.module.cpyext.api import fopen, fclose, Py_ssize_tP
 from pypy.interpreter.gateway import interp2app
 from pypy.tool.udir import udir
+import sys
 
 class TestEval(BaseApiTest):
     def test_eval(self, space, api):
@@ -134,6 +135,18 @@
         assert sorted(locals) == ['cpyvars', 'x']
         assert sorted(globals) == ['__builtins__', 'anonymous', 'y']
 
+    def test_sliceindex(self, space, api):
+        pi = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')
+        assert api._PyEval_SliceIndex(space.w_None, pi) == 0
+        api.PyErr_Clear()
+
+        assert api._PyEval_SliceIndex(space.wrap(123), pi) == 1
+        assert pi[0] == 123
+
+        assert api._PyEval_SliceIndex(space.wrap(1 << 66), pi) == 1
+        assert pi[0] == sys.maxint
+
+        lltype.free(pi, flavor='raw')
 
 class AppTestCall(AppTestCpythonExtensionBase):
     def test_CallFunction(self):



More information about the Pypy-commit mailing list