1- import os
2- import subprocess
31import sys
42
5- import simplejson as json
6-
73from backtracepython .attributes .attribute_manager import attribute_manager
84
95from .report import BacktraceReport
6+ from .report_queue import ReportQueue
7+ from .request_handler import BacktraceRequestHandler
8+ from .source_code_handler import SourceCodeHandler
109
1110if sys .version_info .major >= 3 :
1211 from urllib .parse import urlencode
@@ -18,47 +17,30 @@ class globs:
1817 endpoint = None
1918 next_except_hook = None
2019 debug_backtrace = False
21- timeout = None
22- tab_width = None
23- attributes = {}
24- context_line_count = None
2520 worker = None
26-
27-
28- child_py_path = os .path .join (os .path .dirname (__file__ ), "child.py" )
21+ attachments = []
22+ handler = None
2923
3024
3125def get_attributes ():
3226 return attribute_manager .get ()
3327
3428
35- def send_worker_report (report , source_code ):
36- send_worker_msg (
37- {
38- "id" : "send" ,
39- "report" : report ,
40- "context_line_count" : globs .context_line_count ,
41- "timeout" : globs .timeout ,
42- "endpoint" : globs .endpoint ,
43- "tab_width" : globs .tab_width ,
44- "debug_backtrace" : globs .debug_backtrace ,
45- "source_code" : source_code ,
46- }
47- )
29+ def send (report , attachments = []):
30+ if globs .handler is None :
31+ return False
4832
49-
50- def send_worker_msg (msg ):
51- payload = json .dumps (msg , ignore_nan = True ).encode ("utf-8" )
52- globs .worker .stdin .write (payload )
53- globs .worker .stdin .write ("\n " .encode ("utf-8" ))
54- globs .worker .stdin .flush ()
33+ globs .handler .add (
34+ report .get_data (), report .get_attachments () + globs .attachments + attachments
35+ )
36+ return True
5537
5638
5739def create_and_send_report (ex_type , ex_value , ex_traceback ):
5840 report = BacktraceReport ()
5941 report .set_exception (ex_type , ex_value , ex_traceback )
6042 report .set_attribute ("error.type" , "Unhandled exception" )
61- report .send ( )
43+ globs . handler . process ( report .get_data (), globs . attachments )
6244
6345
6446def bt_except_hook (ex_type , ex_value , ex_traceback ):
@@ -88,17 +70,19 @@ def initialize(**kwargs):
8870 kwargs ["endpoint" ], kwargs .get ("token" , None )
8971 )
9072 globs .debug_backtrace = kwargs .get ("debug_backtrace" , False )
91- globs .timeout = kwargs .get ("timeout" , 4 )
92- globs .tab_width = kwargs .get ("tab_width" , 8 )
93- globs .context_line_count = kwargs .get ("context_line_count" , 200 )
94-
73+ globs .attachments = kwargs .get ("attachments" , [])
9574 attribute_manager .add (kwargs .get ("attributes" , {}))
96- stdio_value = None if globs .debug_backtrace else subprocess .PIPE
97- globs .worker = subprocess .Popen (
98- [sys .executable , child_py_path ],
99- stdin = subprocess .PIPE ,
100- stdout = stdio_value ,
101- stderr = stdio_value ,
75+
76+ globs .handler = ReportQueue (
77+ BacktraceRequestHandler (
78+ globs .endpoint ,
79+ kwargs .get ("timeout" , 4 ),
80+ kwargs .get ("ignore_ssl_certificate" , False ),
81+ globs .debug_backtrace ,
82+ ),
83+ SourceCodeHandler (
84+ kwargs .get ("tab_width" , 8 ), kwargs .get ("context_line_count" , 200 )
85+ ),
10286 )
10387
10488 disable_global_handler = kwargs .get ("disable_global_handler" , False )
@@ -123,11 +107,9 @@ def construct_submission_url(endpoint, token):
123107
124108
125109def finalize ():
126- send_worker_msg ({"id" : "terminate" })
127- if not globs .debug_backtrace :
128- globs .worker .stdout .close ()
129- globs .worker .stderr .close ()
130- globs .worker .wait ()
110+ if globs .handler is None :
111+ return
112+ globs .handler .dispose ()
131113
132114
133115def send_last_exception (** kwargs ):
@@ -139,16 +121,8 @@ def send_last_exception(**kwargs):
139121 report .send ()
140122
141123
142- def make_an_exception ():
143- try :
144- raise Exception
145- except :
146- return sys .exc_info ()
147-
148-
149124def send_report (msg , ** kwargs ):
150125 report = BacktraceReport ()
151- report .set_exception (* make_an_exception ())
152126 report .set_dict_attributes (kwargs .get ("attributes" , {}))
153127 report .set_dict_annotations (kwargs .get ("annotations" , {}))
154128 report .set_attribute ("error.message" , msg )
0 commit comments