def format_stack_entry(self, frame_lineno, lprefix=': '): entry = pdb.Pdb.format_stack_entry(self, frame_lineno, lprefix) entry = self.try_to_decode(entry) if self.config.highlight: match = self.stack_entry_regexp.match(entry) if match: filename, lineno, other = match.groups() filename = Color.set(self.config.filename_color, filename) lineno = Color.set(self.config.line_number_color, lineno) entry = '%s(%s)%s' % (filename, lineno, other) return entry
def format_stack_entry(self, frame_lineno, lprefix=': '): entry = super(Pdb, self).format_stack_entry(frame_lineno, lprefix) entry = self.try_to_decode(entry) if self.config.highlight: match = self.stack_entry_regexp.match(entry) if match: filename, lineno, other = match.groups() filename = Color.set(self.config.filename_color, filename) lineno = Color.set(self.config.line_number_color, lineno) entry = '%s(%s)%s' % (filename, lineno, other) entry = self.stack_line_regexp.sub( lambda m: m.group(1) + self.format_source(m.group(2)).rstrip(), entry ) return entry
def do_inspect(self, arg): obj = self._getval(arg) data = OrderedDict() data['Type'] = type(obj).__name__ data['String Form'] = str(obj).strip() if hasattr(obj, '__len__'): data['Length'] = len(obj) try: data['File'] = inspect.getabsfile(obj) except TypeError: pass if (isinstance(obj, type) and hasattr(obj, '__init__') and getattr(obj, '__module__') != '__builtin__'): # Class - show definition and docstring for constructor data['Docstring'] = obj.__doc__ data['Constructor information'] = '' try: data[' Definition'] = '%s%s' % (arg, signature(obj)) except ValueError: pass data[' Docstring'] = obj.__init__.__doc__ else: try: data['Definition'] = '%s%s' % (arg, signature(obj)) except (TypeError, ValueError): pass data['Docstring'] = obj.__doc__ for key, value in data.items(): formatted_key = Color.set(Color.red, key + ':') self.stdout.write('%-28s %s\n' % (formatted_key, value))
def parseline(self, line): if line.startswith('!!'): # force the "standard" behaviour, i.e. first check for the # command, then for the variable name to display line = line[2:] return pdb.Pdb.parseline(self, line) # pdb++ "smart command mode": don't execute commands if a variable # with the name exits in the current contex; this prevents pdb to quit # if you type e.g. 'r[0]' by mystake. cmd, arg, newline = pdb.Pdb.parseline(self, line) if arg and arg.endswith('?'): if hasattr(self, 'do_' + cmd): cmd, arg = ('help', cmd) elif arg.endswith('??'): arg = cmd + arg.split('?')[0] cmd = 'source' self.do_inspect(arg) self.stdout.write('%-28s\n' % Color.set(Color.red, 'Source:')) else: arg = cmd + arg.split('?')[0] cmd = 'inspect' return cmd, arg, newline if cmd and hasattr(self, 'do_'+cmd) and (cmd in self.curframe.f_globals or cmd in self.curframe.f_locals or arg.startswith('=')): line = '!' + line return pdb.Pdb.parseline(self, line) return cmd, arg, newline
def do_inspect(self, arg): obj = self._getval(arg) data = OrderedDict() data['Type'] = type(obj).__name__ data['String Form'] = str(obj).strip() try: data['Length'] = len(obj) except TypeError: pass try: data['File'] = inspect.getabsfile(obj) except TypeError: pass if (isinstance(obj, type) and hasattr(obj, '__init__') and getattr(obj, '__module__') != '__builtin__'): # Class - show definition and docstring for constructor data['Docstring'] = obj.__doc__ data['Constructor information'] = '' try: data[' Definition'] = '%s%s' % (arg, signature(obj)) except ValueError: pass data[' Docstring'] = obj.__init__.__doc__ else: try: data['Definition'] = '%s%s' % (arg, signature(obj)) except (TypeError, ValueError): pass data['Docstring'] = obj.__doc__ for key, value in data.items(): formatted_key = Color.set(Color.red, key + ':') self.stdout.write('%-28s %s\n' % (formatted_key, value))
def _format_exc_for_sticky(self, exc): if len(exc) != 2: return "pdbpp: got unexpected __exception__: %r" % (exc,) exc_type, exc_value = exc s = '' try: try: s = exc_type.__name__ except AttributeError: s = str(exc_type) if exc_value is not None: s += ': ' s += str(exc_value) except KeyboardInterrupt: raise except Exception as exc: try: s += '(unprintable exception: %r)' % (exc,) except: s += '(unprintable exception)' if self.config.highlight: s = Color.set(self.config.line_number_color, s) return s
def _print_if_sticky(self): if self.sticky: if self.first_time_sticky: self.first_time_sticky = False else: self.stdout.write(CLEARSCREEN) frame, lineno = self.stack[self.curindex] filename = self.canonic(frame.f_code.co_filename) s = '> %s(%r)' % (filename, lineno) print(s, file=self.stdout) print(file=self.stdout) sticky_range = self.sticky_ranges.get(self.curframe, None) self._printlonglist(sticky_range) if '__exception__' in frame.f_locals: s = self._format_exc_for_sticky(frame.f_locals['__exception__']) if s: print(s, file=self.stdout) elif '__return__' in frame.f_locals: rv = frame.f_locals['__return__'] try: s = repr(rv) except KeyboardInterrupt: raise except: s = '(unprintable return value)' s = ' return ' + s if self.config.highlight: s = Color.set(self.config.line_number_color, s) print(s, file=self.stdout)
def _print_if_sticky(self): old_stdout = self.stdout self.stdout = self.sticky_stdout if self.sticky: if self.first_time_sticky: self.first_time_sticky = False else: self.stdout.write(CLEARSCREEN) frame, lineno = self.stack[self.curindex] filename = self.canonic(frame.f_code.co_filename) s = '> %s(%r)' % (filename, lineno) print(s, file=self.stdout) print(file=self.stdout) sticky_range = self.sticky_ranges.get(self.curframe, None) self._printlonglist(sticky_range) if '__exception__' in frame.f_locals: exc = frame.f_locals['__exception__'] if len(exc) == 2: exc_type, exc_value = exc s = '' try: try: s = exc_type.__name__ except AttributeError: s = str(exc_type) if exc_value is not None: s += ': ' s += str(exc_value) except KeyboardInterrupt: raise except: s += '(unprintable exception)' print(Color.set(self.config.line_number_color, ' ' + s), file=self.stdout) return if '__return__' in frame.f_locals: rv = frame.f_locals['__return__'] try: s = repr(rv) except KeyboardInterrupt: raise except: s = '(unprintable return value)' print(Color.set(self.config.line_number_color, ' return ' + s), file=self.stdout) self.stdout = old_stdout
def format_line(self, lineno, marker, line): lineno = '%4d' % lineno if self.config.highlight: lineno = Color.set(self.config.line_number_color, lineno) line = '%s %2s %s' % (lineno, marker, line) if self.config.highlight and marker == '->': line = setbgcolor(line, self.config.current_line_color) return line
def test_complete_attribute_colored(): compl = Completer({'a': 42}, ColorConfig) matches = compl.attr_matches('a.__') assert len(matches) > 2 expected_part = Color.set('31', '__class__') for match in matches: if expected_part in match: break else: assert False assert ' ' in matches
def _print_if_sticky(self): if self.sticky: self.stdout.write(CLEARSCREEN) frame, lineno = self.stack[self.curindex] filename = self.canonic(frame.f_code.co_filename) s = '> %s(%r)' % (filename, lineno) print(s, file=self.stdout) print(file=self.stdout) sticky_range = self.sticky_ranges.get(self.curframe, None) self._printlonglist(sticky_range) if '__exception__' in frame.f_locals: exc = frame.f_locals['__exception__'] if len(exc) == 2: exc_type, exc_value = exc s = '' try: try: s = exc_type.__name__ except AttributeError: s = str(exc_type) if exc_value is not None: s += ': ' s += str(exc_value) except KeyboardInterrupt: raise except: s += '(unprintable exception)' print(Color.set(self.config.line_number_color, ' ' + s), file=self.stdout) return if '__return__' in frame.f_locals: rv = frame.f_locals['__return__'] try: s = repr(rv) except KeyboardInterrupt: raise except: s = '(unprintable return value)' print(Color.set(self.config.line_number_color, ' return ' + s), file=self.stdout)
def test_complete_attribute_colored(): compl = Completer({'a': 42}, ColorConfig) matches = compl.attr_matches('a.__') assert len(matches) > 2 expected_color = compl.config.color_by_type.get(type(compl.__class__)) assert expected_color == '35;01' expected_part = Color.set(expected_color, '__class__') for match in matches: if expected_part in match: break else: assert False, matches assert ' ' in matches
def test_complete_attribute_colored(): class ColorConfig(DefaultConfig): use_colors = True color_by_type = {type: '31'} compl = Completer({'a': 42}, ColorConfig) matches = compl.attr_matches('a.__') for match in matches: if Color.set('31', '__class__') in match: break else: assert False assert ' ' in matches
def parseline(self, line): if line.startswith('!!'): # force the "standard" behaviour, i.e. first check for the # command, then for the variable name to display line = line[2:] return super(Pdb, self).parseline(line) # pdb++ "smart command mode": don't execute commands if a variable # with the name exits in the current contex; this prevents pdb to quit # if you type e.g. 'r[0]' by mystake. cmd, arg, newline = super(Pdb, self).parseline(line) if arg and arg.endswith('?'): if hasattr(self, 'do_' + cmd): cmd, arg = ('help', cmd) elif arg.endswith('??'): arg = cmd + arg.split('?')[0] cmd = 'source' self.do_inspect(arg) self.stdout.write('%-28s\n' % Color.set(Color.red, 'Source:')) else: arg = cmd + arg.split('?')[0] cmd = 'inspect' return cmd, arg, newline # f-strings. if (cmd == 'f' and len(newline) > 1 and (newline[1] == "'" or newline[1] == '"')): return super(Pdb, self).parseline('!' + line) if cmd and hasattr(self, 'do_' + cmd) and (cmd in self.curframe.f_globals or cmd in self.curframe.f_locals or arg.startswith('=')): return super(Pdb, self).parseline('!' + line) if cmd == "list" and arg.startswith("("): # heuristic: handle "list(..." as the builtin. line = '!' + line return super(Pdb, self).parseline(line) return cmd, arg, newline
def parseline(self, line): if getattr(self, "_pdbpp_executing_rc_lines", False): return super(Pdb, self).parseline(line) if line.startswith('!!'): # Force the "standard" behaviour, i.e. first check for the # command, then for the variable name to display. line = line[2:] return super(Pdb, self).parseline(line) if line.endswith('?') and not line.startswith("!"): arg = line.split('?', 1)[0] if line.endswith('??'): cmd = 'source' self.do_inspect(arg) self.stdout.write('%-28s\n' % Color.set(Color.red, 'Source:')) elif (hasattr(self, 'do_' + arg) and arg not in self.curframe.f_globals and arg not in self.curframe_locals): cmd = "help" else: cmd = "inspect" return cmd, arg, line # pdb++ "smart command mode": don't execute commands if a variable # with the name exists in the current context; # This prevents pdb to quit if you type e.g. 'r[0]' by mystake. cmd, arg, newline = super(Pdb, self).parseline(line) if cmd: # prefixed strings. if ( cmd in ("b", "f", "r", "u") and len(newline) > 1 and (newline[1] == "'" or newline[1] == '"') ): cmd, arg, newline = None, None, line elif hasattr(self, "do_" + cmd): if ( cmd in self.curframe.f_globals or cmd in self.curframe_locals or arg.startswith("=") ): cmd, arg, newline = None, None, line elif cmd == "list" and arg.startswith("("): # heuristic: handle "list(..." as the builtin. cmd, arg, newline = None, None, line # Fix cmd to not be None when used in completions. # This would trigger a TypeError (instead of AttributeError) in # Cmd.complete (https://bugs.python.org/issue35270). if cmd is None: f = sys._getframe() while f.f_back: f = f.f_back if f.f_code.co_name == "complete": cmd = "" break return cmd, arg, newline