16 import time |
16 import time |
17 from inspect import CO_GENERATOR |
17 from inspect import CO_GENERATOR |
18 |
18 |
19 from BreakpointWatch import Breakpoint, Watch |
19 from BreakpointWatch import Breakpoint, Watch |
20 |
20 |
21 if sys.version_info[0] == 2: |
21 import _thread |
22 import thread as _thread |
22 from DebugUtilities import getargvalues, formatargvalues |
23 from inspect import getargvalues, formatargvalues |
|
24 else: |
|
25 import _thread |
|
26 from DebugUtilities import getargvalues, formatargvalues |
|
27 unicode = str |
|
28 basestring = str |
|
29 |
23 |
30 gRecursionLimit = 64 |
24 gRecursionLimit = 64 |
31 |
25 |
32 |
26 |
33 def printerr(s): |
27 def printerr(s): |
167 try: |
161 try: |
168 if "__pypy__" in sys.builtin_module_names: |
162 if "__pypy__" in sys.builtin_module_names: |
169 import __pypy__ |
163 import __pypy__ |
170 __pypy__.locals_to_fast(cf) |
164 __pypy__.locals_to_fast(cf) |
171 return |
165 return |
172 except Exception: |
166 except Exception: # secok |
173 pass |
167 pass |
174 |
168 |
175 ctypes.pythonapi.PyFrame_LocalsToFast( |
169 ctypes.pythonapi.PyFrame_LocalsToFast( |
176 ctypes.py_object(cf), |
170 ctypes.py_object(cf), |
177 ctypes.c_int(0)) |
171 ctypes.c_int(0)) |
381 @type frame object |
375 @type frame object |
382 """ |
376 """ |
383 if frame is None: |
377 if frame is None: |
384 frame = sys._getframe().f_back # Skip set_trace method |
378 frame = sys._getframe().f_back # Skip set_trace method |
385 |
379 |
386 if sys.version_info[0] == 2: |
380 stopOnHandleCommand = self._dbgClient.handleJsonCommand.__code__ |
387 stopOnHandleCommand = self._dbgClient.handleJsonCommand.func_code |
|
388 else: |
|
389 stopOnHandleCommand = self._dbgClient.handleJsonCommand.__code__ |
|
390 |
381 |
391 frame.f_trace = self.trace_dispatch |
382 frame.f_trace = self.trace_dispatch |
392 while frame.f_back is not None: |
383 while frame.f_back is not None: |
393 # stop at eric's debugger frame or a threading bootstrap |
384 # stop at eric's debugger frame or a threading bootstrap |
394 if (frame.f_back.f_code == stopOnHandleCommand): |
385 if (frame.f_back.f_code == stopOnHandleCommand): |
457 # function call. This is ensured by setting stop_everywhere. |
448 # function call. This is ensured by setting stop_everywhere. |
458 self.stop_everywhere = True |
449 self.stop_everywhere = True |
459 sys.settrace(self.trace_dispatch) |
450 sys.settrace(self.trace_dispatch) |
460 |
451 |
461 try: |
452 try: |
462 exec(cmd, globalsDict, localsDict) |
453 exec(cmd, globalsDict, localsDict) # secok |
463 atexit._run_exitfuncs() |
454 atexit._run_exitfuncs() |
464 self._dbgClient.progTerminated(0) |
455 self._dbgClient.progTerminated(0) |
465 except SystemExit: |
456 except SystemExit: |
466 atexit._run_exitfuncs() |
457 atexit._run_exitfuncs() |
467 excinfo = sys.exc_info() |
458 excinfo = sys.exc_info() |
619 frame.f_code.co_firstlineno] = False |
610 frame.f_code.co_firstlineno] = False |
620 return False |
611 return False |
621 lineNo = frame.f_code.co_firstlineno |
612 lineNo = frame.f_code.co_firstlineno |
622 lineNumbers = [lineNo] |
613 lineNumbers = [lineNo] |
623 |
614 |
624 if sys.version_info[0] == 2: |
615 co_lnotab = frame.f_code.co_lnotab[1::2] |
625 co_lnotab = map(ord, frame.f_code.co_lnotab[1::2]) |
|
626 else: |
|
627 co_lnotab = frame.f_code.co_lnotab[1::2] |
|
628 |
616 |
629 # No need to handle special case if a lot of lines between |
617 # No need to handle special case if a lot of lines between |
630 # (e.g. closure), because the additional lines won't cause a bp |
618 # (e.g. closure), because the additional lines won't cause a bp |
631 for co_lno in co_lnotab: |
619 for co_lno in co_lnotab: |
632 if co_lno >= 0x80: |
620 if co_lno >= 0x80: |
807 # ignore these |
795 # ignore these |
808 return |
796 return |
809 |
797 |
810 if exctype in [SyntaxError, IndentationError]: |
798 if exctype in [SyntaxError, IndentationError]: |
811 try: |
799 try: |
812 # tuple could only occure on Python 2, but not always! |
|
813 if type(excval) == tuple: |
800 if type(excval) == tuple: |
814 message, details = excval |
801 message, details = excval |
815 filename, lineno, charno, text = details |
802 filename, lineno, charno, text = details |
816 else: |
803 else: |
817 message = excval.msg |
804 message = excval.msg |
868 if unhandled: |
855 if unhandled: |
869 exctypetxt = "unhandled {0!s}".format(str(exctype)) |
856 exctypetxt = "unhandled {0!s}".format(str(exctype)) |
870 else: |
857 else: |
871 exctypetxt = str(exctype) |
858 exctypetxt = str(exctype) |
872 |
859 |
873 if sys.version_info[0] == 2: |
860 excvaltxt = str(excval) |
874 try: |
|
875 excvaltxt = unicode(excval).encode(self._dbgClient.getCoding()) |
|
876 except UnicodeError: |
|
877 excvaltxt = str(excval) |
|
878 else: |
|
879 excvaltxt = str(excval) |
|
880 |
861 |
881 # Don't step into libraries, which are used by our debugger methods |
862 # Don't step into libraries, which are used by our debugger methods |
882 if exctb is not None: |
863 if exctb is not None: |
883 self.stop_everywhere = False |
864 self.stop_everywhere = False |
884 |
865 |
918 type object. |
899 type object. |
919 |
900 |
920 @param exctype type of the exception |
901 @param exctype type of the exception |
921 @return exception name (string) |
902 @return exception name (string) |
922 """ |
903 """ |
923 if sys.version_info[0] == 2: |
904 return str(exctype).replace("<class '", "").replace("'>", "") |
924 if type(exctype) in [types.ClassType, # Python up to 2.4 |
|
925 types.TypeType]: # Python 2.5+ |
|
926 return exctype.__name__ |
|
927 else: |
|
928 return exctype |
|
929 else: |
|
930 return str(exctype).replace("<class '", "").replace("'>", "") |
|
931 |
905 |
932 def __extract_stack(self, exctb): |
906 def __extract_stack(self, exctb): |
933 """ |
907 """ |
934 Private member to return a list of stack frames. |
908 Private member to return a list of stack frames. |
935 |
909 |
955 """ |
929 """ |
956 exctype, excval, exctb = excinfo |
930 exctype, excval, exctb = excinfo |
957 if excval is None: |
931 if excval is None: |
958 exitcode = 0 |
932 exitcode = 0 |
959 message = "" |
933 message = "" |
960 elif isinstance(excval, basestring): |
934 elif isinstance(excval, str): |
961 exitcode = 1 |
935 exitcode = 1 |
962 message = excval |
936 message = excval |
963 elif isinstance(excval, bytes): |
937 elif isinstance(excval, bytes): |
964 exitcode = 1 |
938 exitcode = 1 |
965 message = excval.decode() |
939 message = excval.decode() |
966 elif isinstance(excval, int): |
940 elif isinstance(excval, int): |
967 exitcode = excval |
941 exitcode = excval |
968 message = "" |
942 message = "" |
969 elif isinstance(excval, SystemExit): |
943 elif isinstance(excval, SystemExit): |
970 code = excval.code |
944 code = excval.code |
971 if isinstance(code, basestring): |
945 if isinstance(code, str): |
972 exitcode = 1 |
946 exitcode = 1 |
973 message = code |
947 message = code |
974 elif isinstance(code, bytes): |
948 elif isinstance(code, bytes): |
975 exitcode = 1 |
949 exitcode = 1 |
976 message = code.decode() |
950 message = code.decode() |