def should_stop_on_exception(self, frame, event, arg): mainDebugger, _filename, info, thread = self._args flag = False if info.pydev_state != STATE_SUSPEND: #and breakpoint is not None: exception, value, trace = arg if trace is not None: #on jython trace is None on the first event exception_breakpoint = get_exception_breakpoint( exception, mainDebugger.break_on_caught_exceptions) if exception_breakpoint is not None: if not exception_breakpoint.notify_on_first_raise_only or just_raised(trace): # print frame.f_code.co_name if exception_breakpoint.ignore_libraries: if mainDebugger.not_in_scope(frame.f_code.co_filename): pydev_log.debug("Ignore exception %s in library %s" % (exception, frame.f_code.co_filename)) return False, frame add_exception_to_frame(frame, (exception, value, trace)) thread.additionalInfo.message = exception_breakpoint.qname flag = True else: flag = False else: try: if mainDebugger.plugin is not None: result = mainDebugger.plugin.exception_break(mainDebugger, self, frame, self._args, arg) if result: (flag, frame) = result except: flag = False return flag, frame
def patch_arg_str_win(arg_str): args = str_to_args_windows(arg_str) if not is_python(args[0]): return arg_str arg_str = args_to_str(patch_args(args)) pydev_log.debug("New args: %s" % arg_str) return arg_str
def patch_args(args): try: pydev_log.debug("Patching args: %s"% str(args)) import sys new_args = [] i = 0 if len(args) == 0: return args if is_python(args[0]): try: indC = args.index('-c') except ValueError: indC = -1 if indC != -1: import pydevd host, port = pydevd.dispatch() if port is not None: new_args.extend(args) new_args[indC + 1] = ("import sys; sys.path.append(r'%s'); import pydevd; " "pydevd.settrace(host='%s', port=%s, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True); %s") % ( pydev_src_dir, host, port, args[indC + 1]) return new_args else: new_args.append(args[0]) else: pydev_log.debug("Process is not python, returning.") return args i = 1 while i < len(args): if args[i].startswith('-'): new_args.append(args[i]) else: break i += 1 if args[i].endswith('pydevd.py'): #no need to add pydevd twice return args for x in sys.original_argv: if sys.platform == "win32" and not x.endswith('"'): arg = '"%s"' % x else: arg = x new_args.append(arg) if x == '--file': break while i < len(args): new_args.append(args[i]) i += 1 return new_args except: traceback.print_exc() return args
def _excepthook(exctype, value, tb): global _handle_exceptions if _handle_exceptions: exception_breakpoint = get_exception_breakpoint(exctype, _handle_exceptions) else: exception_breakpoint = None #Always call the original excepthook before going on to call the debugger post mortem to show it. _original_excepthook(exctype, value, tb) if not exception_breakpoint: return if tb is None: #sometimes it can be None, e.g. with GTK return frames = [] while tb: frames.append(tb.tb_frame) tb = tb.tb_next thread = threadingCurrentThread() frames_byid = dict([(id(frame),frame) for frame in frames]) frame = frames[-1] thread.additionalInfo.exception = (exctype, value, tb) thread.additionalInfo.pydev_force_stop_at_exception = (frame, frames_byid) thread.additionalInfo.message = exception_breakpoint.qname debugger = GetGlobalDebugger() pydevd_tracing.SetTrace(None) #no tracing from here pydev_log.debug('Handling post-mortem stop on exception breakpoint %s'% exception_breakpoint.qname) debugger.handle_post_mortem_stop(thread.additionalInfo, thread)
def patch_arg_str_win(arg_str): new_arg_str = arg_str.replace('\\', '/') args = str_to_args(new_arg_str) if not is_python(args[0]): return arg_str arg_str = args_to_str(patch_args(args)) pydev_log.debug("New args: %s"% arg_str) return arg_str
def _get_project_roots(project_roots_cache=[]): # Note: the project_roots_cache is the same instance among the many calls to the method if not project_roots_cache: roots = os.getenv('IDE_PROJECT_ROOTS', '').split(os.pathsep) pydev_log.debug("IDE_PROJECT_ROOTS %s\n" % roots) new_roots = [] for root in roots: new_roots.append(os.path.normcase(root)) project_roots_cache.append(new_roots) return project_roots_cache[-1] # returns the project roots with case normalized
def patch_args(args): try: pydev_log.debug("Patching args: %s" % str(args)) import sys new_args = [] i = 0 if len(args) == 0: return args if is_python(args[0]): if '-c' == args[1]: import pydevd host, port = pydevd.dispatch() if port is not None: args[ 2] = "import sys; sys.path.append('%s'); import pydevd; pydevd.settrace(host='%s', port=%s, suspend=False); %s" % ( helpers, host, port, args[2]) return args else: new_args.append(args[0]) else: pydev_log.debug("Process is not python, returning.") return args i = 1 while i < len(args): if args[i].startswith('-'): new_args.append(args[i]) else: break i += 1 if args[i].endswith('pydevd.py'): #no need to add pydevd twice return args for x in sys.original_argv: if sys.platform == "win32" and not x.endswith('"'): arg = '"%s"' % x else: arg = x new_args.append(arg) if x == '--file': break while i < len(args): new_args.append(args[i]) i += 1 return new_args except: traceback.print_exc() return args
def _get_project_roots(project_roots_cache=[]): # Note: the project_roots_cache is the same instance among the many calls to the method if not project_roots_cache: roots = os.getenv('IDE_PROJECT_ROOTS', '').split(os.pathsep) pydev_log.debug("IDE_PROJECT_ROOTS %s\n" % roots) new_roots = [] for root in roots: new_roots.append(os.path.normcase(root)) project_roots_cache.append(new_roots) return project_roots_cache[ -1] # returns the project roots with case normalized
def should_stop_on_django_breakpoint(self, frame, event, arg): mainDebugger = self._args[0] thread = self._args[3] flag = False template_frame_file = get_template_file_name(frame) #pydev_log.debug("Django is rendering a template: %s\n" % template_frame_file) django_breakpoints_for_file = mainDebugger.django_breakpoints.get(template_frame_file) if django_breakpoints_for_file: #pydev_log.debug("Breakpoints for that file: %s\n" % django_breakpoints_for_file) template_frame_line = get_template_line(frame, template_frame_file) #pydev_log.debug("Tracing template line: %d\n" % template_frame_line) if DictContains(django_breakpoints_for_file, template_frame_line): django_breakpoint = django_breakpoints_for_file[template_frame_line] if django_breakpoint.is_triggered(template_frame_file, template_frame_line): #pydev_log.debug("Breakpoint is triggered.\n") flag = True new_frame = DjangoTemplateFrame( frame, template_frame_file=template_frame_file, template_frame_line=template_frame_line, ) if django_breakpoint.condition is not None: try: val = eval(django_breakpoint.condition, new_frame.f_globals, new_frame.f_locals) if not val: flag = False pydev_log.debug("Condition '%s' is evaluated to %s. Not suspending.\n" % (django_breakpoint.condition, val)) except: pydev_log.info( 'Error while evaluating condition \'%s\': %s\n' % (django_breakpoint.condition, sys.exc_info()[1])) if django_breakpoint.expression is not None: try: try: val = eval(django_breakpoint.expression, new_frame.f_globals, new_frame.f_locals) except: val = sys.exc_info()[1] finally: if val is not None: thread.additionalInfo.message = val if flag: frame = suspend_django(self, mainDebugger, thread, frame) return flag, frame
def get_source(frame): try: node = frame.f_locals['self'] if hasattr(node, 'source'): return node.source else: pydev_log.error_once("WARNING: Template path is not available. Please set TEMPLATE_DEBUG=True in your settings.py to make django template breakpoints working") return None except: pydev_log.debug(traceback.format_exc()) return None
def patch_args(args): try: pydev_log.debug("Patching args: %s"% str(args)) import sys new_args = [] i = 0 if len(args) == 0: return args if is_python(args[0]): if '-c' == args[1]: import pydevd host, port = pydevd.dispatch() if port is not None: args[2] = "import sys; sys.path.append('%s'); import pydevd; pydevd.settrace(host='%s', port=%s, suspend=False); %s"%(helpers, host, port, args[2]) return args else: new_args.append(args[0]) else: pydev_log.debug("Process is not python, returning.") return args i = 1 while i < len(args): if args[i].startswith('-'): new_args.append(args[i]) else: break i+=1 if args[i].endswith('pydevd.py'): #no need to add pydevd twice return args for x in sys.original_argv: if sys.platform == "win32" and not x.endswith('"'): arg = '"%s"'%x else: arg = x new_args.append(arg) if x == '--file': break while i < len(args): new_args.append(args[i]) i+=1 return new_args except: traceback.print_exc() return args
def get_source(frame): try: node = frame.f_locals['self'] if hasattr(node, 'source'): return node.source else: pydev_log.error_once( "WARNING: Template path is not available. Please set TEMPLATE_DEBUG=True in your settings.py to make django template breakpoints working" ) return None except: pydev_log.debug(traceback.format_exc()) return None
def should_stop_on_exception(self, frame, event, arg): mainDebugger, _filename, info, thread = self._args flag = False if info.pydev_state != STATE_SUSPEND: #and breakpoint is not None: exception, value, trace = arg if trace is not None: #on jython trace is None on the first event exception_breakpoint = get_exception_breakpoint( exception, mainDebugger.break_on_caught_exceptions) if exception_breakpoint is not None: if exception_breakpoint.ignore_libraries: if exception_breakpoint.notify_on_first_raise_only: if mainDebugger.first_appearance_in_scope(trace): add_exception_to_frame( frame, (exception, value, trace)) thread.additionalInfo.message = exception_breakpoint.qname flag = True else: pydev_log.debug( "Ignore exception %s in library %s" % (exception, frame.f_code.co_filename)) flag = False else: if not exception_breakpoint.notify_on_first_raise_only or just_raised( trace): add_exception_to_frame(frame, (exception, value, trace)) thread.additionalInfo.message = exception_breakpoint.qname flag = True else: flag = False else: try: if mainDebugger.plugin is not None: result = mainDebugger.plugin.exception_break( mainDebugger, self, frame, self._args, arg) if result: (flag, frame) = result except: flag = False return flag, frame
def OnRun(self): self.stopTrace() buffer = "" try: while not self.killReceived: try: r = self.sock.recv(1024) except: if not self.killReceived: self.handleExcept() return #Finished communication. #Note: the java backend is always expected to pass utf-8 encoded strings. We now work with unicode #internally and thus, we may need to convert to the actual encoding where needed (i.e.: filenames #on python 2 may need to be converted to the filesystem encoding). if hasattr(r, 'decode'): r = r.decode('utf-8') buffer += r if DebugInfoHolder.DEBUG_RECORD_SOCKET_READS: pydev_log.debug('received >>%s<<\n' % (buffer,)) if len(buffer) == 0: self.handleExcept() break while buffer.find('\n') != -1: command, buffer = buffer.split('\n', 1) args = command.split('\t', 2) try: cmd_id = int(args[0]) pydev_log.debug('Received command: %s %s\n' % (ID_TO_MEANING.get(str(cmd_id), '???'), command,)) self.processCommand(cmd_id, int(args[1]), args[2]) except: traceback.print_exc() sys.stderr.write("Can't process net command: %s\n" % command) sys.stderr.flush() except: traceback.print_exc() self.handleExcept()
def _get_source(frame): # This method is usable only for the Django <= 1.8 try: node = frame.f_locals['self'] if hasattr(node, 'source'): return node.source else: if IS_DJANGO18: # The debug setting was changed since Django 1.8 pydev_log.error_once("WARNING: Template path is not available. Set the 'debug' option in the OPTIONS of a DjangoTemplates " "backend.") else: # The debug setting for Django < 1.8 pydev_log.error_once("WARNING: Template path is not available. Please set TEMPLATE_DEBUG=True in your settings.py to make " "django template breakpoints working") return None except: pydev_log.debug(traceback.format_exc()) return None
def _get_template_file_name(frame): try: if IS_DJANGO19: # The Node source was removed since Django 1.9 if DictContains(frame.f_locals, "context"): context = frame.f_locals["context"] if ( hasattr(context, "template") and hasattr(context.template, "origin") and hasattr(context.template.origin, "name") ): return context.template.origin.name return None source = _get_source(frame) if source is None: pydev_log.debug("Source is None\n") return None fname = source[0].name if fname == "<unknown source>": pydev_log.debug("Source name is %s\n" % fname) return None else: filename, base = GetFileNameAndBaseFromFile(fname) return filename except: pydev_log.debug(traceback.format_exc()) return None
def get_breakpoint(plugin, mainDebugger, pydb_frame, frame, event, args): mainDebugger, filename, info, thread = args flag = False django_breakpoint = None new_frame = None type = "django" if ( event == "call" and info.pydev_state != STATE_SUSPEND and mainDebugger.django_breakpoints and _is_django_render_call(frame) ): filename = _get_template_file_name(frame) pydev_log.debug("Django is rendering a template: %s\n" % filename) django_breakpoints_for_file = mainDebugger.django_breakpoints.get(filename) if django_breakpoints_for_file: pydev_log.debug("Breakpoints for that file: %s\n" % django_breakpoints_for_file) template_line = _get_template_line(frame) pydev_log.debug("Tracing template line: %d\n" % template_line) if DictContains(django_breakpoints_for_file, template_line): django_breakpoint = django_breakpoints_for_file[template_line] flag = True new_frame = DjangoTemplateFrame(frame) return flag, django_breakpoint, new_frame, type
def _get_template_file_name(frame): try: if IS_DJANGO19: # The Node source was removed since Django 1.9 if DictContains(frame.f_locals, 'context'): context = frame.f_locals['context'] if hasattr(context, 'template') and hasattr(context.template, 'origin') and \ hasattr(context.template.origin, 'name'): return context.template.origin.name return None source = _get_source(frame) if source is None: pydev_log.debug("Source is None\n") return None fname = source[0].name if fname == '<unknown source>': pydev_log.debug("Source name is %s\n" % fname) return None else: filename, base = GetFileNameAndBaseFromFile(fname) return filename except: pydev_log.debug(traceback.format_exc()) return None
def OnRun(self): self.stopTrace() buffer = "" try: while not self.killReceived: try: r = self.sock.recv(1024) except: if not self.killReceived: self.handleExcept() return #Finished communication. if IS_PY3K: r = r.decode('utf-8') buffer += r if DebugInfoHolder.DEBUG_RECORD_SOCKET_READS: pydev_log.debug('received >>%s<<\n' % (buffer, )) if len(buffer) == 0: self.handleExcept() break while buffer.find('\n') != -1: command, buffer = buffer.split('\n', 1) pydev_log.debug('Received command: >>%s<<\n' % (command, )) args = command.split('\t', 2) try: self.processCommand(int(args[0]), int(args[1]), args[2]) except: traceback.print_exc() sys.stderr.write("Can't process net command: %s\n" % command) sys.stderr.flush() except: traceback.print_exc() self.handleExcept()
def OnRun(self): self.stopTrace() buffer = "" try: while not self.killReceived: try: r = self.sock.recv(1024) except: if not self.killReceived: self.handleExcept() return # Finished communication. if IS_PY3K: r = r.decode("utf-8") buffer += r if DebugInfoHolder.DEBUG_RECORD_SOCKET_READS: pydev_log.debug("received >>%s<<\n" % (buffer,)) if len(buffer) == 0: self.handleExcept() break while buffer.find("\n") != -1: command, buffer = buffer.split("\n", 1) args = command.split("\t", 2) try: cmd_id = int(args[0]) pydev_log.debug("Received command: %s %s\n" % (ID_TO_MEANING.get(str(cmd_id), "???"), command)) self.processCommand(cmd_id, int(args[1]), args[2]) except: traceback.print_exc() sys.stderr.write("Can't process net command: %s\n" % command) sys.stderr.flush() except: traceback.print_exc() self.handleExcept()
def OnRun(self): self.stopTrace() buffer = "" try: while not self.killReceived: try: r = self.sock.recv(1024) except: if not self.killReceived: self.handleExcept() return #Finished communication. if IS_PY3K: r = r.decode('utf-8') buffer += r if DebugInfoHolder.DEBUG_RECORD_SOCKET_READS: pydev_log.debug('received >>%s<<\n' % (buffer,)) if len(buffer) == 0: self.handleExcept() break while buffer.find('\n') != -1: command, buffer = buffer.split('\n', 1) pydev_log.debug('Received command: >>%s<<\n' % (command,)) args = command.split('\t', 2) try: self.processCommand(int(args[0]), int(args[1]), args[2]) except: traceback.print_exc() sys.stderr.write("Can't process net command: %s\n" % command) sys.stderr.flush() except: traceback.print_exc() self.handleExcept()
def _get_source(frame): # This method is usable only for the Django <= 1.8 try: node = frame.f_locals["self"] if hasattr(node, "source"): return node.source else: if IS_DJANGO18: # The debug setting was changed since Django 1.8 pydev_log.error_once( "WARNING: Template path is not available. Set the 'debug' option in the OPTIONS of a DjangoTemplates " "backend." ) else: # The debug setting for Django < 1.8 pydev_log.error_once( "WARNING: Template path is not available. Please set TEMPLATE_DEBUG=True in your settings.py to make " "django template breakpoints working" ) return None except: pydev_log.debug(traceback.format_exc()) return None
def get_template_file_name(frame): try: source = get_source(frame) if source is None: pydev_log.debug("Source is None\n") return None fname = source[0].name pydev_log.debug("Source name is %s\n" % fname) filename, base = GetFileNameAndBaseFromFile(fname) return filename except: pydev_log.debug(traceback.format_exc()) return None
def get_breakpoint(mainDebugger, pydb_frame, frame, event, args): mainDebugger, filename, info, thread = args flag = False django_breakpoint = None new_frame = None if event == 'call' and info.pydev_state != STATE_SUSPEND and hasattr(mainDebugger, 'django_breakpoints') and \ mainDebugger.django_breakpoints and cached_call(pydb_frame, is_django_render_call, frame): filename = get_template_file_name(frame) pydev_log.debug("Django is rendering a template: %s\n" % filename) django_breakpoints_for_file = mainDebugger.django_breakpoints.get(filename) if django_breakpoints_for_file: pydev_log.debug("Breakpoints for that file: %s\n" % django_breakpoints_for_file) template_line = get_template_line(frame) pydev_log.debug("Tracing template line: %d\n" % template_line) if DictContains(django_breakpoints_for_file, template_line): django_breakpoint = django_breakpoints_for_file[template_line] flag = True new_frame = DjangoTemplateFrame(frame) return flag, django_breakpoint, new_frame
def get_breakpoint(plugin, mainDebugger, pydb_frame, frame, event, args): mainDebugger, filename, info, thread = args flag = False django_breakpoint = None new_frame = None type = 'django' if event == 'call' and info.pydev_state != STATE_SUSPEND and \ mainDebugger.django_breakpoints and _is_django_render_call(frame): filename = _get_template_file_name(frame) pydev_log.debug("Django is rendering a template: %s\n" % filename) django_breakpoints_for_file = mainDebugger.django_breakpoints.get(filename) if django_breakpoints_for_file: pydev_log.debug("Breakpoints for that file: %s\n" % django_breakpoints_for_file) template_line = _get_template_line(frame) pydev_log.debug("Tracing template line: %d\n" % template_line) if DictContains(django_breakpoints_for_file, template_line): django_breakpoint = django_breakpoints_for_file[template_line] flag = True new_frame = DjangoTemplateFrame(frame) return flag, django_breakpoint, new_frame, type
def trace(self, file, line, func_name): if DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS > 0: pydev_log.debug("Added breakpoint:%s - line:%s - func_name:%s\n" % (file, line, func_name)) sys.stderr.flush()
def log_debug(msg): import pydev_log pydev_log.debug(msg)
def trace(self, file, line, func_name): if DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS > 0: pydev_log.debug('Added breakpoint:%s - line:%s - func_name:%s\n' % (file, line, func_name)) sys.stderr.flush()