From 5e7f2a62546398dc87e4fa01ac43800dc462e539 Mon Sep 17 00:00:00 2001 From: Ed J Date: Wed, 18 Dec 2024 18:11:43 +0000 Subject: [PATCH] prevent type-conversion of output ndarray with parent already --- lib/PDL/Core/pdlapi.c | 5 +++-- t/core.t | 10 ++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/PDL/Core/pdlapi.c b/lib/PDL/Core/pdlapi.c index c31e3d8bb..78c39d15d 100644 --- a/lib/PDL/Core/pdlapi.c +++ b/lib/PDL/Core/pdlapi.c @@ -1081,14 +1081,13 @@ pdl_trans *pdl_create_trans(pdl_transvtable *vtable) { pdl_error pdl_type_coerce(pdl_trans *trans) { pdl_error PDL_err = {0, NULL, 0}; - PDL_Indx i; pdl_transvtable *vtable = trans->vtable; pdl **pdls = trans->pdls; trans->__datatype = -1; char p2child_has_badvalue = (vtable->npdls == 2 && pdls[0]->has_badvalue && (vtable->par_flags[1] & PDL_PARAM_ISCREATEALWAYS)); PDL_Anyval parent_badvalue = p2child_has_badvalue ? pdls[0]->badvalue : (PDL_Anyval){PDL_INVALID, {0}}; - PDL_Indx nchildren = vtable->npdls - vtable->nparents; + PDL_Indx i, nparents = vtable->nparents, nchildren = vtable->npdls - nparents; /* copy the "real" (passed-in) outputs to the end-area to use as actual outputs, possibly after being converted, leaving the passed-in ones alone to be picked up for use in CopyBadStatusCode */ @@ -1132,6 +1131,8 @@ pdl_error pdl_type_coerce(pdl_trans *trans) { pdl->datatype = new_dtype; } else if (new_dtype != pdl->datatype) { PDLDEBUG_f(printf("pdl_type_coerce (%s) pdl=%"IND_FLAG" from %d to %d\n", vtable->name, i, pdl->datatype, new_dtype)); + if (i >= nparents && pdl->trans_parent && pdl->trans_parent != trans) + return pdl_make_error(PDL_EFATAL, "%s: cannot convert output ndarray %s from type %d to %d with parent", vtable->name, vtable->par_names[i], pdl->datatype, new_dtype); pdl = pdl_get_convertedpdl(pdl, new_dtype); if (!pdl) return pdl_make_error(PDL_EFATAL, "%s got NULL pointer from get_convertedpdl on param %s", vtable->name, vtable->par_names[i]); diff --git a/t/core.t b/t/core.t index 8aa22c950..658f85110 100644 --- a/t/core.t +++ b/t/core.t @@ -803,8 +803,14 @@ my $oneway_slice = $y->slice('0:1'); is_pdl $oneway_slice, pdl '[4 5]'; eval {$oneway_slice .= 11}; isnt $@, '', 'error on assigning into one-way slice'; -my $c = $y->flowing->_convert_int(cdouble->enum); -ok $c->fflows, 'flowing -> converted has "flowing" on'; +ok $y->flowing->_convert_int(cdouble->enum)->fflows, 'flowing -> converted has "flowing" on'; +} + +{ +eval {czip(float(3), float(2), cdouble(0)->slice(''))}; +like $@, qr/convert output ndarray.*with.*parent/; +czip(float(3), float(2), my $out_cd = cdouble(0)); +is_pdl $out_cd, cdouble('3+2i'); } {