From: "byroot (Jean Boussier) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "byroot (Jean Boussier)" <noreply@ruby-lang.org>
Subject: [ruby-core:117147] [Ruby master Feature#20335] `Thread.each_caller_location` should accept the same arguments as `caller` and `caller_locations`
Date: Thu, 14 Mar 2024 09:32:42 +0000 (UTC) [thread overview]
Message-ID: <redmine.issue-20335.20240314093241.7941@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-20335.20240314093241.7941@ruby-lang.org
Issue #20335 has been reported by byroot (Jean Boussier).
----------------------------------------
Feature #20335: `Thread.each_caller_location` should accept the same arguments as `caller` and `caller_locations`
https://bugs.ruby-lang.org/issues/20335
* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
`Thread.each_caller_location` was added to Ruby 3.2 as part of [Feature #16663] and is a very useful API for emitting warnings with a proper source location and similar use cases.
However in many of the cases where I used it, or seen it used, it was needed to skip the first, or a couple frames:
Examples:
Sorbet: https://github.com/Shopify/sorbet/blob/b27a14c247ace7cabdf0f348bfb11fdf0b7e9ab4/gems/sorbet-runtime/lib/types/private/caller_utils.rb#L6-L18
```ruby
def self.find_caller
skiped_first = false
Thread.each_caller_location do |loc|
unless skiped_first
skiped_first = true
next
end
next if loc.path&.start_with?("<internal:")
return loc if yield(loc)
end
nil
end
```
@fxn's PR: https://github.com/ruby/ruby/blob/9c2e686719a5a4df5ea0b8a3b6a373ca6003c229/lib/bundled_gems.rb#L140-L146
```ruby
frames_to_skip = 2
location = nil
Thread.each_caller_location do |cl|
if frames_to_skip >= 1
frames_to_skip -= 1
next
end
# snipp...
```
### Proposal
I think it would be very useful if `Thread.each_caller_location` accepted the same arguments as `caller` and `caller_locations`:
```ruby
#each_caller_location(start = 1, length = nil)
#each_caller_location(range)
```
@jeremyevans0 what do you think?
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/
next parent reply other threads:[~2024-03-14 9:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-14 9:32 byroot (Jean Boussier) via ruby-core [this message]
2024-03-14 14:43 ` [ruby-core:117173] [Ruby master Feature#20335] `Thread.each_caller_location` should accept the same arguments as `caller` and `caller_locations` jeremyevans0 (Jeremy Evans) via ruby-core
2024-03-14 14:52 ` [ruby-core:117174] " Eregon (Benoit Daloze) via ruby-core
2024-03-14 14:53 ` [ruby-core:117175] " Eregon (Benoit Daloze) via ruby-core
2024-03-14 15:08 ` [ruby-core:117180] " byroot (Jean Boussier) via ruby-core
2024-03-14 15:15 ` [ruby-core:117182] " Eregon (Benoit Daloze) via ruby-core
2024-04-17 7:53 ` [ruby-core:117549] " nobu (Nobuyoshi Nakada) via ruby-core
2024-04-17 9:34 ` [ruby-core:117554] " matz (Yukihiro Matsumoto) via ruby-core
2024-04-17 10:16 ` [ruby-core:117557] " mame (Yusuke Endoh) via ruby-core
2024-04-17 10:25 ` [ruby-core:117559] " Eregon (Benoit Daloze) via ruby-core
2024-04-17 17:15 ` [ruby-core:117571] " byroot (Jean Boussier) via ruby-core
2024-04-17 18:54 ` [ruby-core:117574] " Eregon (Benoit Daloze) via ruby-core
2024-04-17 18:59 ` [ruby-core:117575] " byroot (Jean Boussier) via ruby-core
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.issue-20335.20240314093241.7941@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
--cc=noreply@ruby-lang.org \
--cc=ruby-core@ml.ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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).