<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>32.1. parser — Access Python parse trees — Python v2.6.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.6.5', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python v2.6.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python v2.6.5 documentation" href="../index.html" /> <link rel="up" title="32. Python Language Services" href="language.html" /> <link rel="next" title="32.2. Abstract Syntax Trees" href="ast.html" /> <link rel="prev" title="32. Python Language Services" href="language.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../modindex.html" title="Global Module Index" accesskey="M">modules</a> |</li> <li class="right" > <a href="ast.html" title="32.2. Abstract Syntax Trees" accesskey="N">next</a> |</li> <li class="right" > <a href="language.html" title="32. Python Language Services" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v2.6.5 documentation</a> »</li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="language.html" accesskey="U">32. Python Language Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-parser"> <h1>32.1. <tt class="xref docutils literal"><span class="pre">parser</span></tt> — Access Python parse trees<a class="headerlink" href="#module-parser" title="Permalink to this headline">¶</a></h1> <p id="index-482">The <tt class="xref docutils literal"><span class="pre">parser</span></tt> module provides an interface to Python’s internal parser and byte-code compiler. The primary purpose for this interface is to allow Python code to edit the parse tree of a Python expression and create executable code from this. This is better than trying to parse and modify an arbitrary Python code fragment as a string because parsing is performed in a manner identical to the code forming the application. It is also faster.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>From Python 2.5 onward, it’s much more convenient to cut in at the Abstract Syntax Tree (AST) generation and compilation stage, using the <a title="Abstract Syntax Tree classes and manipulation." class="reference external" href="ast.html#module-ast"><tt class="xref docutils literal"><span class="pre">ast</span></tt></a> module.</p> <p class="last">The <tt class="xref docutils literal"><span class="pre">parser</span></tt> module exports the names documented here also with “st” replaced by “ast”; this is a legacy from the time when there was no other AST and has nothing to do with the AST found in Python 2.5. This is also the reason for the functions’ keyword arguments being called <em>ast</em>, not <em>st</em>. The “ast” functions will be removed in Python 3.0.</p> </div> <p>There are a few things to note about this module which are important to making use of the data structures created. This is not a tutorial on editing the parse trees for Python code, but some examples of using the <tt class="xref docutils literal"><span class="pre">parser</span></tt> module are presented.</p> <p>Most importantly, a good understanding of the Python grammar processed by the internal parser is required. For full information on the language syntax, refer to <a class="reference external" href="../reference/index.html#reference-index"><em>The Python Language Reference</em></a>. The parser itself is created from a grammar specification defined in the file <tt class="docutils literal"><span class="pre">Grammar/Grammar</span></tt> in the standard Python distribution. The parse trees stored in the ST objects created by this module are the actual output from the internal parser when created by the <a title="parser.expr" class="reference internal" href="#parser.expr"><tt class="xref docutils literal"><span class="pre">expr()</span></tt></a> or <a title="parser.suite" class="reference internal" href="#parser.suite"><tt class="xref docutils literal"><span class="pre">suite()</span></tt></a> functions, described below. The ST objects created by <a title="parser.sequence2st" class="reference internal" href="#parser.sequence2st"><tt class="xref docutils literal"><span class="pre">sequence2st()</span></tt></a> faithfully simulate those structures. Be aware that the values of the sequences which are considered “correct” will vary from one version of Python to another as the formal grammar for the language is revised. However, transporting code from one Python version to another as source text will always allow correct parse trees to be created in the target version, with the only restriction being that migrating to an older version of the interpreter will not support more recent language constructs. The parse trees are not typically compatible from one version to another, whereas source code has always been forward-compatible.</p> <p>Each element of the sequences returned by <a title="parser.st2list" class="reference internal" href="#parser.st2list"><tt class="xref docutils literal"><span class="pre">st2list()</span></tt></a> or <a title="parser.st2tuple" class="reference internal" href="#parser.st2tuple"><tt class="xref docutils literal"><span class="pre">st2tuple()</span></tt></a> has a simple form. Sequences representing non-terminal elements in the grammar always have a length greater than one. The first element is an integer which identifies a production in the grammar. These integers are given symbolic names in the C header file <tt class="docutils literal"><span class="pre">Include/graminit.h</span></tt> and the Python module <a title="Constants representing internal nodes of the parse tree." class="reference external" href="symbol.html#module-symbol"><tt class="xref docutils literal"><span class="pre">symbol</span></tt></a>. Each additional element of the sequence represents a component of the production as recognized in the input string: these are always sequences which have the same form as the parent. An important aspect of this structure which should be noted is that keywords used to identify the parent node type, such as the keyword <a class="reference external" href="../reference/compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> in an <tt class="xref docutils literal"><span class="pre">if_stmt</span></tt>, are included in the node tree without any special treatment. For example, the <a class="reference external" href="../reference/compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> keyword is represented by the tuple <tt class="docutils literal"><span class="pre">(1,</span> <span class="pre">'if')</span></tt>, where <tt class="docutils literal"><span class="pre">1</span></tt> is the numeric value associated with all <tt class="xref docutils literal"><span class="pre">NAME</span></tt> tokens, including variable and function names defined by the user. In an alternate form returned when line number information is requested, the same token might be represented as <tt class="docutils literal"><span class="pre">(1,</span> <span class="pre">'if',</span> <span class="pre">12)</span></tt>, where the <tt class="docutils literal"><span class="pre">12</span></tt> represents the line number at which the terminal symbol was found.</p> <p>Terminal elements are represented in much the same way, but without any child elements and the addition of the source text which was identified. The example of the <a class="reference external" href="../reference/compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> keyword above is representative. The various types of terminal symbols are defined in the C header file <tt class="docutils literal"><span class="pre">Include/token.h</span></tt> and the Python module <a title="Constants representing terminal nodes of the parse tree." class="reference external" href="token.html#module-token"><tt class="xref docutils literal"><span class="pre">token</span></tt></a>.</p> <p>The ST objects are not required to support the functionality of this module, but are provided for three purposes: to allow an application to amortize the cost of processing complex parse trees, to provide a parse tree representation which conserves memory space when compared to the Python list or tuple representation, and to ease the creation of additional modules in C which manipulate parse trees. A simple “wrapper” class may be created in Python to hide the use of ST objects.</p> <p>The <tt class="xref docutils literal"><span class="pre">parser</span></tt> module defines functions for a few distinct purposes. The most important purposes are to create ST objects and to convert ST objects to other representations such as parse trees and compiled code objects, but there are also functions which serve to query the type of parse tree represented by an ST object.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt>Module <a title="Constants representing internal nodes of the parse tree." class="reference external" href="symbol.html#module-symbol"><tt class="xref docutils literal"><span class="pre">symbol</span></tt></a></dt> <dd>Useful constants representing internal nodes of the parse tree.</dd> <dt>Module <a title="Constants representing terminal nodes of the parse tree." class="reference external" href="token.html#module-token"><tt class="xref docutils literal"><span class="pre">token</span></tt></a></dt> <dd>Useful constants representing leaf nodes of the parse tree and functions for testing node values.</dd> </dl> </div> <div class="section" id="creating-st-objects"> <span id="creating-sts"></span><h2>32.1.1. Creating ST Objects<a class="headerlink" href="#creating-st-objects" title="Permalink to this headline">¶</a></h2> <p>ST objects may be created from source code or from a parse tree. When creating an ST object from source, different functions are used to create the <tt class="docutils literal"><span class="pre">'eval'</span></tt> and <tt class="docutils literal"><span class="pre">'exec'</span></tt> forms.</p> <dl class="function"> <dt id="parser.expr"> <tt class="descclassname">parser.</tt><tt class="descname">expr</tt><big>(</big><em>source</em><big>)</big><a class="headerlink" href="#parser.expr" title="Permalink to this definition">¶</a></dt> <dd>The <a title="parser.expr" class="reference internal" href="#parser.expr"><tt class="xref docutils literal"><span class="pre">expr()</span></tt></a> function parses the parameter <em>source</em> as if it were an input to <tt class="docutils literal"><span class="pre">compile(source,</span> <span class="pre">'file.py',</span> <span class="pre">'eval')</span></tt>. If the parse succeeds, an ST object is created to hold the internal parse tree representation, otherwise an appropriate exception is thrown.</dd></dl> <dl class="function"> <dt id="parser.suite"> <tt class="descclassname">parser.</tt><tt class="descname">suite</tt><big>(</big><em>source</em><big>)</big><a class="headerlink" href="#parser.suite" title="Permalink to this definition">¶</a></dt> <dd>The <a title="parser.suite" class="reference internal" href="#parser.suite"><tt class="xref docutils literal"><span class="pre">suite()</span></tt></a> function parses the parameter <em>source</em> as if it were an input to <tt class="docutils literal"><span class="pre">compile(source,</span> <span class="pre">'file.py',</span> <span class="pre">'exec')</span></tt>. If the parse succeeds, an ST object is created to hold the internal parse tree representation, otherwise an appropriate exception is thrown.</dd></dl> <dl class="function"> <dt id="parser.sequence2st"> <tt class="descclassname">parser.</tt><tt class="descname">sequence2st</tt><big>(</big><em>sequence</em><big>)</big><a class="headerlink" href="#parser.sequence2st" title="Permalink to this definition">¶</a></dt> <dd><p>This function accepts a parse tree represented as a sequence and builds an internal representation if possible. If it can validate that the tree conforms to the Python grammar and all nodes are valid node types in the host version of Python, an ST object is created from the internal representation and returned to the called. If there is a problem creating the internal representation, or if the tree cannot be validated, a <a title="parser.ParserError" class="reference internal" href="#parser.ParserError"><tt class="xref docutils literal"><span class="pre">ParserError</span></tt></a> exception is thrown. An ST object created this way should not be assumed to compile correctly; normal exceptions thrown by compilation may still be initiated when the ST object is passed to <a title="parser.compilest" class="reference internal" href="#parser.compilest"><tt class="xref docutils literal"><span class="pre">compilest()</span></tt></a>. This may indicate problems not related to syntax (such as a <a title="exceptions.MemoryError" class="reference external" href="exceptions.html#exceptions.MemoryError"><tt class="xref docutils literal"><span class="pre">MemoryError</span></tt></a> exception), but may also be due to constructs such as the result of parsing <tt class="docutils literal"><span class="pre">del</span> <span class="pre">f(0)</span></tt>, which escapes the Python parser but is checked by the bytecode compiler.</p> <p>Sequences representing terminal tokens may be represented as either two-element lists of the form <tt class="docutils literal"><span class="pre">(1,</span> <span class="pre">'name')</span></tt> or as three-element lists of the form <tt class="docutils literal"><span class="pre">(1,</span> <span class="pre">'name',</span> <span class="pre">56)</span></tt>. If the third element is present, it is assumed to be a valid line number. The line number may be specified for any subset of the terminal symbols in the input tree.</p> </dd></dl> <dl class="function"> <dt id="parser.tuple2st"> <tt class="descclassname">parser.</tt><tt class="descname">tuple2st</tt><big>(</big><em>sequence</em><big>)</big><a class="headerlink" href="#parser.tuple2st" title="Permalink to this definition">¶</a></dt> <dd>This is the same function as <a title="parser.sequence2st" class="reference internal" href="#parser.sequence2st"><tt class="xref docutils literal"><span class="pre">sequence2st()</span></tt></a>. This entry point is maintained for backward compatibility.</dd></dl> </div> <div class="section" id="converting-st-objects"> <span id="converting-sts"></span><h2>32.1.2. Converting ST Objects<a class="headerlink" href="#converting-st-objects" title="Permalink to this headline">¶</a></h2> <p>ST objects, regardless of the input used to create them, may be converted to parse trees represented as list- or tuple- trees, or may be compiled into executable code objects. Parse trees may be extracted with or without line numbering information.</p> <dl class="function"> <dt id="parser.st2list"> <tt class="descclassname">parser.</tt><tt class="descname">st2list</tt><big>(</big><em>ast</em><span class="optional">[</span>, <em>line_info</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#parser.st2list" title="Permalink to this definition">¶</a></dt> <dd><p>This function accepts an ST object from the caller in <em>ast</em> and returns a Python list representing the equivalent parse tree. The resulting list representation can be used for inspection or the creation of a new parse tree in list form. This function does not fail so long as memory is available to build the list representation. If the parse tree will only be used for inspection, <a title="parser.st2tuple" class="reference internal" href="#parser.st2tuple"><tt class="xref docutils literal"><span class="pre">st2tuple()</span></tt></a> should be used instead to reduce memory consumption and fragmentation. When the list representation is required, this function is significantly faster than retrieving a tuple representation and converting that to nested lists.</p> <p>If <em>line_info</em> is true, line number information will be included for all terminal tokens as a third element of the list representing the token. Note that the line number provided specifies the line on which the token <em>ends</em>. This information is omitted if the flag is false or omitted.</p> </dd></dl> <dl class="function"> <dt id="parser.st2tuple"> <tt class="descclassname">parser.</tt><tt class="descname">st2tuple</tt><big>(</big><em>ast</em><span class="optional">[</span>, <em>line_info</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#parser.st2tuple" title="Permalink to this definition">¶</a></dt> <dd><p>This function accepts an ST object from the caller in <em>ast</em> and returns a Python tuple representing the equivalent parse tree. Other than returning a tuple instead of a list, this function is identical to <a title="parser.st2list" class="reference internal" href="#parser.st2list"><tt class="xref docutils literal"><span class="pre">st2list()</span></tt></a>.</p> <p>If <em>line_info</em> is true, line number information will be included for all terminal tokens as a third element of the list representing the token. This information is omitted if the flag is false or omitted.</p> </dd></dl> <dl class="function"> <dt id="parser.compilest"> <tt class="descclassname">parser.</tt><tt class="descname">compilest</tt><big>(</big><em>ast</em><span class="optional">[</span>, <em>filename='<syntax-tree>'</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#parser.compilest" title="Permalink to this definition">¶</a></dt> <dd><p id="index-483">The Python byte compiler can be invoked on an ST object to produce code objects which can be used as part of an <a class="reference external" href="../reference/simple_stmts.html#exec"><tt class="xref docutils literal"><span class="pre">exec</span></tt></a> statement or a call to the built-in <a title="eval" class="reference external" href="functions.html#eval"><tt class="xref docutils literal"><span class="pre">eval()</span></tt></a> function. This function provides the interface to the compiler, passing the internal parse tree from <em>ast</em> to the parser, using the source file name specified by the <em>filename</em> parameter. The default value supplied for <em>filename</em> indicates that the source was an ST object.</p> <p>Compiling an ST object may result in exceptions related to compilation; an example would be a <a title="exceptions.SyntaxError" class="reference external" href="exceptions.html#exceptions.SyntaxError"><tt class="xref docutils literal"><span class="pre">SyntaxError</span></tt></a> caused by the parse tree for <tt class="docutils literal"><span class="pre">del</span> <span class="pre">f(0)</span></tt>: this statement is considered legal within the formal grammar for Python but is not a legal language construct. The <a title="exceptions.SyntaxError" class="reference external" href="exceptions.html#exceptions.SyntaxError"><tt class="xref docutils literal"><span class="pre">SyntaxError</span></tt></a> raised for this condition is actually generated by the Python byte-compiler normally, which is why it can be raised at this point by the <tt class="xref docutils literal"><span class="pre">parser</span></tt> module. Most causes of compilation failure can be diagnosed programmatically by inspection of the parse tree.</p> </dd></dl> </div> <div class="section" id="queries-on-st-objects"> <span id="querying-sts"></span><h2>32.1.3. Queries on ST Objects<a class="headerlink" href="#queries-on-st-objects" title="Permalink to this headline">¶</a></h2> <p>Two functions are provided which allow an application to determine if an ST was created as an expression or a suite. Neither of these functions can be used to determine if an ST was created from source code via <a title="parser.expr" class="reference internal" href="#parser.expr"><tt class="xref docutils literal"><span class="pre">expr()</span></tt></a> or <a title="parser.suite" class="reference internal" href="#parser.suite"><tt class="xref docutils literal"><span class="pre">suite()</span></tt></a> or from a parse tree via <a title="parser.sequence2st" class="reference internal" href="#parser.sequence2st"><tt class="xref docutils literal"><span class="pre">sequence2st()</span></tt></a>.</p> <dl class="function"> <dt id="parser.isexpr"> <tt class="descclassname">parser.</tt><tt class="descname">isexpr</tt><big>(</big><em>ast</em><big>)</big><a class="headerlink" href="#parser.isexpr" title="Permalink to this definition">¶</a></dt> <dd><p id="index-484">When <em>ast</em> represents an <tt class="docutils literal"><span class="pre">'eval'</span></tt> form, this function returns true, otherwise it returns false. This is useful, since code objects normally cannot be queried for this information using existing built-in functions. Note that the code objects created by <a title="parser.compilest" class="reference internal" href="#parser.compilest"><tt class="xref docutils literal"><span class="pre">compilest()</span></tt></a> cannot be queried like this either, and are identical to those created by the built-in <a title="compile" class="reference external" href="functions.html#compile"><tt class="xref docutils literal"><span class="pre">compile()</span></tt></a> function.</p> </dd></dl> <dl class="function"> <dt id="parser.issuite"> <tt class="descclassname">parser.</tt><tt class="descname">issuite</tt><big>(</big><em>ast</em><big>)</big><a class="headerlink" href="#parser.issuite" title="Permalink to this definition">¶</a></dt> <dd>This function mirrors <a title="parser.isexpr" class="reference internal" href="#parser.isexpr"><tt class="xref docutils literal"><span class="pre">isexpr()</span></tt></a> in that it reports whether an ST object represents an <tt class="docutils literal"><span class="pre">'exec'</span></tt> form, commonly known as a “suite.” It is not safe to assume that this function is equivalent to <tt class="docutils literal"><span class="pre">not</span> <span class="pre">isexpr(ast)</span></tt>, as additional syntactic fragments may be supported in the future.</dd></dl> </div> <div class="section" id="exceptions-and-error-handling"> <span id="st-errors"></span><h2>32.1.4. Exceptions and Error Handling<a class="headerlink" href="#exceptions-and-error-handling" title="Permalink to this headline">¶</a></h2> <p>The parser module defines a single exception, but may also pass other built-in exceptions from other portions of the Python runtime environment. See each function for information about the exceptions it can raise.</p> <dl class="exception"> <dt id="parser.ParserError"> <em class="property">exception </em><tt class="descclassname">parser.</tt><tt class="descname">ParserError</tt><a class="headerlink" href="#parser.ParserError" title="Permalink to this definition">¶</a></dt> <dd>Exception raised when a failure occurs within the parser module. This is generally produced for validation failures rather than the built in <a title="exceptions.SyntaxError" class="reference external" href="exceptions.html#exceptions.SyntaxError"><tt class="xref docutils literal"><span class="pre">SyntaxError</span></tt></a> thrown during normal parsing. The exception argument is either a string describing the reason of the failure or a tuple containing a sequence causing the failure from a parse tree passed to <a title="parser.sequence2st" class="reference internal" href="#parser.sequence2st"><tt class="xref docutils literal"><span class="pre">sequence2st()</span></tt></a> and an explanatory string. Calls to <a title="parser.sequence2st" class="reference internal" href="#parser.sequence2st"><tt class="xref docutils literal"><span class="pre">sequence2st()</span></tt></a> need to be able to handle either type of exception, while calls to other functions in the module will only need to be aware of the simple string values.</dd></dl> <p>Note that the functions <a title="parser.compilest" class="reference internal" href="#parser.compilest"><tt class="xref docutils literal"><span class="pre">compilest()</span></tt></a>, <a title="parser.expr" class="reference internal" href="#parser.expr"><tt class="xref docutils literal"><span class="pre">expr()</span></tt></a>, and <a title="parser.suite" class="reference internal" href="#parser.suite"><tt class="xref docutils literal"><span class="pre">suite()</span></tt></a> may throw exceptions which are normally thrown by the parsing and compilation process. These include the built in exceptions <a title="exceptions.MemoryError" class="reference external" href="exceptions.html#exceptions.MemoryError"><tt class="xref docutils literal"><span class="pre">MemoryError</span></tt></a>, <a title="exceptions.OverflowError" class="reference external" href="exceptions.html#exceptions.OverflowError"><tt class="xref docutils literal"><span class="pre">OverflowError</span></tt></a>, <a title="exceptions.SyntaxError" class="reference external" href="exceptions.html#exceptions.SyntaxError"><tt class="xref docutils literal"><span class="pre">SyntaxError</span></tt></a>, and <a title="exceptions.SystemError" class="reference external" href="exceptions.html#exceptions.SystemError"><tt class="xref docutils literal"><span class="pre">SystemError</span></tt></a>. In these cases, these exceptions carry all the meaning normally associated with them. Refer to the descriptions of each function for detailed information.</p> </div> <div class="section" id="st-objects"> <span id="id1"></span><h2>32.1.5. ST Objects<a class="headerlink" href="#st-objects" title="Permalink to this headline">¶</a></h2> <p>Ordered and equality comparisons are supported between ST objects. Pickling of ST objects (using the <a title="Convert Python objects to streams of bytes and back." class="reference external" href="pickle.html#module-pickle"><tt class="xref docutils literal"><span class="pre">pickle</span></tt></a> module) is also supported.</p> <dl class="data"> <dt id="parser.STType"> <tt class="descclassname">parser.</tt><tt class="descname">STType</tt><a class="headerlink" href="#parser.STType" title="Permalink to this definition">¶</a></dt> <dd>The type of the objects returned by <a title="parser.expr" class="reference internal" href="#parser.expr"><tt class="xref docutils literal"><span class="pre">expr()</span></tt></a>, <a title="parser.suite" class="reference internal" href="#parser.suite"><tt class="xref docutils literal"><span class="pre">suite()</span></tt></a> and <a title="parser.sequence2st" class="reference internal" href="#parser.sequence2st"><tt class="xref docutils literal"><span class="pre">sequence2st()</span></tt></a>.</dd></dl> <p>ST objects have the following methods:</p> <dl class="method"> <dt id="parser.ST.compile"> <tt class="descclassname">ST.</tt><tt class="descname">compile</tt><big>(</big><span class="optional">[</span><em>filename</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#parser.ST.compile" title="Permalink to this definition">¶</a></dt> <dd>Same as <tt class="docutils literal"><span class="pre">compilest(st,</span> <span class="pre">filename)</span></tt>.</dd></dl> <dl class="method"> <dt id="parser.ST.isexpr"> <tt class="descclassname">ST.</tt><tt class="descname">isexpr</tt><big>(</big><big>)</big><a class="headerlink" href="#parser.ST.isexpr" title="Permalink to this definition">¶</a></dt> <dd>Same as <tt class="docutils literal"><span class="pre">isexpr(st)</span></tt>.</dd></dl> <dl class="method"> <dt id="parser.ST.issuite"> <tt class="descclassname">ST.</tt><tt class="descname">issuite</tt><big>(</big><big>)</big><a class="headerlink" href="#parser.ST.issuite" title="Permalink to this definition">¶</a></dt> <dd>Same as <tt class="docutils literal"><span class="pre">issuite(st)</span></tt>.</dd></dl> <dl class="method"> <dt id="parser.ST.tolist"> <tt class="descclassname">ST.</tt><tt class="descname">tolist</tt><big>(</big><span class="optional">[</span><em>line_info</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#parser.ST.tolist" title="Permalink to this definition">¶</a></dt> <dd>Same as <tt class="docutils literal"><span class="pre">st2list(st,</span> <span class="pre">line_info)</span></tt>.</dd></dl> <dl class="method"> <dt id="parser.ST.totuple"> <tt class="descclassname">ST.</tt><tt class="descname">totuple</tt><big>(</big><span class="optional">[</span><em>line_info</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#parser.ST.totuple" title="Permalink to this definition">¶</a></dt> <dd>Same as <tt class="docutils literal"><span class="pre">st2tuple(st,</span> <span class="pre">line_info)</span></tt>.</dd></dl> </div> <div class="section" id="examples"> <span id="st-examples"></span><h2>32.1.6. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2> <p id="index-485">The parser modules allows operations to be performed on the parse tree of Python source code before the <a class="reference external" href="../glossary.html#term-bytecode"><em class="xref">bytecode</em></a> is generated, and provides for inspection of the parse tree for information gathering purposes. Two examples are presented. The simple example demonstrates emulation of the <a title="compile" class="reference external" href="functions.html#compile"><tt class="xref docutils literal"><span class="pre">compile()</span></tt></a> built-in function and the complex example shows the use of a parse tree for information discovery.</p> <div class="section" id="emulation-of-compile"> <h3>32.1.6.1. Emulation of <a title="compile" class="reference external" href="functions.html#compile"><tt class="xref docutils literal"><span class="pre">compile()</span></tt></a><a class="headerlink" href="#emulation-of-compile" title="Permalink to this headline">¶</a></h3> <p>While many useful operations may take place between parsing and bytecode generation, the simplest operation is to do nothing. For this purpose, using the <tt class="xref docutils literal"><span class="pre">parser</span></tt> module to produce an intermediate data structure is equivalent to the code</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">code</span> <span class="o">=</span> <span class="nb">compile</span><span class="p">(</span><span class="s">'a + 5'</span><span class="p">,</span> <span class="s">'file.py'</span><span class="p">,</span> <span class="s">'eval'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="mi">5</span> <span class="gp">>>> </span><span class="nb">eval</span><span class="p">(</span><span class="n">code</span><span class="p">)</span> <span class="go">10</span> </pre></div> </div> <p>The equivalent operation using the <tt class="xref docutils literal"><span class="pre">parser</span></tt> module is somewhat longer, and allows the intermediate internal parse tree to be retained as an ST object:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">parser</span> <span class="gp">>>> </span><span class="n">st</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">expr</span><span class="p">(</span><span class="s">'a + 5'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">code</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">'file.py'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="mi">5</span> <span class="gp">>>> </span><span class="nb">eval</span><span class="p">(</span><span class="n">code</span><span class="p">)</span> <span class="go">10</span> </pre></div> </div> <p>An application which needs both ST and code objects can package this code into readily available functions:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">parser</span> <span class="k">def</span> <span class="nf">load_suite</span><span class="p">(</span><span class="n">source_string</span><span class="p">):</span> <span class="n">st</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">suite</span><span class="p">(</span><span class="n">source_string</span><span class="p">)</span> <span class="k">return</span> <span class="n">st</span><span class="p">,</span> <span class="n">st</span><span class="o">.</span><span class="n">compile</span><span class="p">()</span> <span class="k">def</span> <span class="nf">load_expression</span><span class="p">(</span><span class="n">source_string</span><span class="p">):</span> <span class="n">st</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">expr</span><span class="p">(</span><span class="n">source_string</span><span class="p">)</span> <span class="k">return</span> <span class="n">st</span><span class="p">,</span> <span class="n">st</span><span class="o">.</span><span class="n">compile</span><span class="p">()</span> </pre></div> </div> </div> <div class="section" id="information-discovery"> <h3>32.1.6.2. Information Discovery<a class="headerlink" href="#information-discovery" title="Permalink to this headline">¶</a></h3> <p id="index-486">Some applications benefit from direct access to the parse tree. The remainder of this section demonstrates how the parse tree provides access to module documentation defined in docstrings without requiring that the code being examined be loaded into a running interpreter via <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a>. This can be very useful for performing analyses of untrusted code.</p> <p>Generally, the example will demonstrate how the parse tree may be traversed to distill interesting information. Two functions and a set of classes are developed which provide programmatic access to high level function and class definitions provided by a module. The classes extract information from the parse tree and provide access to the information at a useful semantic level, one function provides a simple low-level pattern matching capability, and the other function defines a high-level interface to the classes by handling file operations on behalf of the caller. All source files mentioned here which are not part of the Python installation are located in the <tt class="docutils literal"><span class="pre">Demo/parser/</span></tt> directory of the distribution.</p> <p>The dynamic nature of Python allows the programmer a great deal of flexibility, but most modules need only a limited measure of this when defining classes, functions, and methods. In this example, the only definitions that will be considered are those which are defined in the top level of their context, e.g., a function defined by a <a class="reference external" href="../reference/compound_stmts.html#def"><tt class="xref docutils literal"><span class="pre">def</span></tt></a> statement at column zero of a module, but not a function defined within a branch of an <a class="reference external" href="../reference/compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> ... <a class="reference external" href="../reference/compound_stmts.html#else"><tt class="xref docutils literal"><span class="pre">else</span></tt></a> construct, though there are some good reasons for doing so in some situations. Nesting of definitions will be handled by the code developed in the example.</p> <p>To construct the upper-level extraction methods, we need to know what the parse tree structure looks like and how much of it we actually need to be concerned about. Python uses a moderately deep parse tree so there are a large number of intermediate nodes. It is important to read and understand the formal grammar used by Python. This is specified in the file <tt class="docutils literal"><span class="pre">Grammar/Grammar</span></tt> in the distribution. Consider the simplest case of interest when searching for docstrings: a module consisting of a docstring and nothing else. (See file <tt class="docutils literal"><span class="pre">docstring.py</span></tt>.)</p> <div class="highlight-python"><div class="highlight"><pre><span class="sd">"""Some documentation.</span> <span class="sd">"""</span> </pre></div> </div> <p>Using the interpreter to take a look at the parse tree, we find a bewildering mass of numbers and parentheses, with the documentation buried deep in nested tuples.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">parser</span> <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">pprint</span> <span class="gp">>>> </span><span class="n">st</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">suite</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">'docstring.py'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span> <span class="gp">>>> </span><span class="n">tup</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">totuple</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">tup</span><span class="p">)</span> <span class="go">(257,</span> <span class="go"> (264,</span> <span class="go"> (265,</span> <span class="go"> (266,</span> <span class="go"> (267,</span> <span class="go"> (307,</span> <span class="go"> (287,</span> <span class="go"> (288,</span> <span class="go"> (289,</span> <span class="go"> (290,</span> <span class="go"> (292,</span> <span class="go"> (293,</span> <span class="go"> (294,</span> <span class="go"> (295,</span> <span class="go"> (296,</span> <span class="go"> (297,</span> <span class="go"> (298,</span> <span class="go"> (299,</span> <span class="go"> (300, (3, '"""Some documentation.\n"""'))))))))))))))))),</span> <span class="go"> (4, ''))),</span> <span class="go"> (4, ''),</span> <span class="go"> (0, ''))</span> </pre></div> </div> <p>The numbers at the first element of each node in the tree are the node types; they map directly to terminal and non-terminal symbols in the grammar. Unfortunately, they are represented as integers in the internal representation, and the Python structures generated do not change that. However, the <a title="Constants representing internal nodes of the parse tree." class="reference external" href="symbol.html#module-symbol"><tt class="xref docutils literal"><span class="pre">symbol</span></tt></a> and <a title="Constants representing terminal nodes of the parse tree." class="reference external" href="token.html#module-token"><tt class="xref docutils literal"><span class="pre">token</span></tt></a> modules provide symbolic names for the node types and dictionaries which map from the integers to the symbolic names for the node types.</p> <p>In the output presented above, the outermost tuple contains four elements: the integer <tt class="docutils literal"><span class="pre">257</span></tt> and three additional tuples. Node type <tt class="docutils literal"><span class="pre">257</span></tt> has the symbolic name <tt class="xref docutils literal"><span class="pre">file_input</span></tt>. Each of these inner tuples contains an integer as the first element; these integers, <tt class="docutils literal"><span class="pre">264</span></tt>, <tt class="docutils literal"><span class="pre">4</span></tt>, and <tt class="docutils literal"><span class="pre">0</span></tt>, represent the node types <tt class="xref docutils literal"><span class="pre">stmt</span></tt>, <tt class="xref docutils literal"><span class="pre">NEWLINE</span></tt>, and <tt class="xref docutils literal"><span class="pre">ENDMARKER</span></tt>, respectively. Note that these values may change depending on the version of Python you are using; consult <tt class="docutils literal"><span class="pre">symbol.py</span></tt> and <tt class="docutils literal"><span class="pre">token.py</span></tt> for details of the mapping. It should be fairly clear that the outermost node is related primarily to the input source rather than the contents of the file, and may be disregarded for the moment. The <tt class="xref docutils literal"><span class="pre">stmt</span></tt> node is much more interesting. In particular, all docstrings are found in subtrees which are formed exactly as this node is formed, with the only difference being the string itself. The association between the docstring in a similar tree and the defined entity (class, function, or module) which it describes is given by the position of the docstring subtree within the tree defining the described structure.</p> <p>By replacing the actual docstring with something to signify a variable component of the tree, we allow a simple pattern matching approach to check any given subtree for equivalence to the general pattern for docstrings. Since the example demonstrates information extraction, we can safely require that the tree be in tuple form rather than list form, allowing a simple variable representation to be <tt class="docutils literal"><span class="pre">['variable_name']</span></tt>. A simple recursive function can implement the pattern matching, returning a Boolean and a dictionary of variable name to value mappings. (See file <tt class="docutils literal"><span class="pre">example.py</span></tt>.)</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">types</span> <span class="kn">import</span> <span class="n">ListType</span><span class="p">,</span> <span class="n">TupleType</span> <span class="k">def</span> <span class="nf">match</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="nb">vars</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="k">if</span> <span class="nb">vars</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="nb">vars</span> <span class="o">=</span> <span class="p">{}</span> <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span> <span class="ow">is</span> <span class="n">ListType</span><span class="p">:</span> <span class="nb">vars</span><span class="p">[</span><span class="n">pattern</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">data</span> <span class="k">return</span> <span class="mi">1</span><span class="p">,</span> <span class="nb">vars</span> <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">TupleType</span><span class="p">:</span> <span class="k">return</span> <span class="p">(</span><span class="n">pattern</span> <span class="o">==</span> <span class="n">data</span><span class="p">),</span> <span class="nb">vars</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pattern</span><span class="p">):</span> <span class="k">return</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">vars</span> <span class="k">for</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">data</span> <span class="ow">in</span> <span class="nb">map</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span> <span class="n">same</span><span class="p">,</span> <span class="nb">vars</span> <span class="o">=</span> <span class="n">match</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="nb">vars</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">same</span><span class="p">:</span> <span class="k">break</span> <span class="k">return</span> <span class="n">same</span><span class="p">,</span> <span class="nb">vars</span> </pre></div> </div> <p>Using this simple representation for syntactic variables and the symbolic node types, the pattern for the candidate docstring subtrees becomes fairly readable. (See file <tt class="docutils literal"><span class="pre">example.py</span></tt>.)</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">symbol</span> <span class="kn">import</span> <span class="nn">token</span> <span class="n">DOCSTRING_STMT_PATTERN</span> <span class="o">=</span> <span class="p">(</span> <span class="n">symbol</span><span class="o">.</span><span class="n">stmt</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">simple_stmt</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">small_stmt</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">expr_stmt</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">testlist</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">test</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">and_test</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">not_test</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">comparison</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">expr</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">xor_expr</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">and_expr</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">shift_expr</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">arith_expr</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">term</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">factor</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">power</span><span class="p">,</span> <span class="p">(</span><span class="n">symbol</span><span class="o">.</span><span class="n">atom</span><span class="p">,</span> <span class="p">(</span><span class="n">token</span><span class="o">.</span><span class="n">STRING</span><span class="p">,</span> <span class="p">[</span><span class="s">'docstring'</span><span class="p">])</span> <span class="p">)))))))))))))))),</span> <span class="p">(</span><span class="n">token</span><span class="o">.</span><span class="n">NEWLINE</span><span class="p">,</span> <span class="s">''</span><span class="p">)</span> <span class="p">))</span> </pre></div> </div> <p>Using the <tt class="xref docutils literal"><span class="pre">match()</span></tt> function with this pattern, extracting the module docstring from the parse tree created previously is easy:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">found</span><span class="p">,</span> <span class="nb">vars</span> <span class="o">=</span> <span class="n">match</span><span class="p">(</span><span class="n">DOCSTRING_STMT_PATTERN</span><span class="p">,</span> <span class="n">tup</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">found</span> <span class="go">1</span> <span class="gp">>>> </span><span class="nb">vars</span> <span class="go">{'docstring': '"""Some documentation.\n"""'}</span> </pre></div> </div> <p>Once specific data can be extracted from a location where it is expected, the question of where information can be expected needs to be answered. When dealing with docstrings, the answer is fairly simple: the docstring is the first <tt class="xref docutils literal"><span class="pre">stmt</span></tt> node in a code block (<tt class="xref docutils literal"><span class="pre">file_input</span></tt> or <a title="parser.suite" class="reference internal" href="#parser.suite"><tt class="xref docutils literal"><span class="pre">suite</span></tt></a> node types). A module consists of a single <tt class="xref docutils literal"><span class="pre">file_input</span></tt> node, and class and function definitions each contain exactly one <a title="parser.suite" class="reference internal" href="#parser.suite"><tt class="xref docutils literal"><span class="pre">suite</span></tt></a> node. Classes and functions are readily identified as subtrees of code block nodes which start with <tt class="docutils literal"><span class="pre">(stmt,</span> <span class="pre">(compound_stmt,</span> <span class="pre">(classdef,</span> <span class="pre">...</span></tt> or <tt class="docutils literal"><span class="pre">(stmt,</span> <span class="pre">(compound_stmt,</span> <span class="pre">(funcdef,</span> <span class="pre">...</span></tt>. Note that these subtrees cannot be matched by <tt class="xref docutils literal"><span class="pre">match()</span></tt> since it does not support multiple sibling nodes to match without regard to number. A more elaborate matching function could be used to overcome this limitation, but this is sufficient for the example.</p> <p>Given the ability to determine whether a statement might be a docstring and extract the actual string from the statement, some work needs to be performed to walk the parse tree for an entire module and extract information about the names defined in each context of the module and associate any docstrings with the names. The code to perform this work is not complicated, but bears some explanation.</p> <p>The public interface to the classes is straightforward and should probably be somewhat more flexible. Each “major” block of the module is described by an object providing several methods for inquiry and a constructor which accepts at least the subtree of the complete parse tree which it represents. The <tt class="xref docutils literal"><span class="pre">ModuleInfo</span></tt> constructor accepts an optional <em>name</em> parameter since it cannot otherwise determine the name of the module.</p> <p>The public classes include <tt class="xref docutils literal"><span class="pre">ClassInfo</span></tt>, <tt class="xref docutils literal"><span class="pre">FunctionInfo</span></tt>, and <tt class="xref docutils literal"><span class="pre">ModuleInfo</span></tt>. All objects provide the methods <tt class="xref docutils literal"><span class="pre">get_name()</span></tt>, <tt class="xref docutils literal"><span class="pre">get_docstring()</span></tt>, <tt class="xref docutils literal"><span class="pre">get_class_names()</span></tt>, and <tt class="xref docutils literal"><span class="pre">get_class_info()</span></tt>. The <tt class="xref docutils literal"><span class="pre">ClassInfo</span></tt> objects support <tt class="xref docutils literal"><span class="pre">get_method_names()</span></tt> and <tt class="xref docutils literal"><span class="pre">get_method_info()</span></tt> while the other classes provide <tt class="xref docutils literal"><span class="pre">get_function_names()</span></tt> and <tt class="xref docutils literal"><span class="pre">get_function_info()</span></tt>.</p> <p>Within each of the forms of code block that the public classes represent, most of the required information is in the same form and is accessed in the same way, with classes having the distinction that functions defined at the top level are referred to as “methods.” Since the difference in nomenclature reflects a real semantic distinction from functions defined outside of a class, the implementation needs to maintain the distinction. Hence, most of the functionality of the public classes can be implemented in a common base class, <tt class="xref docutils literal"><span class="pre">SuiteInfoBase</span></tt>, with the accessors for function and method information provided elsewhere. Note that there is only one class which represents function and method information; this parallels the use of the <a class="reference external" href="../reference/compound_stmts.html#def"><tt class="xref docutils literal"><span class="pre">def</span></tt></a> statement to define both types of elements.</p> <p>Most of the accessor functions are declared in <tt class="xref docutils literal"><span class="pre">SuiteInfoBase</span></tt> and do not need to be overridden by subclasses. More importantly, the extraction of most information from a parse tree is handled through a method called by the <tt class="xref docutils literal"><span class="pre">SuiteInfoBase</span></tt> constructor. The example code for most of the classes is clear when read alongside the formal grammar, but the method which recursively creates new information objects requires further examination. Here is the relevant part of the <tt class="xref docutils literal"><span class="pre">SuiteInfoBase</span></tt> definition from <tt class="docutils literal"><span class="pre">example.py</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">SuiteInfoBase</span><span class="p">:</span> <span class="n">_docstring</span> <span class="o">=</span> <span class="s">''</span> <span class="n">_name</span> <span class="o">=</span> <span class="s">''</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span> <span class="o">=</span> <span class="bp">None</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">_class_info</span> <span class="o">=</span> <span class="p">{}</span> <span class="bp">self</span><span class="o">.</span><span class="n">_function_info</span> <span class="o">=</span> <span class="p">{}</span> <span class="k">if</span> <span class="n">tree</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extract_info</span><span class="p">(</span><span class="n">tree</span><span class="p">)</span> <span class="k">def</span> <span class="nf">_extract_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span> <span class="c"># extract docstring</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tree</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span> <span class="n">found</span><span class="p">,</span> <span class="nb">vars</span> <span class="o">=</span> <span class="n">match</span><span class="p">(</span><span class="n">DOCSTRING_STMT_PATTERN</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">tree</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="k">else</span><span class="p">:</span> <span class="n">found</span><span class="p">,</span> <span class="nb">vars</span> <span class="o">=</span> <span class="n">match</span><span class="p">(</span><span class="n">DOCSTRING_STMT_PATTERN</span><span class="p">,</span> <span class="n">tree</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span> <span class="k">if</span> <span class="n">found</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_docstring</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="nb">vars</span><span class="p">[</span><span class="s">'docstring'</span><span class="p">])</span> <span class="c"># discover inner definitions</span> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">tree</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span> <span class="n">found</span><span class="p">,</span> <span class="nb">vars</span> <span class="o">=</span> <span class="n">match</span><span class="p">(</span><span class="n">COMPOUND_STMT_PATTERN</span><span class="p">,</span> <span class="n">node</span><span class="p">)</span> <span class="k">if</span> <span class="n">found</span><span class="p">:</span> <span class="n">cstmt</span> <span class="o">=</span> <span class="nb">vars</span><span class="p">[</span><span class="s">'compound'</span><span class="p">]</span> <span class="k">if</span> <span class="n">cstmt</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">symbol</span><span class="o">.</span><span class="n">funcdef</span><span class="p">:</span> <span class="n">name</span> <span class="o">=</span> <span class="n">cstmt</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="bp">self</span><span class="o">.</span><span class="n">_function_info</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">FunctionInfo</span><span class="p">(</span><span class="n">cstmt</span><span class="p">)</span> <span class="k">elif</span> <span class="n">cstmt</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">symbol</span><span class="o">.</span><span class="n">classdef</span><span class="p">:</span> <span class="n">name</span> <span class="o">=</span> <span class="n">cstmt</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="bp">self</span><span class="o">.</span><span class="n">_class_info</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">ClassInfo</span><span class="p">(</span><span class="n">cstmt</span><span class="p">)</span> </pre></div> </div> <p>After initializing some internal state, the constructor calls the <tt class="xref docutils literal"><span class="pre">_extract_info()</span></tt> method. This method performs the bulk of the information extraction which takes place in the entire example. The extraction has two distinct phases: the location of the docstring for the parse tree passed in, and the discovery of additional definitions within the code block represented by the parse tree.</p> <p>The initial <a class="reference external" href="../reference/compound_stmts.html#if"><tt class="xref docutils literal"><span class="pre">if</span></tt></a> test determines whether the nested suite is of the “short form” or the “long form.” The short form is used when the code block is on the same line as the definition of the code block, as in</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">square</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="s">"Square an argument."</span><span class="p">;</span> <span class="k">return</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span> </pre></div> </div> <p>while the long form uses an indented block and allows nested definitions:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">make_power</span><span class="p">(</span><span class="n">exp</span><span class="p">):</span> <span class="s">"Make a function that raises an argument to the exponent `exp`."</span> <span class="k">def</span> <span class="nf">raiser</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">exp</span><span class="p">):</span> <span class="k">return</span> <span class="n">x</span> <span class="o">**</span> <span class="n">y</span> <span class="k">return</span> <span class="n">raiser</span> </pre></div> </div> <p>When the short form is used, the code block may contain a docstring as the first, and possibly only, <tt class="xref docutils literal"><span class="pre">small_stmt</span></tt> element. The extraction of such a docstring is slightly different and requires only a portion of the complete pattern used in the more common case. As implemented, the docstring will only be found if there is only one <tt class="xref docutils literal"><span class="pre">small_stmt</span></tt> node in the <tt class="xref docutils literal"><span class="pre">simple_stmt</span></tt> node. Since most functions and methods which use the short form do not provide a docstring, this may be considered sufficient. The extraction of the docstring proceeds using the <tt class="xref docutils literal"><span class="pre">match()</span></tt> function as described above, and the value of the docstring is stored as an attribute of the <tt class="xref docutils literal"><span class="pre">SuiteInfoBase</span></tt> object.</p> <p>After docstring extraction, a simple definition discovery algorithm operates on the <tt class="xref docutils literal"><span class="pre">stmt</span></tt> nodes of the <a title="parser.suite" class="reference internal" href="#parser.suite"><tt class="xref docutils literal"><span class="pre">suite</span></tt></a> node. The special case of the short form is not tested; since there are no <tt class="xref docutils literal"><span class="pre">stmt</span></tt> nodes in the short form, the algorithm will silently skip the single <tt class="xref docutils literal"><span class="pre">simple_stmt</span></tt> node and correctly not discover any nested definitions.</p> <p>Each statement in the code block is categorized as a class definition, function or method definition, or something else. For the definition statements, the name of the element defined is extracted and a representation object appropriate to the definition is created with the defining subtree passed as an argument to the constructor. The representation objects are stored in instance variables and may be retrieved by name using the appropriate accessor methods.</p> <p>The public classes provide any accessors required which are more specific than those provided by the <tt class="xref docutils literal"><span class="pre">SuiteInfoBase</span></tt> class, but the real extraction algorithm remains common to all forms of code blocks. A high-level function can be used to extract the complete set of information from a source file. (See file <tt class="docutils literal"><span class="pre">example.py</span></tt>.)</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">get_docs</span><span class="p">(</span><span class="n">fileName</span><span class="p">):</span> <span class="kn">import</span> <span class="nn">os</span> <span class="kn">import</span> <span class="nn">parser</span> <span class="n">source</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">fileName</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="n">basename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">fileName</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="n">st</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">suite</span><span class="p">(</span><span class="n">source</span><span class="p">)</span> <span class="k">return</span> <span class="n">ModuleInfo</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">totuple</span><span class="p">(),</span> <span class="n">basename</span><span class="p">)</span> </pre></div> </div> <p>This provides an easy-to-use interface to the documentation of a module. If information is required which is not extracted by the code of this example, the code may be extended at clearly defined points to provide additional capabilities.</p> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference external" href="#">32.1. <tt class="docutils literal"><span class="pre">parser</span></tt> — Access Python parse trees</a><ul> <li><a class="reference external" href="#creating-st-objects">32.1.1. Creating ST Objects</a></li> <li><a class="reference external" href="#converting-st-objects">32.1.2. Converting ST Objects</a></li> <li><a class="reference external" href="#queries-on-st-objects">32.1.3. Queries on ST Objects</a></li> <li><a class="reference external" href="#exceptions-and-error-handling">32.1.4. Exceptions and Error Handling</a></li> <li><a class="reference external" href="#st-objects">32.1.5. ST Objects</a></li> <li><a class="reference external" href="#examples">32.1.6. Examples</a><ul> <li><a class="reference external" href="#emulation-of-compile">32.1.6.1. Emulation of <tt class="docutils literal"><span class="pre">compile()</span></tt></a></li> <li><a class="reference external" href="#information-discovery">32.1.6.2. Information Discovery</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="language.html" title="previous chapter">32. Python Language Services</a></p> <h4>Next topic</h4> <p class="topless"><a href="ast.html" title="next chapter">32.2. Abstract Syntax Trees</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/parser.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../modindex.html" title="Global Module Index" >modules</a> |</li> <li class="right" > <a href="ast.html" title="32.2. Abstract Syntax Trees" >next</a> |</li> <li class="right" > <a href="language.html" title="32. Python Language Services" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v2.6.5 documentation</a> »</li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="language.html" >32. Python Language Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Mar 19, 2010. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5. </div> </body> </html>