File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed
Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -36,3 +36,4 @@ cdef extern from 'ql/instruments/swap.hpp' namespace 'QuantLib' nogil:
3636 DiscountFactor endDiscounts(Size j) except +
3737 DiscountFactor npvDateDiscount() except +
3838 Leg& leg(Size j) except +
39+ const vector[Leg]& legs()
Original file line number Diff line number Diff line change @@ -11,9 +11,11 @@ from quantlib.types cimport Size
1111from quantlib.cashflow cimport Leg
1212cimport quantlib.time._date as _date
1313from quantlib.time.date cimport date_from_qldate
14+ from quantlib._cashflow cimport Leg as QlLeg
15+
1416from . cimport _swap
1517
16- cdef inline _swap.Swap* get_swap(Swap swap):
18+ cdef inline _swap.Swap* get_swap(Swap swap) noexcept :
1719 """ Utility function to extract a properly casted Swap pointer out of the
1820 internal _thisptr attribute of the Instrument base class. """
1921
@@ -60,10 +62,18 @@ cdef class Swap(Instrument):
6062
6163 def leg (self , int i ):
6264 cdef Leg leg = Leg.__new__ (Leg)
63- leg._thisptr = get_swap(self ).leg(i)
65+ cdef _swap.Swap* swap = < _swap.Swap* > self ._thisptr.get()
66+ if 0 <= i < swap.numberOfLegs():
67+ leg._thisptr = swap.legs()[i]
68+ else :
69+ raise IndexError (f" leg #{i} doesn't exist" )
6470 return leg
6571
6672 def __getitem__ (self , int i ):
6773 cdef Leg leg = Leg.__new__ (Leg)
68- leg._thisptr = get_swap(self ).leg(i)
74+ cdef _swap.Swap* swap = < _swap.Swap* > self ._thisptr.get()
75+ if 0 <= i < swap.numberOfLegs():
76+ leg._thisptr = swap.legs()[i]
77+ else :
78+ raise IndexError (f" leg #{i} doesn't exist" )
6979 return leg
You can’t perform that action at this time.
0 commit comments