Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: py3.5
Changeset: r90041:7d3aa9ec294f
Date: 2017-02-10 20:19 +0000
http://bitbucket.org/pypy/pypy/changeset/7d3aa9ec294f/

Log:    Test and fix for PyLong_AsLong

diff --git a/pypy/module/cpyext/longobject.py b/pypy/module/cpyext/longobject.py
--- a/pypy/module/cpyext/longobject.py
+++ b/pypy/module/cpyext/longobject.py
@@ -73,7 +73,7 @@
     Return a C long representation of the contents of pylong.  If
     pylong is greater than LONG_MAX, an OverflowError is raised
     and -1 will be returned."""
-    return space.int_w(w_long)
+    return space.int_w(space.int(w_long))
 
 @cpython_api([PyObject], Py_ssize_t, error=-1)
 def PyLong_AsSsize_t(space, w_long):
diff --git a/pypy/module/cpyext/test/test_longobject.py 
b/pypy/module/cpyext/test/test_longobject.py
--- a/pypy/module/cpyext/test/test_longobject.py
+++ b/pypy/module/cpyext/test/test_longobject.py
@@ -269,6 +269,20 @@
         # A string with arabic digits. 'BAD' is after the 6th character.
         assert module.from_unicode(u'  1\u0662\u0663\u0664BAD') == (1234, 4660)
 
+    def test_aslong(self):
+        module = self.import_extension('foo', [
+            ("as_long", "METH_O",
+             """
+                long n = PyLong_AsLong(args);
+                if (n == -1 && PyErr_Occurred()) {
+                    return NULL;
+                }
+                return PyLong_FromLong(n);
+             """)])
+        assert module.as_long(123) == 123
+        assert module.as_long(-1) == -1
+        assert module.as_long(1.23) == 1
+
     def test_strtol(self):
         module = self.import_extension('foo', [
             ("from_str", "METH_NOARGS",
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to