Skip to content

Commit

Permalink
Replace PyInt functions with PyLong (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Jan 30, 2024
2 parents f030c54 + 98d015e commit a7720b2
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 78 deletions.
17 changes: 7 additions & 10 deletions SWIG/calendars.i
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,15 @@ enum JointCalendarRule { JoinHolidays, JoinBusinessDays };

#if defined(SWIGPYTHON)
%typemap(in) ext::optional<BusinessDayConvention> %{
if($input == Py_None)
$1 = ext::nullopt;
else if (PyInt_Check($input))
$1 = (BusinessDayConvention) PyInt_AsLong($input);
else
$1 = (BusinessDayConvention) PyLong_AsLong($input);
if ($input == Py_None)
$1 = ext::nullopt;
else if (PyLong_Check($input))
$1 = (BusinessDayConvention)PyLong_AsLong($input);
else
SWIG_exception(SWIG_TypeError, "int expected");
%}
%typecheck (QL_TYPECHECK_BUSINESSDAYCONVENTION) ext::optional<BusinessDayConvention> {
if (PyInt_Check($input) || PyLong_Check($input) || Py_None == $input)
$1 = 1;
else
$1 = 0;
$1 = (PyLong_Check($input) || $input == Py_None) ? 1 : 0;
}
#endif

Expand Down
17 changes: 7 additions & 10 deletions SWIG/date.i
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,15 @@ enum Frequency {
#if defined(SWIGPYTHON)
%define QL_TYPECHECK_FREQUENCY 6210 %enddef
%typemap(in) ext::optional<Frequency> %{
if($input == Py_None)
$1 = ext::nullopt;
else if (PyInt_Check($input))
$1 = (Frequency) PyInt_AsLong($input);
else
$1 = (Frequency) PyLong_AsLong($input);
if ($input == Py_None)
$1 = ext::nullopt;
else if (PyLong_Check($input))
$1 = (Frequency)PyLong_AsLong($input);
else
SWIG_exception(SWIG_TypeError, "int expected");
%}
%typecheck (QL_TYPECHECK_FREQUENCY) ext::optional<Frequency> {
if (PyInt_Check($input) || PyLong_Check($input) || Py_None == $input)
$1 = 1;
else
$1 = 0;
$1 = (PyLong_Check($input) || $input == Py_None) ? 1 : 0;
}
#else
#if defined(SWIGCSHARP)
Expand Down
35 changes: 11 additions & 24 deletions SWIG/fdm.i
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,9 @@ class FdmLinearOpCompositeProxy : public FdmLinearOpComposite {

QL_ENSURE(pyResult != NULL,
"failed to call size() on Python object");
QL_ENSURE(PyLong_Check(pyResult), "size() is not an int");

Size result = PyInt_AsLong(pyResult);
Size result = PyLong_AsLong(pyResult);
Py_XDECREF(pyResult);

return result;
Expand Down Expand Up @@ -476,21 +477,14 @@ class FdmLinearOpCompositeProxy : public FdmLinearOpComposite {
}

private:
Array apply(const Array& r, const std::string& methodName) const {
Array apply(const Array& r, const char* methodName) const {
PyObject* pyArray = SWIG_NewPointerObj(
SWIG_as_voidptr(&r), SWIGTYPE_p_Array, 0);

#if !defined(PY_VERSION_HEX) || PY_VERSION_HEX < 0x03040000
std::vector<char> cstr(
methodName.c_str(), methodName.c_str() + methodName.size() + 1);
PyObject* pyResult
= PyObject_CallMethod(callback_, &cstr[0], "O", pyArray);
#else

PyObject* pyResult
= PyObject_CallMethod(callback_, methodName.c_str(), "O", pyArray);
#endif
Py_XDECREF(pyArray);

= PyObject_CallMethod(callback_, methodName, "O", pyArray);

Py_DECREF(pyArray);
return extractArray(pyResult, methodName);
}

Expand Down Expand Up @@ -1251,21 +1245,14 @@ class FdmInnerValueCalculatorProxy : public FdmInnerValueCalculator {
}

private:
Real getValue(const FdmLinearOpIterator& iter, Time t, const std::string& methodName) {
Real getValue(const FdmLinearOpIterator& iter, Time t, const char* methodName) {
PyObject* pyIter = SWIG_NewPointerObj(
SWIG_as_voidptr(&iter), SWIGTYPE_p_FdmLinearOpIterator, 0);

#if !defined(PY_VERSION_HEX) || PY_VERSION_HEX < 0x03040000
std::vector<char> cstr(
methodName.c_str(), methodName.c_str() + methodName.size() + 1);
PyObject* pyResult
= PyObject_CallMethod(callback_, &cstr[0], "Od",pyIter, t);
#else
PyObject* pyResult
= PyObject_CallMethod(callback_, methodName.c_str(), "Od", pyIter, t);
#endif

Py_XDECREF(pyIter);
= PyObject_CallMethod(callback_, methodName, "Od", pyIter, t);

Py_DECREF(pyIter);

QL_ENSURE(pyResult != NULL, "failed to call innerValue function on Python object");

Expand Down
12 changes: 6 additions & 6 deletions SWIG/linearalgebra.i
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ bool ArrayFromSequence(PyObject* source, Array* target) {
if (PyFloat_Check(o)) {
(*target)[i] = PyFloat_AsDouble(o);
Py_DECREF(o);
} else if (PyInt_Check(o)) {
(*target)[i] = Real(PyInt_AsLong(o));
} else if (PyLong_Check(o)) {
(*target)[i] = Real(PyLong_AsLong(o));
Py_DECREF(o);
} else {
Py_DECREF(o);
Expand Down Expand Up @@ -176,8 +176,8 @@ bool ArrayFromSequence(PyObject* source, Array* target) {
if (PyFloat_Check(d)) {
$1[i][j] = PyFloat_AsDouble(d);
Py_DECREF(d);
} else if (PyInt_Check(d)) {
$1[i][j] = Real(PyInt_AsLong(d));
} else if (PyLong_Check(d)) {
$1[i][j] = Real(PyLong_AsLong(d));
Py_DECREF(d);
} else {
PyErr_SetString(PyExc_TypeError,"doubles expected");
Expand Down Expand Up @@ -247,8 +247,8 @@ bool ArrayFromSequence(PyObject* source, Array* target) {
if (PyFloat_Check(d)) {
temp[i][j] = PyFloat_AsDouble(d);
Py_DECREF(d);
} else if (PyInt_Check(d)) {
temp[i][j] = Real(PyInt_AsLong(d));
} else if (PyLong_Check(d)) {
temp[i][j] = Real(PyLong_AsLong(d));
Py_DECREF(d);
} else {
PyErr_SetString(PyExc_TypeError,"doubles expected");
Expand Down
10 changes: 5 additions & 5 deletions SWIG/null.i
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ double nullDouble() { return Null<double>(); }
%typemap(in) intOrNull {
if ($input == Py_None)
$1 = Null<int>();
else if (PyInt_Check($input))
$1 = int(PyInt_AsLong($input));
else if (PyLong_Check($input))
$1 = int(PyLong_AsLong($input));
else
SWIG_exception(SWIG_TypeError,"int expected");
SWIG_exception(SWIG_TypeError, "int expected");
}
%typecheck(SWIG_TYPECHECK_INTEGER) intOrNull {
$1 = ($input == Py_None || PyInt_Check($input)) ? 1 : 0;
$1 = ($input == Py_None || PyLong_Check($input)) ? 1 : 0;
}
%typemap(out) intOrNull {
if ($1 == Null<int>()) {
Py_INCREF(Py_None);
$result = Py_None;
} else {
$result = PyInt_FromLong(long($1));
$result = PyLong_FromLong($1);
}
}

Expand Down
13 changes: 5 additions & 8 deletions SWIG/scheduler.i
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,15 @@ struct DateGeneration {
};
#if defined(SWIGPYTHON)
%typemap(in) ext::optional<DateGeneration::Rule> %{
if($input == Py_None)
if ($input == Py_None)
$1 = ext::nullopt;
else if (PyInt_Check($input))
$1 = (DateGeneration::Rule) PyInt_AsLong($input);
else if (PyLong_Check($input))
$1 = (DateGeneration::Rule)PyLong_AsLong($input);
else
$1 = (DateGeneration::Rule) PyLong_AsLong($input);
SWIG_exception(SWIG_TypeError, "int expected");
%}
%typecheck (QL_TYPECHECK_DATEGENERATION) ext::optional<DateGeneration::Rule> {
if (PyInt_Check($input) || PyLong_Check($input) || Py_None == $input)
$1 = 1;
else
$1 = 0;
$1 = (PyLong_Check($input) || $input == Py_None) ? 1 : 0;
}
#endif

Expand Down
7 changes: 1 addition & 6 deletions SWIG/timebasket.i
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class TimeBasket {
return self->hasDate(d);
}
PyObject* __iter__() {
%#if PY_VERSION_HEX >= 0x02020000
PyObject* keyList = PyList_New(self->size());
TimeBasket::iterator i;
Size j;
Expand All @@ -77,11 +76,7 @@ class TimeBasket {
PyObject* iter = PyObject_GetIter(keyList);
Py_DECREF(keyList);
return iter;
%#else
throw std::runtime_error("Python 2.2 or later is needed"
" for iterator support");
%#endif
}
}
#endif
}
};
Expand Down
2 changes: 1 addition & 1 deletion SWIG/types.i
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ typedef Real Probability;
#if defined(SWIGPYTHON)
// needed for those using SWIG 1.3.21 in order to compile with VC++6
%typecheck(SWIG_TYPECHECK_INTEGER) std::size_t {
$1 = (PyInt_Check($input) || PyLong_Check($input)) ? 1 : 0;
$1 = PyLong_Check($input) ? 1 : 0;
}
#endif

Expand Down
13 changes: 5 additions & 8 deletions SWIG/volatilities.i
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,15 @@ enum VolatilityType { ShiftedLognormal, Normal };

#if defined(SWIGPYTHON)
%typemap(in) ext::optional<VolatilityType> %{
if($input == Py_None)
if ($input == Py_None)
$1 = ext::nullopt;
else if (PyInt_Check($input))
$1 = (VolatilityType) PyInt_AsLong($input);
else if (PyLong_Check($input))
$1 = (VolatilityType)PyLong_AsLong($input);
else
$1 = (VolatilityType) PyLong_AsLong($input);
SWIG_exception(SWIG_TypeError, "int expected");
%}
%typecheck (QL_TYPECHECK_VOLATILITYTYPE) ext::optional<VolatilityType> {
if (PyInt_Check($input) || PyLong_Check($input) || Py_None == $input)
$1 = 1;
else
$1 = 0;
$1 = (PyLong_Check($input) || $input == Py_None) ? 1 : 0;
}
#endif

Expand Down

0 comments on commit a7720b2

Please sign in to comment.