Skip to content

Commit ad237dc

Browse files
fix(scope): Copy _last_event_id in Scope.__copy__
Fixes GH-3113 Co-authored-by: Adam Johnson <[email protected]>
1 parent c80cad1 commit ad237dc

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

sentry_sdk/scope.py

+2
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ def __copy__(self):
244244

245245
rv._profile = self._profile
246246

247+
rv._last_event_id = self._last_event_id
248+
247249
return rv
248250

249251
@classmethod

tests/test_basics.py

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
start_transaction,
1818
last_event_id,
1919
add_breadcrumb,
20+
isolation_scope,
2021
Hub,
2122
Scope,
2223
)
@@ -800,3 +801,11 @@ def test_last_event_id_transaction(sentry_init):
800801
pass
801802

802803
assert last_event_id() is None, "Transaction should not set last_event_id"
804+
805+
806+
def test_last_event_id_scope(sentry_init):
807+
sentry_init(enable_tracing=True)
808+
809+
# Should not crash
810+
with isolation_scope() as scope:
811+
assert scope.last_event_id() is None

tests/test_scope.py

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
)
2020

2121

22+
SLOTS_NOT_COPIED = {"client"}
23+
"""__slots__ that are not copied when copying a Scope object."""
24+
25+
2226
def test_copying():
2327
s1 = Scope()
2428
s1.fingerprint = {}
@@ -34,6 +38,15 @@ def test_copying():
3438
assert s1._fingerprint is s2._fingerprint
3539

3640

41+
def test_all_slots_copied():
42+
scope = Scope()
43+
scope_copy = copy.copy(scope)
44+
45+
# Check all attributes are copied
46+
for attr in set(Scope.__slots__) - SLOTS_NOT_COPIED:
47+
assert getattr(scope_copy, attr) == getattr(scope, attr)
48+
49+
3750
def test_merging(sentry_init, capture_events):
3851
sentry_init()
3952

0 commit comments

Comments
 (0)