[Cython] [cython-users] Re: Bringing Cython and PyPy closer together

Lisandro Dalcin dalcinl at gmail.com
Sun Mar 29 14:52:26 CEST 2015


On 29 March 2015 at 14:16, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Lisandro Dalcin schrieb am 29.03.2015 um 12:23:
>> One thing that Cython developers really need is PyPy defining a macro
>> such as PYPY_VERSION_HEX in such a way us we can properly use
>> conditional compilation. For example, a few days ago I was pushing
>> PyPy fixes to Cython. I tried to use _PyLong_Sign in my patch, but the
>> interpreter broke at runtime. This issue will be eventually fixed, I
>> hope. Unce that happens, how can we know it is save to use the call
>> for that pypy version and upwards? I mean, Cython should still support
>> previous PyPy releases...
>
> Yes, it's unfortunate that cpyext isn't versioned. But PY_VERSION_HEX
> should still change sometimes over PyPy releases, so that might at least
> help a bit.
>

Well, you know that's not enough ;-)

> In general, however, you shouldn't expect any CPython internals to work in
> PyPy. Avoiding multiple C-API calls when a generic one exists is really the
> best of all strategies. For example, why read just the sign, when you can
> read the whole value? Everything else just risks aggregating slowness and
> running into buggy corners.
>

Well, I agree, however I do require _PyLong_Sign to work if I want to
report correct overflow error messages when converting PyLong values
to a C integral type. In CPython, we are using Py_SIZE for that, not
only for speed, but also to figure out the right overflow kind when
converting to unsigned integers.

> That being said, if you find anything that can be optimised, I'm happy to
> learn about your changes.
>

Again, this is not about optimization, but proper error reporting.

diff --git a/Cython/Utility/TypeConversion.c b/Cython/Utility/TypeConversion.c
index 4cd8229..fbac649 100644
--- a/Cython/Utility/TypeConversion.c
+++ b/Cython/Utility/TypeConversion.c
@@ -560,6 +560,10 @@ static CYTHON_INLINE {{TYPE}}
{{FROM_PY_FUNCTION}}(PyObject *x) {
             if (unlikely(Py_SIZE(x) < 0)) {
                 goto raise_neg_overflow;
             }
+#elif CYTHON_COMPILING_IN_PYPY
+            if (unlikely(_PyLong_Sign(x) < 0)) {
+                goto raise_neg_overflow;
+            }
 #endif
             if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
                 __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long,
PyLong_AsUnsignedLong(x))



-- 
Lisandro Dalcin
============
Research Scientist
Computer, Electrical and Mathematical Sciences & Engineering (CEMSE)
Numerical Porous Media Center (NumPor)
King Abdullah University of Science and Technology (KAUST)
http://numpor.kaust.edu.sa/

4700 King Abdullah University of Science and Technology
al-Khawarizmi Bldg (Bldg 1), Office # 4332
Thuwal 23955-6900, Kingdom of Saudi Arabia
http://www.kaust.edu.sa

Office Phone: +966 12 808-0459


More information about the cython-devel mailing list