Skip to content

Commit a1ace33

Browse files
committed
fix: remove comments
1 parent 6351964 commit a1ace33

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

posthog/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,21 @@ def capture_exception(
991991
context_mask = get_code_variables_mask_patterns_context()
992992
context_ignore = get_code_variables_ignore_patterns_context()
993993

994+
print("\n\n\nCAPTURING EXCEPTION\n\n\n")
995+
994996
enabled = (
995997
context_enabled
996998
if context_enabled is not None
997999
else self.capture_exception_code_variables
9981000
)
1001+
1002+
print("Context enabled state: ", context_enabled)
1003+
print(
1004+
"Self capture_exception_code_variables: ",
1005+
self.capture_exception_code_variables,
1006+
)
1007+
print("Computed enabled state: ", enabled)
1008+
9991009
mask_patterns = (
10001010
context_mask
10011011
if context_mask is not None

posthog/test/test_exception_capture.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def process_data():
9696
assert b"'my_number': 42" in output
9797
assert b"'my_bool': 'True'" in output
9898
assert b'"my_dict": "{\\"name\\": \\"test\\", \\"value\\": 123}"' in output
99-
# With repr() fallback, objects without custom __repr__ show full repr including module and memory address
10099
assert b"<__main__.UnserializableObject object at" in output
101100
assert b"'my_password': '$$_posthog_redacted_based_on_masking_rules_$$'" in output
102101
assert b"'__should_be_ignored':" not in output
@@ -336,7 +335,6 @@ def process_data():
336335

337336

338337
def test_code_variables_repr_fallback(tmpdir):
339-
"""Test that repr() is used for variables that can't be JSON-serialized but can be repr'd"""
340338
app = tmpdir.join("app.py")
341339
app.write(
342340
dedent(

test.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
import re
3+
from datetime import datetime, timedelta
4+
from decimal import Decimal
5+
from fractions import Fraction
6+
from posthog import Posthog
7+
8+
class CustomReprClass:
9+
def __repr__(self):
10+
return '<CustomReprClass: custom representation>'
11+
12+
class CustomObject:
13+
def __init__(self, value):
14+
self.value = value
15+
16+
def __repr__(self):
17+
return f'CustomObject(value={self.value})'
18+
19+
class CircularRef:
20+
def __init__(self):
21+
self.ref = self
22+
23+
def __repr__(self):
24+
return '<CircularRef with self-reference>'
25+
26+
posthog = Posthog(
27+
"phc_J1o2BXYxzXBHJeG2mS5hk62ijkTWk38Z385lO0MhU5w",
28+
host="http://localhost:8010",
29+
debug=True,
30+
enable_exception_autocapture=True,
31+
capture_exception_code_variables=True,
32+
project_root=os.path.dirname(os.path.abspath(__file__))
33+
)
34+
35+
def trigger_error():
36+
# Variables that can't be JSON-serialized but have useful repr()
37+
my_regex = re.compile(r'\d+')
38+
my_datetime = datetime(2024, 1, 15, 10, 30, 45)
39+
my_timedelta = timedelta(days=5, hours=3)
40+
my_decimal = Decimal('123.456')
41+
my_fraction = Fraction(3, 4)
42+
my_set = {1, 2, 3}
43+
my_frozenset = frozenset([4, 5, 6])
44+
my_bytes = b'hello bytes'
45+
my_bytearray = bytearray(b'mutable bytes')
46+
my_memoryview = memoryview(b'memory view')
47+
my_complex = complex(3, 4)
48+
my_range = range(10)
49+
my_custom = CustomReprClass()
50+
my_obj = CustomObject(42)
51+
my_circular = CircularRef()
52+
my_lambda = lambda x: x * 2
53+
my_function = trigger_error
54+
55+
1/0
56+
57+
trigger_error()

test_output.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
DEBUG:posthog:consumer is running...
2+
DEBUG:posthog:queueing: {'properties': {'$exception_type': 'ZeroDivisionError', '$exception_message': 'division by zero', '$exception_list': [{'mechanism': {'type': 'generic', 'handled': True}, 'module': None, 'type': 'ZeroDivisionError', 'value': 'division by zero', 'stacktrace': {'frames': [{'platform': 'python', 'filename': 'test.py', 'abs_path': '/Users/ablaszkiewicz/Documents/repos/posthog-python/test.py', 'function': '<module>', 'module': '__main__', 'lineno': 57, 'pre_context': [' my_lambda = lambda x: x * 2', ' my_function = trigger_error', ' ', ' 1/0', ''], 'context_line': 'trigger_error()', 'post_context': [], 'in_app': True, 'code_variables': {'CircularRef': "<class '__main__.CircularRef'>", 'CustomObject': "<class '__main__.CustomObject'>", 'CustomReprClass': "<class '__main__.CustomReprClass'>", 'Decimal': "<class 'decimal.Decimal'>", 'Fraction': "<class 'fractions.Fraction'>", 'Posthog': "<class 'posthog.Posthog'>", 'datetime': "<class 'datetime.datetime'>", 'os': "<module 'os' (frozen)>", 'posthog': '<posthog.Posthog object at 0x105b4a350>', 're': "<module 're' from '/Users/ablaszkiewicz/.local/share/uv/python/cpython-3.11.12-macos-aarch64-none/lib/python3.11/re/__init__.py'>", 'timedelta': "<class 'datetime.timedelta'>", 'trigger_error': '<function trigger_error at 0x1077a0720>'}}, {'platform': 'python', 'filename': 'test.py', 'abs_path': '/Users/ablaszkiewicz/Documents/repos/posthog-python/test.py', 'function': 'trigger_error', 'module': '__main__', 'lineno': 55, 'pre_context': [' my_obj = CustomObject(42)', ' my_circular = CircularRef()', ' my_lambda = lambda x: x * 2', ' my_function = trigger_error', ' '], 'context_line': ' 1/0', 'post_context': ['', 'trigger_error()'], 'in_app': True, 'code_variables': {'my_bytearray': "bytearray(b'mutable bytes')", 'my_bytes': "b'hello bytes'", 'my_circular': '<CircularRef with self-reference>', 'my_complex': '(3+4j)', 'my_custom': '<CustomReprClass: custom representation>', 'my_datetime': 'datetime.datetime(2024, 1, 15, 10, 30, 45)', 'my_decimal': "Decimal('123.456')", 'my_fraction': 'Fraction(3, 4)', 'my_frozenset': 'frozenset({4, 5, 6})', 'my_function': '<function trigger_error at 0x1077a0720>', 'my_lambda': '<function trigger_error.<locals>.<lambda> at 0x1077a07c0>', 'my_memoryview': '<memory at 0x1076d2500>', 'my_obj': 'CustomObject(value=42)', 'my_range': 'range(0, 10)', 'my_regex': "re.compile('\\\\d+')", 'my_set': '{1, 2, 3}', 'my_timedelta': 'datetime.timedelta(days=5, seconds=10800)'}}], 'type': 'raw'}}], '$python_runtime': 'CPython', '$python_version': '3.11.12', '$os': 'Mac OS X', '$os_version': '15.6.1', '$process_person_profile': False, '$lib': 'posthog-python', '$lib_version': '6.9.1', '$geoip_disable': True}, 'timestamp': '2025-11-15T09:17:48.228041+00:00', 'distinct_id': '0d6d2c5b-7dde-4066-a691-b005b13e8220', 'event': '$exception', 'uuid': 'd96626d6-a5b7-4d50-8e2a-0b957826aea5'}
3+
DEBUG:posthog:enqueued $exception.
4+
Traceback (most recent call last):
5+
File "/Users/ablaszkiewicz/Documents/repos/posthog-python/test.py", line 57, in <module>
6+
trigger_error()
7+
File "/Users/ablaszkiewicz/Documents/repos/posthog-python/test.py", line 55, in trigger_error
8+
1/0
9+
~^~
10+
ZeroDivisionError: division by zero
11+
DEBUG:posthog:making request: {"batch": [{"properties": {"$exception_type": "ZeroDivisionError", "$exception_message": "division by zero", "$exception_list": [{"mechanism": {"type": "generic", "handled": true}, "module": null, "type": "ZeroDivisionError", "value": "division by zero", "stacktrace": {"frames": [{"platform": "python", "filename": "test.py", "abs_path": "/Users/ablaszkiewicz/Documents/repos/posthog-python/test.py", "function": "<module>", "module": "__main__", "lineno": 57, "pre_context": [" my_lambda = lambda x: x * 2", " my_function = trigger_error", " ", " 1/0", ""], "context_line": "trigger_error()", "post_context": [], "in_app": true, "code_variables": {"CircularRef": "<class '__main__.CircularRef'>", "CustomObject": "<class '__main__.CustomObject'>", "CustomReprClass": "<class '__main__.CustomReprClass'>", "Decimal": "<class 'decimal.Decimal'>", "Fraction": "<class 'fractions.Fraction'>", "Posthog": "<class 'posthog.Posthog'>", "datetime": "<class 'datetime.datetime'>", "os": "<module 'os' (frozen)>", "posthog": "<posthog.Posthog object at 0x105b4a350>", "re": "<module 're' from '/Users/ablaszkiewicz/.local/share/uv/python/cpython-3.11.12-macos-aarch64-none/lib/python3.11/re/__init__.py'>", "timedelta": "<class 'datetime.timedelta'>", "trigger_error": "<function trigger_error at 0x1077a0720>"}}, {"platform": "python", "filename": "test.py", "abs_path": "/Users/ablaszkiewicz/Documents/repos/posthog-python/test.py", "function": "trigger_error", "module": "__main__", "lineno": 55, "pre_context": [" my_obj = CustomObject(42)", " my_circular = CircularRef()", " my_lambda = lambda x: x * 2", " my_function = trigger_error", " "], "context_line": " 1/0", "post_context": ["", "trigger_error()"], "in_app": true, "code_variables": {"my_bytearray": "bytearray(b'mutable bytes')", "my_bytes": "b'hello bytes'", "my_circular": "<CircularRef with self-reference>", "my_complex": "(3+4j)", "my_custom": "<CustomReprClass: custom representation>", "my_datetime": "datetime.datetime(2024, 1, 15, 10, 30, 45)", "my_decimal": "Decimal('123.456')", "my_fraction": "Fraction(3, 4)", "my_frozenset": "frozenset({4, 5, 6})", "my_function": "<function trigger_error at 0x1077a0720>", "my_lambda": "<function trigger_error.<locals>.<lambda> at 0x1077a07c0>", "my_memoryview": "<memory at 0x1076d2500>", "my_obj": "CustomObject(value=42)", "my_range": "range(0, 10)", "my_regex": "re.compile('\\\\d+')", "my_set": "{1, 2, 3}", "my_timedelta": "datetime.timedelta(days=5, seconds=10800)"}}], "type": "raw"}}], "$python_runtime": "CPython", "$python_version": "3.11.12", "$os": "Mac OS X", "$os_version": "15.6.1", "$process_person_profile": false, "$lib": "posthog-python", "$lib_version": "6.9.1", "$geoip_disable": true}, "timestamp": "2025-11-15T09:17:48.228041+00:00", "distinct_id": "0d6d2c5b-7dde-4066-a691-b005b13e8220", "event": "$exception", "uuid": "d96626d6-a5b7-4d50-8e2a-0b957826aea5"}], "historical_migration": false, "sentAt": "2025-11-15T09:17:48.730239+00:00", "api_key": "phc_J1o2BXYxzXBHJeG2mS5hk62ijkTWk38Z385lO0MhU5w"} to url: http://localhost:8010/batch/
12+
DEBUG:posthog:data uploaded successfully
13+
DEBUG:posthog:data uploaded successfully
14+
DEBUG:posthog:consumer exited.
15+
16+
17+
18+
CAPTURING EXCEPTION
19+
20+
21+
22+
Context enabled state: None
23+
Self capture_exception_code_variables: True
24+
Computed enabled state: True
25+
Trying to attach code variables to frames
26+
tb_frames found
27+
exception found
28+
variables found
29+
variables: {'CircularRef': "<class '__main__.CircularRef'>", 'CustomObject': "<class '__main__.CustomObject'>", 'CustomReprClass': "<class '__main__.CustomReprClass'>", 'Decimal': "<class 'decimal.Decimal'>", 'Fraction': "<class 'fractions.Fraction'>", 'Posthog': "<class 'posthog.Posthog'>", 'datetime': "<class 'datetime.datetime'>", 'os': "<module 'os' (frozen)>", 'posthog': '<posthog.Posthog object at 0x105b4a350>', 're': "<module 're' from '/Users/ablaszkiewicz/.local/share/uv/python/cpython-3.11.12-macos-aarch64-none/lib/python3.11/re/__init__.py'>", 'timedelta': "<class 'datetime.timedelta'>", 'trigger_error': '<function trigger_error at 0x1077a0720>'}
30+
code_variables added
31+
variables found
32+
variables: {'my_bytearray': "bytearray(b'mutable bytes')", 'my_bytes': "b'hello bytes'", 'my_circular': '<CircularRef with self-reference>', 'my_complex': '(3+4j)', 'my_custom': '<CustomReprClass: custom representation>', 'my_datetime': 'datetime.datetime(2024, 1, 15, 10, 30, 45)', 'my_decimal': "Decimal('123.456')", 'my_fraction': 'Fraction(3, 4)', 'my_frozenset': 'frozenset({4, 5, 6})', 'my_function': '<function trigger_error at 0x1077a0720>', 'my_lambda': '<function trigger_error.<locals>.<lambda> at 0x1077a07c0>', 'my_memoryview': '<memory at 0x1076d2500>', 'my_obj': 'CustomObject(value=42)', 'my_range': 'range(0, 10)', 'my_regex': "re.compile('\\\\d+')", 'my_set': '{1, 2, 3}', 'my_timedelta': 'datetime.timedelta(days=5, seconds=10800)'}
33+
code_variables added

0 commit comments

Comments
 (0)