Skip to content

Commit

Permalink
Support loading builtin booleans
Browse files Browse the repository at this point in the history
Since perl 5.36, PL_sv_yes/PL_sv_no will be preserved as booleans.
If we create them with newSVsv(), they are not readonly anymore and
will still roundtrip.

Also add a cleanup in t/file.t
  • Loading branch information
perlpunk committed Sep 7, 2024
1 parent c717baf commit bf67fac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions LibYAML/perl_libyaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,11 @@ load_scalar(perl_yaml_loader_t *loader)
scalar = sv_setref_iv(scalar, name, 1);
}
else {
#ifdef PERL_HAVE_BOOLEANS
scalar = newSVsv(&PL_sv_yes);
#else
scalar = &PL_sv_yes;
#endif
}
if (anchor)
hv_store(loader->anchors, anchor, strlen(anchor), SvREFCNT_inc(scalar), 0);
Expand All @@ -567,7 +571,11 @@ load_scalar(perl_yaml_loader_t *loader)
scalar = sv_setref_iv(scalar, name, 0);
}
else {
#ifdef PERL_HAVE_BOOLEANS
scalar = newSVsv(&PL_sv_no);
#else
scalar = &PL_sv_no;
#endif
}
if (anchor)
hv_store(loader->anchors, anchor, strlen(anchor), SvREFCNT_inc(scalar), 0);
Expand Down
6 changes: 5 additions & 1 deletion t/boolean.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use FindBin '$Bin';
use lib $Bin;
use constant HAVE_BOOLEANS => ($^V ge v5.36);
use TestYAMLTests tests => 5 + (HAVE_BOOLEANS ? 2 : 0);
use TestYAMLTests tests => 5 + (HAVE_BOOLEANS ? 4 : 0);

my $yaml = <<'...';
---
Expand Down Expand Up @@ -58,4 +58,8 @@ if( HAVE_BOOLEANS ) {
'true': true
...
'booleans loaded as core booleans';

eval { $hash->{a} = 'something else' };
is $@, '', "core boolean element in hash is not readonly";
is $hash->{a}, 'something else', "core boolean element is changed";
}
4 changes: 4 additions & 0 deletions t/file.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ YAML::XS::DumpFile($test_file, $t3, $t4);
my ($t3_, $t4_) = LoadFile($test_file);

is_deeply [$t3_, $t4_], [$t3, $t4], 'Unicode roundtrip ok';

END {
rmtree('t/output');
}

0 comments on commit bf67fac

Please sign in to comment.