summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-05-18 20:23:07 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-05-18 20:23:07 -0700
commit18a88dd3f40abf73451e6be1a93d6307d111c66b (patch)
tree72c5ac7de118dfd8533fbb596ae0b7a977ad5a37
parent07667473d0ec204eb2cfa7b8938ae2f5010e3220 (diff)
downloadpsych-18a88dd3f40abf73451e6be1a93d6307d111c66b.zip
setting the encoding twice is not allowed
-rw-r--r--ext/psych/parser.c7
-rw-r--r--lib/psych.rb3
-rw-r--r--test/psych/test_parser.rb9
3 files changed, 19 insertions, 0 deletions
diff --git a/ext/psych/parser.c b/ext/psych/parser.c
index 41260ab..c8b92a0 100644
--- a/ext/psych/parser.c
+++ b/ext/psych/parser.c
@@ -293,8 +293,15 @@ static VALUE parse(VALUE self, VALUE yaml)
static VALUE set_external_encoding(VALUE self, VALUE encoding)
{
yaml_parser_t * parser;
+ VALUE exception;
Data_Get_Struct(self, yaml_parser_t, parser);
+
+ if(parser->encoding) {
+ exception = rb_const_get_at(mPsych, rb_intern("Exception"));
+ rb_raise(exception, "don't set the encoding twice!");
+ }
+
yaml_parser_set_encoding(parser, NUM2INT(encoding));
return encoding;
diff --git a/lib/psych.rb b/lib/psych.rb
index 464fae4..a73b892 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -94,6 +94,9 @@ module Psych
# The version of libyaml Psych is using
LIBYAML_VERSION = Psych.libyaml_version.join '.'
+ class Exception < RuntimeError
+ end
+
###
# Load +yaml+ in to a Ruby data structure. If multiple documents are
# provided, the object contained in the first document will be returned.
diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb
index 8c44833..0b1e92e 100644
--- a/test/psych/test_parser.rb
+++ b/test/psych/test_parser.rb
@@ -26,6 +26,15 @@ module Psych
@parser = Psych::Parser.new EventCatcher.new
end
+ def test_set_encoding_twice
+ @parser.external_encoding = Psych::Parser::UTF16LE
+
+ e = assert_raises(Psych::Exception) do
+ @parser.external_encoding = Psych::Parser::UTF16LE
+ end
+ assert_equal "don't set the encoding twice!", e.message
+ end
+
def test_bom
tadpole = 'おたまじゃくし'