Skip to content

Commit

Permalink
prevent type-conversion of output ndarray with parent already - #511
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Dec 26, 2024
1 parent c69ded6 commit 383a807
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- add Pars type-spec "!complex" which makes it an error to supply complex values (#511)
- fix xform datatype selection when "real" or "complex" (#511)
- xforms now select datatype from outputs only if can (#511)
- xforms now give error if supply output with trans_parent needing converting (#511)

2.095 2024-11-03
- add PDL_GENTYPE_IS_{REAL,FLOATREAL,COMPLEX,SIGNED,UNSIGNED}_##ppsym (#502)
Expand Down
11 changes: 8 additions & 3 deletions lib/PDL/Core/pdlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,11 +1151,11 @@ pdl_error pdl__type_coerce_recprotect(pdl_trans *trans, int recurse_count) {
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 i, 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 */
for (i=vtable->nparents; i<vtable->npdls; i++) pdls[i+nchildren] = pdls[i];
for (i=nparents; i<vtable->npdls; i++) pdls[i+nchildren] = pdls[i];
for (i=0; i<vtable->npdls; i++) {
pdl *pdl = pdls[i];
short flags = vtable->par_flags[i];
Expand All @@ -1167,11 +1167,16 @@ pdl_error pdl__type_coerce_recprotect(pdl_trans *trans, int recurse_count) {
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_EUSERERROR,
"%s: cannot convert output ndarray %s from type %s to %s with parent",
vtable->name, vtable->par_names[i],
PDL_TYPENAME(pdl->datatype), PDL_TYPENAME(new_dtype));
PDL_RETERROR(PDL_err, pdl__get_convertedpdl_recprotect(pdl, &pdl, new_dtype, recurse_count + 1));
if (pdl->datatype != new_dtype)
return pdl_make_error_simple(PDL_EFATAL, "type not expected value after get_convertedpdl\n");
/* if type-convert output, put in end-area */
pdls[i + (i >= vtable->nparents ? nchildren : 0)] = pdl;
pdls[i + (i >= nparents ? nchildren : 0)] = pdl;
}
}
return PDL_err;
Expand Down
4 changes: 2 additions & 2 deletions t/core.t
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,8 @@ is_pdl $o_float, float(3), 'output right from flowing output type < inputs';
is $o_float->trans_parent->vtable->name, 'PDL::Ops::plus', 'trans_parent of output is plus from flowing output type < inputs';
is 0+$o_float->trans_children, 0, '0 trans_children on output from flowing output type < inputs';

eval {PDL::plus(double(1)->flowing, double(2), float(0)->slice(''), 0)};
is $@, '', "current no error when flowing output to flowing xform, out type < input";
eval {PDL::eqvec(double([1,2]), double([1,2]), float(0)->slice(''))};
like $@, qr/cannot convert/, "error when flowing output to xform, out forcetype != supplied out type";

for ([\&float,\&cfloat,\&cdouble], [\&double,\&cdouble,\&cfloat], [\&ldouble,\&cldouble]) {
my ($rt, $ct, $other_ct) = @$_;
Expand Down

0 comments on commit 383a807

Please sign in to comment.