summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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 = 'おたまじゃくし'