Empty document test
Matěj Cepl <[email protected]>
| Newsgroups | gmane.text.yaml.general |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I am in the process of porting a Perl module Data::YAML to Python (using
PyYAML in background) and working through the Data::YAML tests. I have
stumbled on this test
{
out => undef,
in => [ '---', '...' ],
name => 'Regression: only_header',
error => qr{Premature\s+end}i,
},
which could be hopefully translated into this Perl script
#!/usr/bin/perl -Wall
use strict;
use warnings;
use Data::YAML::Reader;
my %test = (
name => 'Hello World 5',
in => [ '---', '...', ],
error => qr{Missing\s+'[.][.][.]'},
);
sub iter {
my $ar = shift;
return sub {
return shift @$ar;
};
}
my $yaml = Data::YAML::Reader->new;
my $source = join( "\n", @{ $test{in} } ) . "\n";
print "\$source = $source\n";
my $iter = iter( $test{in} );
my $got = $yaml->read($iter) ;
print "\$got = $got\n";
This happily crashes as expected:
mitmanek:yamlish (master) $ perl test-perl.pl
$source = ---
...
Premature end of YAML at test-perl.pl line 25
mitmanek:yamlish (master) $
However, when I attempt to generate the similar exception with this script
#!/usr/bin/python
import yaml
IN = {
"name": 'Hello World 6',
"in": """---
...
"""
}
print IN['in']
res = yaml.load(IN['in'], Loader=yaml.SafeLoader)
print "'%s'" % res
PyYAML doesn't crash and returns None (or empty string with
yaml.BaseLoader, which I otherwise use).
mitmanek:yamlish (master) $ python failing_test.py
---
...
'None'
mitmanek:yamlish (master) $
Whose fault this difference is? Data::YAML, PyYAML, or (most likely)
mine, because I misunderstood something? When looking through the spec
(http://www.yaml.org/spec/1.2/spec.html) I haven't found explicit
treatment of empty documents, but from all the careful wordings about
when individual objects can be empty, it seems that generally emptiness
is a problem. So, why PyYAML doesn't raise an exception?
Do I have to explicitly check for empty documents to 100% emulate
Data::YAML or are there some configuration options for yaml.load() (or
something) to behave the same?
Thank you in advance for any hints,
Matěj
--
http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC
If trains stop at train stations, what happens at work stations?
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Yaml-core mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/yaml-core