ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "JesseJohnson (Jesse Johnson) via ruby-dev" <ruby-dev@ml.ruby-lang.org>
To: ruby-dev@ml.ruby-lang.org
Cc: "JesseJohnson (Jesse Johnson)" <noreply@ruby-lang.org>
Subject: [ruby-dev:52058]  [Ruby master Bug#8445] IO.open and IO#set_enconding does not support :fallback option
Date: Mon, 13 Nov 2023 19:29:49 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-105299.20231113192949.7143@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-8445.20130524220300.7143@ruby-lang.org

Issue #8445 has been updated by JesseJohnson (Jesse Johnson).


I can replicate this in 3.2.2.

----------------------------------------
Bug #8445: IO.open and IO#set_enconding does not support :fallback option
https://bugs.ruby-lang.org/issues/8445#change-105299

* Author: pjmtdw (Haruhiro Yoshimoto)
* Status: Assigned
* Priority: Normal
* Assignee: akr (Akira Tanaka)
* ruby -v: trunk(ruby 2.1.0dev)
----------------------------------------
RubyDoc says that `IO.open` and `IO#set_encoding` supports optional argument defined in `String#encode`.
http://ruby-doc.org/core-2.0/IO.html#method-c-new-label-Options
In fact, `:invalid, :undef and :replace` works as expected.

However, `:fallback` option does not work neither for `IO.open` and `IO#set_encoding`.
Following is the example code which does not work.
`f(x)` is never called even if hoge.txt contains non convertible character.

```ruby
File.open("./hoge.txt","r:Shift_JIS:utf-8", :fallback => lambda{|x|f(x)}){|f|
  ...
}
File.open("./hoge.txt"){|f|
  f.set_encoding("Shift_JIS","utf-8",:fallback => lambda{|x|f(x)})
  ...
}
```

I Think this is because `fill_cbuf()` in `io.c` calls `rb_econv_convert()` from `transcode.c` directly.
On the other hand, `fallback_func` is called in `transcode_loop()`, which is called by `str_encode()`. 

Since `transcode_loop()` also calls `rb_econv_convert()`, I wrote a small patch which moves some codes from
`transcode_loop()` to `rb_econv_convert()` to fix the problem.

The attached file is the patch. Hope this helps.


---Files--------------------------------
support-fallback-for-io.patch (3.9 KB)


-- 
https://bugs.ruby-lang.org/

           reply	other threads:[~2023-11-13 19:30 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <redmine.issue-8445.20130524220300.7143@ruby-lang.org>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-105299.20231113192949.7143@ruby-lang.org \
    --to=ruby-dev@ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    --cc=ruby-dev@ml.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).