Skip to content

Commit

Permalink
Do not set loaded booleans to readonly for perl >= v5.36
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
automatically. 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 44aa92f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
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
4 changes: 3 additions & 1 deletion lib/YAML/XS.pod
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ This ensures leading that things like leading zeros and other formatting are pre

Default: undef

When used with perl 5.36 or later, builtin booleans will work out of the box. They will be created by C<Load> and recognized by C<Dump> automatically (since YAML::XS 0.89).
Since YAML::XS 0.89: When used with perl 5.36 or later, builtin booleans will work out of the box. They will be created by C<Load> and recognized by C<Dump> automatically (since YAML::XS 0.89).

say Dump({ truth => builtin::true });
# truth: true

Since YAML::XS v0.902: loaded booleans are not set to readonly anymore.

For older perl versions you can use the following configuration to serialize data as YAML booleans:

When set to C<"JSON::PP"> or C<"boolean">, the plain (unquoted) strings C<true> and C<false> will be loaded as C<JSON::PP::Boolean> or C<boolean.pm> objects. Those objects will be dumped again as plain "true" or "false".
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 44aa92f

Please sign in to comment.