-
Notifications
You must be signed in to change notification settings - Fork 21.9k
sub, sub!, gsub, and gsub! should set back references #34405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kamipo (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
Yes, that's why Proc#binding is used in this PR. |
@shugo Oh! Sorry, totally missed this was a PR instead of an issue, and also missed it was you, who of course is aware of my remark. |
Huge 👍 from me. |
activesupport/lib/active_support/core_ext/string/output_safety.rb
Outdated
Show resolved
Hide resolved
No problem. I found the following article in Issue #10598 you mentioned: It seems that I'm one or more laps behind. |
activesupport/lib/active_support/core_ext/string/output_safety.rb
Outdated
Show resolved
Hide resolved
Does this have a race condition if It sounds like two threads could each call |
Two threads can call
|
Ah, this is wrong, but I believe 2 and 3 are enough. |
I realized that this doesn't apply to orphan Proc objects. require "active_support"
require "active_support/core_ext"
def run
block = Proc.new {
10.times do
p $~
sleep(0.1)
end
$&.upcase
}
t = Thread.start {
p "bar".html_safe.sub(/\w+/, &block)
}
p "baz".html_safe.sub(/\w+/, &block)
t.join
end
run However, the following program doesn't work: require "active_support"
require "active_support/core_ext"
def make_block
Proc.new {
10.times do
p $~
sleep(0.1)
end
$&.upcase
}
end
block = make_block
t = Thread.start {
p "bar".html_safe.sub(/\w+/, &block)
}
p "baz".html_safe.sub(/\w+/, &block) #=> "BAR"!!!
t.join In the latter case, However, String#sub does not support back references in orphan blocks either. def make_block
Proc.new {
10.times do
p $~ #=> nil
sleep(0.1)
end
$&.upcase #=> undefined method `upcase' for nil:NilClass (NoMethodError)
}
end
block = make_block
t = Thread.start {
p "bar".sub(/\w+/, &block)
}
p "baz".sub(/\w+/, &block)
t.join |
@shugo what is an orphaned block? Are there different scoping rules or something that explain the difference in behavior? |
If a method call which created a block has already been returned, such a block is called an orphan block.
This is wrong. Basically, the variable binding of In the following code, static inline struct vm_svar *
lep_svar(const rb_execution_context_t *ec, const VALUE *lep)
{
VALUE svar;
if (lep && (ec == NULL || ec->root_lep != lep)) {
svar = lep[VM_ENV_DATA_INDEX_ME_CREF];
}
else {
svar = ec->root_svar;
}
VM_ASSERT(svar == Qfalse || vm_svar_valid_p(svar));
return (struct vm_svar *)svar;
} It seems that JRuby doesn't have this feature. Anyway, use of |
Strictly speaking, it's not exactly the same issue. The summary of thread safety of CRuby:
JRuby:
|
private | ||
|
||
def html_escape_interpolated_argument(arg) | ||
(!html_safe? || arg.html_safe?) ? arg : CGI.escapeHTML(arg.to_s) | ||
end | ||
|
||
def set_block_back_references(block, match_data) | ||
Thread.current[:__active_support_safe_buffer_backref] = match_data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block.binding.eval("proc {|m| $~ = m}").call(match_data)
could eliminate the thread local variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nobu Thanks for your suggestion. I've fixed it.
Does the proc version change any of the table in #34405 (comment)? I think this is the situation I was still worried about: def run
block = Proc.new {
n = $&.to_i
if n > 1
p "987654321".sub(/#{n - 1}/, &block)
end
"(#{$&}#{n})"
}
p "987654321".sub(/9/, &block)
end
run ... which doesn't actually work as I thought it would, even in plain Ruby. So not a worry after all. 😄 |
@matthewd Thank you all! |
Summary
ActiveSupport::SafeBuffer has a trap that back references are unavailable:
This PR sets back references using Proc#binding.