[ruby-core:121683] [Ruby Bug#21272] Class.new doesn't trigger :class TracePoint
Issue #21272 has been reported by st0012 (Stan Lo). ---------------------------------------- Bug #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272 * Author: st0012 (Stan Lo) * Status: Open * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
Issue #21272 has been updated by Eregon (Benoit Daloze). In my view, `Baz = Class.new` doesn't really "Start a class or module definition." it creates a class but there is no definition of it, no body. Though `Baz = Class.new { ... }` would arguably start a definition/body, but then so would `Baz.class_exec { ... }` and that seems less reasonable to catch with a :class TracePoint. I think unless there is a good motivating example to change behavior (which could be incompatible), it's better to just document it better, so I'd suggest opening a PR to document it better. ---------------------------------------- Bug #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272#change-112793 * Author: st0012 (Stan Lo) * Status: Open * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
Issue #21272 has been updated by cfis (Charlie Savage). This is a difference in Ruby 4.0, all previous versions of Ruby send out a Class.new event. You can see this in the ruby-prof test suite which now has lots of new failures. For example this test now fails: ---------------------------------------- Bug #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272#change-116309 * Author: st0012 (Stan Lo) * Status: Open * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
Issue #21272 has been updated by Eregon (Benoit Daloze). cfis (Charlie Savage) wrote in #note-2:
This is a difference in Ruby 4.0. Previous versions of Ruby sent out a Class.new event.
No they did not as far as I can see. Even Ruby 2.0.0 does not emit a `:class` tracepoint for `Class.new`: ``` $ ruby -ve 'TracePoint.trace(:class) { puts "class tp" }; p 1; class Foo; end; p 2; Class.new {}' ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-linux] 1 class tp 2 $ ruby -ve 'TracePoint.trace(:class) { puts "class tp" }; p 1; class Foo; end; p 2; Class.new {}' ruby 4.0.1 (2026-01-13 revision e04267a14b) +PRISM [x86_64-linux] 1 class tp 2 ``` ---------------------------------------- Bug #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272#change-116311 * Author: st0012 (Stan Lo) * Status: Open * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
Issue #21272 has been updated by Eregon (Benoit Daloze). Tracker changed from Bug to Feature Backport deleted (3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN) Changing this to a feature request. ---------------------------------------- Feature #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272#change-116312 * Author: st0012 (Stan Lo) * Status: Open ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
Issue #21272 has been updated by Eregon (Benoit Daloze). @cfis I think you're thinking about `:call` TracePoints, that's unrelated to this issue and that did change in 4.0, see #21254 and #21298. ---------------------------------------- Feature #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272#change-116313 * Author: st0012 (Stan Lo) * Status: Open ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
Issue #21272 has been updated by cfis (Charlie Savage). @Eregon - Ah, you are right. Thanks for the pointers to the other issues. Reading them I can see that Class.new is eliminated but I also see this note:
Before inlining, ObjectSpace would report the allocation class path and method id as Class#new which isn't very helpful. With the inlining patch, we can see that the object is allocated in Foo#test.
It seems like tracepoint doesn't report anything anymore though? Should I move my questions to those tickets? Thanks! ---------------------------------------- Feature #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272#change-116320 * Author: st0012 (Stan Lo) * Status: Open ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
Issue #21272 has been updated by cfis (Charlie Savage). Ok, I see, now its `Array#initialize` (or some such). That looks like a nice change, will update ruby-prof. ---------------------------------------- Feature #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272#change-116331 * Author: st0012 (Stan Lo) * Status: Open ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
Issue #21272 has been updated by Eregon (Benoit Daloze). cfis (Charlie Savage) wrote in #note-6:
Should I move my questions to those tickets?
Yes, or file a new ticket with a reproduction. ---------------------------------------- Feature #21272: Class.new doesn't trigger :class TracePoint https://bugs.ruby-lang.org/issues/21272#change-116332 * Author: st0012 (Stan Lo) * Status: Open ---------------------------------------- According to the official [documentation](https://docs.ruby-lang.org/en/master/TracePoint.html#class-TracePoint-label-...):
To filter what is traced, you can pass any of the following as events:
:class Start a class or module definition.
I'd expect `:class` events to be triggered when new classes are defined via `Class.new` as well, but currently that's not the case. Should we either support `Class.new`, or clarify the behaviour in documentation? ### Reproduction ```rb TracePoint.trace(:class) do |tp| puts "Class created at line: #{tp.lineno}" end class Foo; end # Triggers the tracepoint Baz = Class.new # Doesn't trigger the tracepoint # ruby test.rb # Class created at line: 5 ``` -- https://bugs.ruby-lang.org/
participants (3)
-
cfis (Charlie Savage) -
Eregon (Benoit Daloze) -
st0012 (Stan Lo)