Pretty-Printing-API.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Pretty Printing API</title>
  16. <meta name="description" content="Debugging with GDB: Pretty Printing API">
  17. <meta name="keywords" content="Debugging with GDB: Pretty Printing API">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Python-API.html#Python-API" rel="up" title="Python API">
  26. <link href="Selecting-Pretty_002dPrinters.html#Selecting-Pretty_002dPrinters" rel="next" title="Selecting Pretty-Printers">
  27. <link href="Types-In-Python.html#Types-In-Python" rel="previous" title="Types In Python">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Pretty-Printing-API"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Selecting-Pretty_002dPrinters.html#Selecting-Pretty_002dPrinters" accesskey="n" rel="next">Selecting Pretty-Printers</a>, Previous: <a href="Types-In-Python.html#Types-In-Python" accesskey="p" rel="previous">Types In Python</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="Pretty-Printing-API-1"></a>
  65. <h4 class="subsubsection">23.2.2.5 Pretty Printing API</h4>
  66. <a name="index-python-pretty-printing-api"></a>
  67. <p>A pretty-printer is just an object that holds a value and implements a
  68. specific interface, defined here. An example output is provided
  69. (see <a href="Pretty-Printing.html#Pretty-Printing">Pretty Printing</a>).
  70. </p>
  71. <dl>
  72. <dt><a name="index-pretty_005fprinter_002echildren"></a>Function: <strong>pretty_printer.children</strong> <em>(self)</em></dt>
  73. <dd><p><small>GDB</small> will call this method on a pretty-printer to compute the
  74. children of the pretty-printer&rsquo;s value.
  75. </p>
  76. <p>This method must return an object conforming to the Python iterator
  77. protocol. Each item returned by the iterator must be a tuple holding
  78. two elements. The first element is the &ldquo;name&rdquo; of the child; the
  79. second element is the child&rsquo;s value. The value can be any Python
  80. object which is convertible to a <small>GDB</small> value.
  81. </p>
  82. <p>This method is optional. If it does not exist, <small>GDB</small> will act
  83. as though the value has no children.
  84. </p>
  85. <p>For efficiency, the <code>children</code> method should lazily compute its
  86. results. This will let <small>GDB</small> read as few elements as
  87. necessary, for example when various print settings (see <a href="Print-Settings.html#Print-Settings">Print Settings</a>) or <code>-var-list-children</code> (see <a href="GDB_002fMI-Variable-Objects.html#GDB_002fMI-Variable-Objects">GDB/MI Variable Objects</a>) limit the number of elements to be displayed.
  88. </p>
  89. <p>Children may be hidden from display based on the value of &lsquo;<samp>set
  90. print max-depth</samp>&rsquo; (see <a href="Print-Settings.html#Print-Settings">Print Settings</a>).
  91. </p></dd></dl>
  92. <dl>
  93. <dt><a name="index-pretty_005fprinter_002edisplay_005fhint"></a>Function: <strong>pretty_printer.display_hint</strong> <em>(self)</em></dt>
  94. <dd><p>The CLI may call this method and use its result to change the
  95. formatting of a value. The result will also be supplied to an MI
  96. consumer as a &lsquo;<samp>displayhint</samp>&rsquo; attribute of the variable being
  97. printed.
  98. </p>
  99. <p>This method is optional. If it does exist, this method must return a
  100. string or the special value <code>None</code>.
  101. </p>
  102. <p>Some display hints are predefined by <small>GDB</small>:
  103. </p>
  104. <dl compact="compact">
  105. <dt>&lsquo;<samp>array</samp>&rsquo;</dt>
  106. <dd><p>Indicate that the object being printed is &ldquo;array-like&rdquo;. The CLI
  107. uses this to respect parameters such as <code>set print elements</code> and
  108. <code>set print array</code>.
  109. </p>
  110. </dd>
  111. <dt>&lsquo;<samp>map</samp>&rsquo;</dt>
  112. <dd><p>Indicate that the object being printed is &ldquo;map-like&rdquo;, and that the
  113. children of this value can be assumed to alternate between keys and
  114. values.
  115. </p>
  116. </dd>
  117. <dt>&lsquo;<samp>string</samp>&rsquo;</dt>
  118. <dd><p>Indicate that the object being printed is &ldquo;string-like&rdquo;. If the
  119. printer&rsquo;s <code>to_string</code> method returns a Python string of some
  120. kind, then <small>GDB</small> will call its internal language-specific
  121. string-printing function to format the string. For the CLI this means
  122. adding quotation marks, possibly escaping some characters, respecting
  123. <code>set print elements</code>, and the like.
  124. </p></dd>
  125. </dl>
  126. <p>The special value <code>None</code> causes <small>GDB</small> to apply the default
  127. display rules.
  128. </p></dd></dl>
  129. <dl>
  130. <dt><a name="index-pretty_005fprinter_002eto_005fstring"></a>Function: <strong>pretty_printer.to_string</strong> <em>(self)</em></dt>
  131. <dd><p><small>GDB</small> will call this method to display the string
  132. representation of the value passed to the object&rsquo;s constructor.
  133. </p>
  134. <p>When printing from the CLI, if the <code>to_string</code> method exists,
  135. then <small>GDB</small> will prepend its result to the values returned by
  136. <code>children</code>. Exactly how this formatting is done is dependent on
  137. the display hint, and may change as more hints are added. Also,
  138. depending on the print settings (see <a href="Print-Settings.html#Print-Settings">Print Settings</a>), the CLI may
  139. print just the result of <code>to_string</code> in a stack trace, omitting
  140. the result of <code>children</code>.
  141. </p>
  142. <p>If this method returns a string, it is printed verbatim.
  143. </p>
  144. <p>Otherwise, if this method returns an instance of <code>gdb.Value</code>,
  145. then <small>GDB</small> prints this value. This may result in a call to
  146. another pretty-printer.
  147. </p>
  148. <p>If instead the method returns a Python value which is convertible to a
  149. <code>gdb.Value</code>, then <small>GDB</small> performs the conversion and prints
  150. the resulting value. Again, this may result in a call to another
  151. pretty-printer. Python scalars (integers, floats, and booleans) and
  152. strings are convertible to <code>gdb.Value</code>; other types are not.
  153. </p>
  154. <p>Finally, if this method returns <code>None</code> then no further operations
  155. are peformed in this method and nothing is printed.
  156. </p>
  157. <p>If the result is not one of these types, an exception is raised.
  158. </p></dd></dl>
  159. <p><small>GDB</small> provides a function which can be used to look up the
  160. default pretty-printer for a <code>gdb.Value</code>:
  161. </p>
  162. <a name="index-gdb_002edefault_005fvisualizer"></a>
  163. <dl>
  164. <dt><a name="index-gdb_002edefault_005fvisualizer-1"></a>Function: <strong>gdb.default_visualizer</strong> <em>(value)</em></dt>
  165. <dd><p>This function takes a <code>gdb.Value</code> object as an argument. If a
  166. pretty-printer for this value exists, then it is returned. If no such
  167. printer exists, then this returns <code>None</code>.
  168. </p></dd></dl>
  169. <hr>
  170. <div class="header">
  171. <p>
  172. Next: <a href="Selecting-Pretty_002dPrinters.html#Selecting-Pretty_002dPrinters" accesskey="n" rel="next">Selecting Pretty-Printers</a>, Previous: <a href="Types-In-Python.html#Types-In-Python" accesskey="p" rel="previous">Types In Python</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  173. </div>
  174. </body>
  175. </html>