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