From 42a3e9e413682f9b078313d2acc585826cab3358 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Tue, 27 Feb 2024 10:50:19 -0500 Subject: [PATCH] do not use pointers --- quantlib/cashflows/fixed_rate_coupon.pyx | 2 +- quantlib/instruments/bonds/cpibond.pyx | 2 +- quantlib/instruments/bonds/fixedratebond.pyx | 2 +- .../instruments/bonds/floatingratebond.pyx | 2 +- quantlib/instruments/credit_default_swap.pyx | 4 ++-- quantlib/instruments/overnightindexedswap.pyx | 4 ++-- quantlib/instruments/vanillaswap.pyx | 9 +++++---- .../termstructures/yields/bond_helpers.pyx | 2 +- quantlib/time/_schedule.pxd | 5 ++--- quantlib/time/daycounters/actual_actual.pyx | 4 ++-- quantlib/time/schedule.pxd | 2 +- quantlib/time/schedule.pyx | 19 ++++++++----------- 12 files changed, 27 insertions(+), 30 deletions(-) diff --git a/quantlib/cashflows/fixed_rate_coupon.pyx b/quantlib/cashflows/fixed_rate_coupon.pyx index 481ccfc27..dc87fc751 100644 --- a/quantlib/cashflows/fixed_rate_coupon.pyx +++ b/quantlib/cashflows/fixed_rate_coupon.pyx @@ -38,7 +38,7 @@ cdef class FixedRateCoupon(Coupon): cdef class FixedRateLeg(Leg): def __init__(self, Schedule schedule): - self.frl = new _frc.FixedRateLeg(deref(schedule._thisptr)) + self.frl = new _frc.FixedRateLeg(schedule._thisptr) def with_notional(self, Real notional): self.frl.withNotionals(notional) diff --git a/quantlib/instruments/bonds/cpibond.pyx b/quantlib/instruments/bonds/cpibond.pyx index f391c2a2c..df5a17972 100644 --- a/quantlib/instruments/bonds/cpibond.pyx +++ b/quantlib/instruments/bonds/cpibond.pyx @@ -34,7 +34,7 @@ cdef class CPIBond(Bond): static_pointer_cast[_inf.ZeroInflationIndex]( cpi_index._thisptr), observation_interpolation, - deref(schedule._thisptr), coupons, + schedule._thisptr, coupons, deref(accrual_day_counter._thisptr), payment_convention, deref(issue_date._thisptr), payment_calendar._thisptr, deref(ex_coupon_period._thisptr), diff --git a/quantlib/instruments/bonds/fixedratebond.pyx b/quantlib/instruments/bonds/fixedratebond.pyx index a592a79c3..25d8fe13a 100644 --- a/quantlib/instruments/bonds/fixedratebond.pyx +++ b/quantlib/instruments/bonds/fixedratebond.pyx @@ -59,7 +59,7 @@ cdef class FixedRateBond(Bond): new _frb.FixedRateBond( settlement_days, face_amount, - deref(schedule._thisptr), + schedule._thisptr, coupons, deref(accrual_day_counter._thisptr), payment_convention, diff --git a/quantlib/instruments/bonds/floatingratebond.pyx b/quantlib/instruments/bonds/floatingratebond.pyx index 73decc325..939131be5 100644 --- a/quantlib/instruments/bonds/floatingratebond.pyx +++ b/quantlib/instruments/bonds/floatingratebond.pyx @@ -69,7 +69,7 @@ cdef class FloatingRateBond(Bond): self._thisptr.reset( new _frb.FloatingRateBond( settlement_days, face_amount, - deref(schedule._thisptr), + schedule._thisptr, static_pointer_cast[_ii.IborIndex](ibor_index._thisptr), deref(accrual_day_counter._thisptr), payment_convention, diff --git a/quantlib/instruments/credit_default_swap.pyx b/quantlib/instruments/credit_default_swap.pyx index 3fbf2cc09..834d6a1c7 100644 --- a/quantlib/instruments/credit_default_swap.pyx +++ b/quantlib/instruments/credit_default_swap.pyx @@ -110,7 +110,7 @@ cdef class CreditDefaultSwap(Instrument): self._thisptr = shared_ptr[_instrument.Instrument]( new _cds.CreditDefaultSwap( - side, notional, spread, deref(schedule._thisptr), + side, notional, spread, schedule._thisptr, payment_convention, deref(day_counter._thisptr), settles_accrual, pays_at_default_time, deref(protection_start._thisptr), @@ -183,7 +183,7 @@ cdef class CreditDefaultSwap(Instrument): cdef CreditDefaultSwap instance = CreditDefaultSwap.__new__(CreditDefaultSwap) instance._thisptr = shared_ptr[_instrument.Instrument]( new _cds.CreditDefaultSwap( - side, notional, upfront, spread, deref(schedule._thisptr), + side, notional, upfront, spread, schedule._thisptr, payment_convention, deref(day_counter._thisptr), settles_accrual, pays_at_default_time, deref(protection_start._thisptr), diff --git a/quantlib/instruments/overnightindexedswap.pyx b/quantlib/instruments/overnightindexedswap.pyx index d6ee1e70e..b3f4ca8e5 100644 --- a/quantlib/instruments/overnightindexedswap.pyx +++ b/quantlib/instruments/overnightindexedswap.pyx @@ -38,7 +38,7 @@ cdef class OvernightIndexedSwap(Swap): if isinstance(nominal, float): self._thisptr = static_pointer_cast[Instrument]( make_shared[_ois.OvernightIndexedSwap]( - swap_type, nominal, deref(schedule._thisptr), fixed_rate, + swap_type, nominal, schedule._thisptr, fixed_rate, deref(fixed_dc._thisptr), static_pointer_cast[_ii.OvernightIndex](overnight_index._thisptr), spread, payment_lag, payment_adjustment, payment_calendar._thisptr, telescopic_value_dates, averaging_method @@ -49,7 +49,7 @@ cdef class OvernightIndexedSwap(Swap): nominals.push_back(n) self._thisptr = static_pointer_cast[Instrument]( make_shared[_ois.OvernightIndexedSwap]( - swap_type, nominals, deref(schedule._thisptr), fixed_rate, + swap_type, nominals, schedule._thisptr, fixed_rate, deref(fixed_dc._thisptr), static_pointer_cast[_ii.OvernightIndex](overnight_index._thisptr), spread, payment_lag, payment_adjustment, payment_calendar._thisptr, telescopic_value_dates, averaging_method diff --git a/quantlib/instruments/vanillaswap.pyx b/quantlib/instruments/vanillaswap.pyx index 9df369733..d4f3bc3dd 100644 --- a/quantlib/instruments/vanillaswap.pyx +++ b/quantlib/instruments/vanillaswap.pyx @@ -1,5 +1,6 @@ """Simple fixed-rate vs Libor swap""" from cython.operator cimport dereference as deref +from libcpp.utility cimport move from quantlib.types cimport Rate, Real, Spread from quantlib.handle cimport optional, static_pointer_cast from quantlib.cashflows.fixed_rate_coupon cimport FixedRateLeg @@ -44,10 +45,10 @@ cdef class VanillaSwap(Swap): new _vanillaswap.VanillaSwap( type, nominal, - deref(fixed_schedule._thisptr), + fixed_schedule._thisptr, fixed_rate, deref(fixed_daycount._thisptr), - deref(float_schedule._thisptr), + float_schedule._thisptr, static_pointer_cast[_ib.IborIndex](ibor_index._thisptr), spread, deref(floating_daycount._thisptr), @@ -112,7 +113,7 @@ cdef class VanillaSwap(Swap): @property def fixed_schedule(self): cdef Schedule sched = Schedule.__new__(Schedule) - sched._thisptr = new QlSchedule(get_vanillaswap(self).fixedSchedule()) + sched._thisptr = move(QlSchedule(get_vanillaswap(self).fixedSchedule())) return sched @property @@ -124,7 +125,7 @@ cdef class VanillaSwap(Swap): @property def floating_schedule(self): cdef Schedule sched = Schedule.__new__(Schedule) - sched._thisptr = new QlSchedule(get_vanillaswap(self).floatingSchedule()) + sched._thisptr = move(QlSchedule(get_vanillaswap(self).floatingSchedule())) return sched @property diff --git a/quantlib/termstructures/yields/bond_helpers.pyx b/quantlib/termstructures/yields/bond_helpers.pyx index 7c1510a62..c6bc61134 100644 --- a/quantlib/termstructures/yields/bond_helpers.pyx +++ b/quantlib/termstructures/yields/bond_helpers.pyx @@ -44,7 +44,7 @@ cdef class FixedRateBondHelper(BondHelper): clean_price.handle(), settlement_days, face_amount, - deref(schedule._thisptr), + schedule._thisptr, coupons, deref(day_counter._thisptr), payment_conv, diff --git a/quantlib/time/_schedule.pxd b/quantlib/time/_schedule.pxd index 5a022bb4b..5d94328e5 100644 --- a/quantlib/time/_schedule.pxd +++ b/quantlib/time/_schedule.pxd @@ -1,5 +1,4 @@ -include '../types.pxi' - +from quantlib.types cimport Size from libcpp cimport bool from libcpp.vector cimport vector from ._period cimport Period @@ -7,7 +6,7 @@ from ._date cimport Date from ._calendar cimport Calendar, BusinessDayConvention from quantlib.handle cimport optional from .dategeneration cimport DateGeneration -cdef extern from 'ql/time/schedule.hpp' namespace 'QuantLib': +cdef extern from 'ql/time/schedule.hpp' namespace 'QuantLib' nogil: cdef cppclass Schedule: Schedule() diff --git a/quantlib/time/daycounters/actual_actual.pyx b/quantlib/time/daycounters/actual_actual.pyx index 92e5467ab..ef3d7968b 100644 --- a/quantlib/time/daycounters/actual_actual.pyx +++ b/quantlib/time/daycounters/actual_actual.pyx @@ -21,7 +21,7 @@ from quantlib.time.daycounter cimport DayCounter cdef class ActualActual(DayCounter): - def __init__(self, Convention convention=Convention.ISDA, Schedule schedule=Schedule.from_dates([])): + def __init__(self, Convention convention=Convention.ISDA, Schedule schedule=Schedule.__new__(Schedule)): """ Actual/Actual day count The day count can be calculated according to: @@ -37,7 +37,7 @@ cdef class ActualActual(DayCounter): https://www.isda.org/a/pIJEE/The-Actual-Actual-Day-Count-Fraction-1999.pdf """ self._thisptr = <_daycounter.DayCounter*> new \ - _aa.ActualActual(convention, deref(schedule._thisptr)) + _aa.ActualActual(convention, schedule._thisptr) cdef _daycounter.DayCounter* from_name(str convention): diff --git a/quantlib/time/schedule.pxd b/quantlib/time/schedule.pxd index f39534c85..3b4ba874a 100644 --- a/quantlib/time/schedule.pxd +++ b/quantlib/time/schedule.pxd @@ -1,4 +1,4 @@ cimport quantlib.time._schedule as _schedule cdef class Schedule: - cdef _schedule.Schedule* _thisptr + cdef _schedule.Schedule _thisptr diff --git a/quantlib/time/schedule.pyx b/quantlib/time/schedule.pyx index 1401cf451..93d226648 100644 --- a/quantlib/time/schedule.pyx +++ b/quantlib/time/schedule.pyx @@ -1,6 +1,7 @@ from cython.operator cimport dereference as deref, preincrement as preinc from libcpp cimport bool from libcpp.vector cimport vector +from libcpp.utility cimport move from quantlib.handle cimport optional cimport quantlib.time._date as _date @@ -31,7 +32,8 @@ cdef class Schedule: warnings.warn("Deprecated: use class method from_rule instead", DeprecationWarning) - self._thisptr = new _schedule.Schedule( + self._thisptr = move( + _schedule.Schedule( deref(effective_date._thisptr), deref(termination_date._thisptr), deref(tenor._thisptr), @@ -40,6 +42,7 @@ cdef class Schedule: termination_date_convention, date_generation_rule, end_of_month, _date.Date(), _date.Date() + ) ) @classmethod @@ -69,7 +72,7 @@ cdef class Schedule: opt_rule = rule if end_of_month is not None: opt_end_of_month = end_of_month - instance._thisptr = new _schedule.Schedule( + instance._thisptr = move(_schedule.Schedule( _dates, calendar._thisptr, business_day_convention, @@ -78,7 +81,7 @@ cdef class Schedule: opt_rule, opt_end_of_month, is_regular - ) + )) return instance @@ -92,7 +95,7 @@ cdef class Schedule: Date first_date=Date(), Date next_to_lastdate=Date()): cdef Schedule instance = Schedule.__new__(Schedule) - instance._thisptr = new _schedule.Schedule( + instance._thisptr = move(_schedule.Schedule( deref(effective_date._thisptr), deref(termination_date._thisptr), deref(tenor._thisptr), @@ -101,15 +104,9 @@ cdef class Schedule: termination_date_convention, rule, end_of_month, deref(first_date._thisptr), deref(next_to_lastdate._thisptr) - ) + )) return instance - def __dealloc__(self): - if self._thisptr is not NULL: - del self._thisptr - self._thisptr = NULL - - def dates(self): cdef vector[_date.Date] dates = self._thisptr.dates() cdef list t = []