Skip to content

Commit

Permalink
Fix writing to an IO with no external_encoding
Browse files Browse the repository at this point in the history
A file-IO has no external_encoding by default, so that rb_to_encoding() fails with a TypeError.

This regression was introduces in commit 2e260f5.

Fixes #2752
  • Loading branch information
larskanis committed Jan 4, 2023
1 parent a6e369c commit 1bfd8a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ext/nokogiri/nokogiri.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ noko_io_write(void *io, char *c_buffer, int c_buffer_len)
{
VALUE rb_args[2], rb_n_bytes_written;
VALUE rb_io = (VALUE)io;
rb_encoding *io_encoding = rb_to_encoding(rb_funcall(rb_io, id_external_encoding, 0));
VALUE rb_enc = rb_funcall(rb_io, id_external_encoding, 0);
rb_encoding *io_encoding = RB_NIL_P(rb_enc) ? rb_ascii8bit_encoding() : rb_to_encoding(rb_enc);

rb_args[0] = rb_io;
rb_args[1] = rb_enc_str_new(c_buffer, (long)c_buffer_len, io_encoding);
Expand Down
8 changes: 8 additions & 0 deletions test/xml/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,14 @@ def test_write_to_with_block
end
end

def test_write_to_file_without_encoding
io = Tempfile.new
xml.write_to(io)
io.rewind
assert_equal(xml.to_xml, io.read)
io.close
end

def test_serialize_with_block
called = false
conf = nil
Expand Down

0 comments on commit 1bfd8a7

Please sign in to comment.