[pypy-svn] r78845 - in pypy/branch/fast-forward/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Sun Nov 7 23:01:21 CET 2010


Author: afa
Date: Sun Nov  7 23:01:18 2010
New Revision: 78845

Modified:
   pypy/branch/fast-forward/pypy/module/cpyext/longobject.py
   pypy/branch/fast-forward/pypy/module/cpyext/test/test_longobject.py
Log:
Add _PyLong_NumBits, _PyLong_Sign


Modified: pypy/branch/fast-forward/pypy/module/cpyext/longobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/longobject.py	(original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/longobject.py	Sun Nov  7 23:01:18 2010
@@ -1,6 +1,6 @@
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.module.cpyext.api import (cpython_api, PyObject, build_type_checkers,
-                                    CONST_STRING, ADDR)
+                                    CONST_STRING, ADDR, CANNOT_FAIL)
 from pypy.objspace.std.longobject import W_LongObject
 from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.intobject import PyInt_AsUnsignedLongMask
@@ -159,3 +159,13 @@
     For values outside 0..LONG_MAX, both signed and unsigned integers are accepted."""
     return rffi.cast(rffi.VOIDP_real, space.uint_w(w_long))
 
+ at cpython_api([PyObject], rffi.SIZE_T, error=-1)
+def _PyLong_NumBits(space, w_long):
+    return space.uint_w(space.call_method(w_long, "bit_length"))
+
+ at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def _PyLong_Sign(space, w_long):
+    assert isinstance(w_long, W_LongObject)
+    return w_long.num.sign
+
+

Modified: pypy/branch/fast-forward/pypy/module/cpyext/test/test_longobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/test/test_longobject.py	(original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/test/test_longobject.py	Sun Nov  7 23:01:18 2010
@@ -93,6 +93,19 @@
         assert space.unwrap(w_l) == 0L
         assert api.PyLong_AsVoidPtr(w_l) == lltype.nullptr(rffi.VOIDP_real.TO)
 
+    def test_sign_and_bits(self, space, api):
+        assert api._PyLong_Sign(space.wrap(0L)) == 0
+        assert api._PyLong_Sign(space.wrap(2L)) == 1
+        assert api._PyLong_Sign(space.wrap(-2L)) == -1
+
+        assert api._PyLong_NumBits(space.wrap(0)) == 0
+        assert api._PyLong_NumBits(space.wrap(1)) == 1
+        assert api._PyLong_NumBits(space.wrap(-1)) == 1
+        assert api._PyLong_NumBits(space.wrap(2)) == 2
+        assert api._PyLong_NumBits(space.wrap(-2)) == 2
+        assert api._PyLong_NumBits(space.wrap(3)) == 2
+        assert api._PyLong_NumBits(space.wrap(-3)) == 2
+
 class AppTestLongObject(AppTestCpythonExtensionBase):
     def test_fromunsignedlong(self):
         module = self.import_extension('foo', [



More information about the Pypy-commit mailing list