From 4c9228b51dbf06e2f932368400f6a34fea324f31 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 25 Jan 2023 23:31:51 -0500 Subject: fix deepcopy --- pyisda/credit_index.pxd | 2 +- pyisda/credit_index.pyx | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pyisda/credit_index.pxd b/pyisda/credit_index.pxd index e355eb8..db446bc 100644 --- a/pyisda/credit_index.pxd +++ b/pyisda/credit_index.pxd @@ -23,4 +23,4 @@ cdef class CreditIndex(CurveList): cdef readonly string cal_file -cdef copy(CurveList orig, CurveList copy) +cdef CurveList_copy(CurveList orig, CurveList copy) diff --git a/pyisda/credit_index.pyx b/pyisda/credit_index.pyx index 861a103..e126964 100644 --- a/pyisda/credit_index.pyx +++ b/pyisda/credit_index.pyx @@ -29,15 +29,10 @@ cimport cython ctypedef TFeeLeg* TFeeLeg_ptr ctypedef TContingentLeg* TContingentLeg_ptr -cdef TContingentLeg* copyContingentLeg(TContingentLeg* leg) nogil: - cdef TContingentLeg* new_leg = malloc(sizeof(TContingentLeg)) - memcpy(new_leg, leg, sizeof(TContingentLeg)) - return new_leg - cdef inline void char_free(char* ptr) nogil: free(ptr) -cdef copy(CurveList orig, CurveList copy): +cdef CurveList_copy(CurveList orig, CurveList copy): cdef: char* buf char* new_buf @@ -50,6 +45,7 @@ cdef copy(CurveList orig, CurveList copy): copy._curves.resize(orig._curves.size()) copy.defaulted = orig.defaulted copy.offset_recovery_rates = orig.offset_recovery_rates + copy.defaulted_weight = orig.defaulted_weight for p in orig.names: buf = orig._curves[p.second].get() offset = p.first.name - buf @@ -236,7 +232,8 @@ cdef class CurveList: def __deepcopy__(self, memo): cdef CurveList copy = CurveList.__new__(CurveList) - copy(self, copy) + CurveList_copy(self, copy) + memo[id(self)] = copy def __reduce__(self): return (self.__class__, (self.curves, TDate_to_pydate(self.base_date))) @@ -284,7 +281,7 @@ cdef class CreditIndex(CurveList): int n TContingentLeg* cl TFeeLeg* fl - copy(self, copy) + CurveList_copy(self, copy) copy._start_date = self._start_date copy._maturities = self._maturities n = self._maturities.size() @@ -319,6 +316,8 @@ cdef class CreditIndex(CurveList): for p in self.names: buf = self._curves[p.second].get() + #skip pointer + buf += sizeof(TRatePt*) offset = p.first.name - buf h ^= Hash64(buf, offset + p.first.size()) -- cgit v1.2.3-70-g09d2