Project

General

Profile

Actions

Bug #18743

open

Enumerator#next / peek re-use each others stacktraces

Added by sos4nt (Stefan Schüßler) 8 months ago. Updated 30 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:108294]

Description

I encountered an odd behavior.

If I rescue the StopIteration exception from peek and call next afterwards: (or vice-versa)

# enum.rb             # 1
                      # 2
enum = [].each        # 3
enum.peek rescue nil  # 4
enum.next             # 5

it will show the stacktrace from the rescued peek call:

$ ruby enum.rb
enum.rb:4:in `peek': iteration reached an end (StopIteration)
	from enum.rb:4:in `<main>'

Whereas the error should refer to next on line number 5.

The same happens when calling peek after next or when having muliple peek / next calls:

# enum.rb                # 1
                         # 2
enum = [].each           # 3
enum.peek rescue nil     # 4
enum.next rescue nil     # 5
enum.peek rescue nil     # 6
puts "line #{__LINE__}"  # 7
enum.next                # 8

The stacktrace from the first (rescued) peek or next call will be shown which doesn't reflect the actual error location:

$ ruby enum.rb
line 7
enum.rb:4:in `peek': iteration reached an end (StopIteration)
	from enum.rb:4:in `<main>'

This is very confusing when debugging code.


Files

01-Recreate-stacktrace-enumerator.patch (1.29 KB) 01-Recreate-stacktrace-enumerator.patch marcper (Marcelo Pereira), 10/23/2022 01:09 PM

Updated by marcper (Marcelo Pereira) 3 months ago

Good day, @matz (Yukihiro Matsumoto). Is the fix above acceptable?

Updated by marcper (Marcelo Pereira) 2 months ago

Hello @matz (Yukihiro Matsumoto), please let me know if someone else should be pinged for this.

Updated by marcper (Marcelo Pereira) about 2 months ago

@matz (Yukihiro Matsumoto), I'm adding the patch file here as well, following the contributor's guide.

Updated by marcper (Marcelo Pereira) 30 days ago

Pinging @matz (Yukihiro Matsumoto). Sorry if I'm being annoying, but the contributor guide says I should send a reminder after a few weeks if there's no response.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0