| 1 |
"""Unit tests for celery.app.trace module.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
from celery.app.trace import traceback_clear |
| 6 |
|
| 7 |
|
| 8 |
class test_traceback_clear: |
| 9 |
"""Unit tests for traceback_clear function.""" |
| 10 |
|
| 11 |
def test_uses_exc_argument(self): |
| 12 |
"""Test that traceback_clear(exc) correctly uses the exc argument. |
| 13 |
|
| 14 |
This test proves that the reported issue about traceback_clear not using |
| 15 |
the exc argument is NOT valid. The function does use the exc argument correctly. |
| 16 |
""" |
| 17 |
# Create exception with traceback |
| 18 |
def create_exception_with_traceback(): |
| 19 |
"""Create an exception with a traceback for testing.""" |
| 20 |
try: |
| 21 |
# Create a nested call stack to have frames to clear |
| 22 |
def inner_function(): |
| 23 |
x = "some_local_variable" * 1000 # Create local variable # noqa: F841 |
| 24 |
y = list(range(1000)) # Another local variable # noqa: F841 |
| 25 |
raise ValueError("Test exception with traceback") |
| 26 |
|
| 27 |
def outer_function(): |
| 28 |
z = "outer_local_variable" * 1000 # Local variable in outer frame # noqa: F841 |
| 29 |
inner_function() |
| 30 |
|
| 31 |
outer_function() |
| 32 |
except Exception as e: |
| 33 |
return e |
| 34 |
|
| 35 |
# Test 1: traceback_clear(exc) with provided exception |
| 36 |
exc = create_exception_with_traceback() |
| 37 |
|
| 38 |
# Verify exception has traceback |
| 39 |
exc_tb = getattr(exc, '__traceback__', None) |
| 40 |
assert exc_tb is not None, "Exception should have traceback" |
| 41 |
|
| 42 |
# Count initial frames |
| 43 |
initial_frames = [] |
| 44 |
tb = exc_tb |
| 45 |
while tb is not None: |
| 46 |
initial_frames.append(tb.tb_frame) |
| 47 |
tb = tb.tb_next |
| 48 |
|
| 49 |
assert len(initial_frames) > 0, "Should have traceback frames" |
| 50 |
|
| 51 |
# Verify frames have local variables before clearing |
| 52 |
frame_locals_before = [] |
| 53 |
for frame in initial_frames: |
| 54 |
frame_locals_before.append(len(frame.f_locals)) |
| 55 |
|
| 56 |
assert any(count > 0 for count in frame_locals_before), "Frames should have local variables" |
| 57 |
|
| 58 |
# Call traceback_clear with the exception - this should use exc argument |
| 59 |
traceback_clear(exc) |
| 60 |
|
| 61 |
# Verify frames are cleared |
| 62 |
exc_tb_after = getattr(exc, '__traceback__', None) |
| 63 |
assert exc_tb_after is not None, "Traceback should still exist after clearing" |
| 64 |
|
| 65 |
tb = exc_tb_after |
| 66 |
frames_after = [] |
| 67 |
while tb is not None: |
| 68 |
frames_after.append(tb.tb_frame) |
| 69 |
tb = tb.tb_next |
| 70 |
|
| 71 |
# Check that frame locals are cleared |
| 72 |
cleared_count = 0 |
| 73 |
for frame in frames_after: |
| 74 |
if len(frame.f_locals) == 0: |
| 75 |
cleared_count += 1 |
| 76 |
|
| 77 |
assert cleared_count == len(frames_after), "All frames should be cleared" |
| 78 |
|
| 79 |
# Verify the function actually used the exc argument by checking traceback still exists |
| 80 |
assert getattr(exc, '__traceback__', None) is not None, ( |
| 81 |
"Traceback should still exist but frames should be cleared" |
| 82 |
) |
| 83 |
|
| 84 |
def test_without_exc_argument(self): |
| 85 |
"""Test traceback_clear() without exc argument uses sys.exc_info().""" |
| 86 |
try: |
| 87 |
def test_function(): |
| 88 |
local_var = "test" * 1000 # noqa: F841 |
| 89 |
raise RuntimeError("Test exception") |
| 90 |
|
| 91 |
test_function() |
| 92 |
except Exception: |
| 93 |
# Now we're in except block with active traceback |
| 94 |
_, _, tb_before = sys.exc_info() |
| 95 |
assert tb_before is not None, "Should have active traceback" |
| 96 |
|
| 97 |
# Call traceback_clear without argument - should use sys.exc_info() |
| 98 |
traceback_clear() |
| 99 |
# Test passes if no exception is raised |
| 100 |
|
| 101 |
def test_with_none(self): |
| 102 |
"""Test traceback_clear(None) uses sys.exc_info() fallback.""" |
| 103 |
try: |
| 104 |
def test_function(): |
| 105 |
local_var = "test" * 1000 # noqa: F841 |
| 106 |
raise RuntimeError("Test exception") |
| 107 |
|
| 108 |
test_function() |
| 109 |
except Exception: |
| 110 |
# Call with None - should fall back to sys.exc_info() |
| 111 |
traceback_clear(None) |
| 112 |
# Test passes if no exception is raised |
| 113 |
|
| 114 |
def test_with_exception_no_traceback(self): |
| 115 |
"""Test traceback_clear with exception that has no __traceback__.""" |
| 116 |
# Create exception without traceback |
| 117 |
exc = ValueError("Test exception") |
| 118 |
|
| 119 |
# Should not raise exception |
| 120 |
traceback_clear(exc) |
| 121 |
|
| 122 |
def test_handles_runtime_error(self): |
| 123 |
"""Test that traceback_clear handles RuntimeError when frame is executing.""" |
| 124 |
# This test is mainly for coverage - RuntimeError handling is internal |
| 125 |
# and difficult to trigger in normal circumstances |
| 126 |
try: |
| 127 |
def test_function(): |
| 128 |
local_var = "test" * 1000 # noqa: F841 |
| 129 |
raise RuntimeError("Test exception") |
| 130 |
|
| 131 |
test_function() |
| 132 |
except Exception as exc: |
| 133 |
# Should not raise exception even if RuntimeError occurs internally |
| 134 |
traceback_clear(exc) |