Home | History | Annotate | Line # | Download | only in gdb.python
      1 # Copyright (C) 2008-2024 Free Software Foundation, Inc.
      2 
      3 # This program is free software; you can redistribute it and/or modify
      4 # it under the terms of the GNU General Public License as published by
      5 # the Free Software Foundation; either version 3 of the License, or
      6 # (at your option) any later version.
      7 #
      8 # This program is distributed in the hope that it will be useful,
      9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 # GNU General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU General Public License
     14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15 
     16 # This file is part of the GDB testsuite.  It tests GDB's handling of
     17 # bad python pretty printers.
     18 
     19 # Test a printer with a bad children iterator.
     20 
     21 import re
     22 
     23 import gdb.printing
     24 
     25 
     26 class BadChildrenContainerPrinter1(object):
     27     """Children iterator doesn't return a tuple of two elements."""
     28 
     29     def __init__(self, val):
     30         self.val = val
     31 
     32     def to_string(self):
     33         return "container %s with %d elements" % (self.val["name"], self.val["len"])
     34 
     35     @staticmethod
     36     def _bad_iterator(pointer, len):
     37         start = pointer
     38         end = pointer + len
     39         while pointer != end:
     40             yield "intentional violation of children iterator protocol"
     41             pointer += 1
     42 
     43     def children(self):
     44         return self._bad_iterator(self.val["elements"], self.val["len"])
     45 
     46 
     47 class BadChildrenContainerPrinter2(object):
     48     """Children iterator returns a tuple of two elements with bad values."""
     49 
     50     def __init__(self, val):
     51         self.val = val
     52 
     53     def to_string(self):
     54         return "container %s with %d elements" % (self.val["name"], self.val["len"])
     55 
     56     @staticmethod
     57     def _bad_iterator(pointer, len):
     58         start = pointer
     59         end = pointer + len
     60         while pointer != end:
     61             # The first argument is supposed to be a string.
     62             yield (42, "intentional violation of children iterator protocol")
     63             pointer += 1
     64 
     65     def children(self):
     66         return self._bad_iterator(self.val["elements"], self.val["len"])
     67 
     68 
     69 def build_pretty_printer():
     70     pp = gdb.printing.RegexpCollectionPrettyPrinter("bad-printers")
     71 
     72     pp.add_printer("container1", "^container$", BadChildrenContainerPrinter1)
     73     pp.add_printer("container2", "^container$", BadChildrenContainerPrinter2)
     74 
     75     return pp
     76 
     77 
     78 my_pretty_printer = build_pretty_printer()
     79 gdb.printing.register_pretty_printer(gdb, my_pretty_printer)
     80