Skip to content

Commit

Permalink
Make __Pyx_PyObject_ToDouble() work in the Limited API (GH-5888)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-woods committed Dec 3, 2023
1 parent 59268a9 commit bcc6799
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Cython/Compiler/PyrexTypes.py
Expand Up @@ -2377,15 +2377,15 @@ class CFloatType(CNumericType):

is_float = 1
to_py_function = "PyFloat_FromDouble"
from_py_function = "__pyx_PyFloat_AsDouble"
from_py_function = "__Pyx_PyFloat_AsDouble"

exception_value = -1

def __init__(self, rank, math_h_modifier = ''):
CNumericType.__init__(self, rank, 1)
self.math_h_modifier = math_h_modifier
if rank == RANK_FLOAT:
self.from_py_function = "__pyx_PyFloat_AsFloat"
self.from_py_function = "__Pyx_PyFloat_AsFloat"

def assignable_from_resolved_type(self, src_type):
return (src_type.is_numeric and not src_type.is_complex) or src_type is error_type
Expand Down
28 changes: 6 additions & 22 deletions Cython/Utility/Optimize.c
Expand Up @@ -598,7 +598,7 @@ static double __Pyx__PyObject_AsDouble(PyObject* obj); /* proto */
PyFloat_AsDouble(obj) : __Pyx__PyObject_AsDouble(obj))
#else
#define __Pyx_PyObject_AsDouble(obj) \
((likely(PyFloat_CheckExact(obj))) ? PyFloat_AS_DOUBLE(obj) : \
((likely(PyFloat_CheckExact(obj))) ? __Pyx_PyFloat_AS_DOUBLE(obj) : \
likely(PyLong_CheckExact(obj)) ? \
PyLong_AsDouble(obj) : __Pyx__PyObject_AsDouble(obj))
#endif
Expand Down Expand Up @@ -639,7 +639,7 @@ static double __Pyx__PyObject_AsDouble(PyObject* obj) {
}
#endif
if (likely(float_value)) {
double value = PyFloat_AS_DOUBLE(float_value);
double value = __Pyx_PyFloat_AS_DOUBLE(float_value);
Py_DECREF(float_value);
return value;
}
Expand Down Expand Up @@ -845,11 +845,7 @@ static CYTHON_INLINE double __Pyx_PyByteArray_AsDouble(PyObject *obj) {
static double __Pyx_SlowPyString_AsDouble(PyObject *obj) {
PyObject *float_value = PyFloat_FromString(obj);
if (likely(float_value)) {
#if CYTHON_ASSUME_SAFE_MACROS
double value = PyFloat_AS_DOUBLE(float_value);
#else
double value = PyFloat_AsDouble(float_value);
#endif
double value = __Pyx_PyFloat_AS_DOUBLE(float_value);
Py_DECREF(float_value);
return value;
}
Expand Down Expand Up @@ -1096,11 +1092,7 @@ static CYTHON_INLINE {{c_ret_type}} __Pyx_PyInt_{{'' if ret_type.is_pyobject els

if (PyFloat_CheckExact({{pyval}})) {
const long {{'a' if order == 'CObj' else 'b'}} = intval;
#if CYTHON_COMPILING_IN_LIMITED_API
double {{ival}} = __pyx_PyFloat_AsDouble({{pyval}});
#else
double {{ival}} = PyFloat_AS_DOUBLE({{pyval}});
#endif
double {{ival}} = __Pyx_PyFloat_AS_DOUBLE({{pyval}});
{{return_compare('(double)a', '(double)b', c_op)}}
}

Expand Down Expand Up @@ -1322,11 +1314,7 @@ static {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2, long intval,
{{if c_op in '+-*' or op in ('TrueDivide', 'Eq', 'Ne')}}
if (PyFloat_CheckExact({{pyval}})) {
const long {{'a' if order == 'CObj' else 'b'}} = intval;
#if CYTHON_COMPILING_IN_LIMITED_API
double {{ival}} = __pyx_PyFloat_AsDouble({{pyval}});
#else
double {{ival}} = PyFloat_AS_DOUBLE({{pyval}});
#endif
double {{ival}} = __Pyx_PyFloat_AS_DOUBLE({{pyval}});
{{if op in ('Eq', 'Ne')}}
if ((double)a {{c_op}} (double)b) {
{{return_true}};
Expand Down Expand Up @@ -1402,11 +1390,7 @@ static {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2, double floatv
{{endif}}

if (likely(PyFloat_CheckExact({{pyval}}))) {
#if CYTHON_COMPILING_IN_LIMITED_API
{{fval}} = __pyx_PyFloat_AsDouble({{pyval}});
#else
{{fval}} = PyFloat_AS_DOUBLE({{pyval}});
#endif
{{fval}} = __Pyx_PyFloat_AS_DOUBLE({{pyval}});
{{zerodiv_check(fval)}}
} else

Expand Down
8 changes: 5 additions & 3 deletions Cython/Utility/TypeConversion.c
Expand Up @@ -123,11 +123,13 @@ static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);

#if CYTHON_ASSUME_SAFE_MACROS
#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
#define __Pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
#define __Pyx_PyFloat_AS_DOUBLE(x) PyFloat_AS_DOUBLE(x)
#else
#define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)
#define __Pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)
#define __Pyx_PyFloat_AS_DOUBLE(x) PyFloat_AsDouble(x)
#endif
#define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x))
#define __Pyx_PyFloat_AsFloat(x) ((float) __Pyx_PyFloat_AsDouble(x))

#define __Pyx_PyNumber_Int(x) (PyLong_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Long(x))
// __Pyx_PyNumber_Float is now in its own section since it has dependencies (needed to make
Expand Down

0 comments on commit bcc6799

Please sign in to comment.