Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Static Private Attributes
doctest.DocTestParser Class Reference

List of all members.

Public Member Functions

def parse
def get_doctest
def get_examples

Private Member Functions

def _parse_example

Static Private Attributes

tuple _EXAMPLE_RE
tuple _EXCEPTION_RE = re.compile(r""" # Grab the traceback header. Different versions of Python have # said different things on the first traceback line. ^(?P<hdr> Traceback\ \( (?: most\ recent\ call\ last | innermost\ last ) \) : ) \s* $ # toss trailing whitespace on the header. (?P<stack> .*?) # don't blink: absorb stuff until... ^ (?P<msg> \w+ .*) # a line *starts* with alphanum. """, re.VERBOSE | re.MULTILINE | re.DOTALL)
tuple _IS_BLANK_OR_COMMENT = re.compile(r'^[ ]*(#.*)?$')

Detailed Description

  1. DocTestParser
    A class used to parse strings containing doctest examples.
    

Definition at line 504 of file doctest.py.


Member Function Documentation

def doctest.DocTestParser._parse_example (   self,
  m,
  name,
  lineno 
) [private]
Given a regular expression match from `_EXAMPLE_RE` (`m`),
return a pair `(source, want)`, where `source` is the matched
example's source code (with prompts and indentation stripped);
and `want` is the example's expected output (with indentation
stripped).

`name` is the string's name, and `lineno` is the line number
where the example starts; both are used for error messages.

Definition at line 617 of file doctest.py.

00617 
00618     def _parse_example(self, m, name, lineno):
00619         """
00620         Given a regular expression match from `_EXAMPLE_RE` (`m`),
00621         return a pair `(source, want)`, where `source` is the matched
00622         example's source code (with prompts and indentation stripped);
00623         and `want` is the example's expected output (with indentation
00624         stripped).
00625 
00626         `name` is the string's name, and `lineno` is the line number
00627         where the example starts; both are used for error messages.
00628         """
00629         # Get the example's indentation level.
00630         indent = len(m.group('indent'))
00631 
00632         # Divide source into lines; check that they're properly
00633         # indented; and then strip their indentation & prompts.
00634         source_lines = m.group('source').split('\n')
00635         self._check_prompt_blank(source_lines, indent, name, lineno)
00636         self._check_prefix(source_lines[1:], ' '*indent + '.', name, lineno)
00637         source = '\n'.join([sl[indent+4:] for sl in source_lines])
00638 
00639         # Divide want into lines; check that it's properly indented; and
00640         # then strip the indentation.  Spaces before the last newline should
00641         # be preserved, so plain rstrip() isn't good enough.
00642         want = m.group('want')
00643         want_lines = want.split('\n')
00644         if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
00645             del want_lines[-1]  # forget final newline & spaces after it
00646         self._check_prefix(want_lines, ' '*indent, name,
00647                            lineno + len(source_lines))
00648         want = '\n'.join([wl[indent:] for wl in want_lines])
00649 
00650         # If `want` contains a traceback message, then extract it.
00651         m = self._EXCEPTION_RE.match(want)
00652         if m:
00653             exc_msg = m.group('msg')
00654         else:
00655             exc_msg = None
00656 
00657         # Extract options from the source.
00658         options = self._find_options(source, name, lineno)
00659 
00660         return source, options, want, exc_msg

Here is the call graph for this function:

Here is the caller graph for this function:

def doctest.DocTestParser.get_doctest (   self,
  string,
  globs,
  name,
  filename,
  lineno 
)
Extract all doctest examples from the given string, and
collect them into a `DocTest` object.

`globs`, `name`, `filename`, and `lineno` are attributes for
the new `DocTest` object.  See the documentation for `DocTest`
for more information.

Definition at line 591 of file doctest.py.

00591 
00592     def get_doctest(self, string, globs, name, filename, lineno):
00593         """
00594         Extract all doctest examples from the given string, and
00595         collect them into a `DocTest` object.
00596 
00597         `globs`, `name`, `filename`, and `lineno` are attributes for
00598         the new `DocTest` object.  See the documentation for `DocTest`
00599         for more information.
00600         """
00601         return DocTest(self.get_examples(string, name), globs,
00602                        name, filename, lineno, string)

Here is the call graph for this function:

def doctest.DocTestParser.get_examples (   self,
  string,
  name = '<string>' 
)
Extract all doctest examples from the given string, and return
them as a list of `Example` objects.  Line numbers are
0-based, because it's most common in doctests that nothing
interesting appears on the same line as opening triple-quote,
and so the first interesting line is called \"line 1\" then.

The optional argument `name` is a name identifying this
string, and is only used for error messages.

Definition at line 603 of file doctest.py.

00603 
00604     def get_examples(self, string, name='<string>'):
00605         """
00606         Extract all doctest examples from the given string, and return
00607         them as a list of `Example` objects.  Line numbers are
00608         0-based, because it's most common in doctests that nothing
00609         interesting appears on the same line as opening triple-quote,
00610         and so the first interesting line is called \"line 1\" then.
00611 
00612         The optional argument `name` is a name identifying this
00613         string, and is only used for error messages.
00614         """
00615         return [x for x in self.parse(string, name)
00616                 if isinstance(x, Example)]

Here is the call graph for this function:

Here is the caller graph for this function:

def doctest.DocTestParser.parse (   self,
  string,
  name = '<string>' 
)
Divide the given string into examples and intervening text,
and return them as a list of alternating Examples and strings.
Line numbers for the Examples are 0-based.  The optional
argument `name` is a name identifying this string, and is only
used for error messages.

Definition at line 552 of file doctest.py.

00552 
00553     def parse(self, string, name='<string>'):
00554         """
00555         Divide the given string into examples and intervening text,
00556         and return them as a list of alternating Examples and strings.
00557         Line numbers for the Examples are 0-based.  The optional
00558         argument `name` is a name identifying this string, and is only
00559         used for error messages.
00560         """
00561         string = string.expandtabs()
00562         # If all lines begin with the same indentation, then strip it.
00563         min_indent = self._min_indent(string)
00564         if min_indent > 0:
00565             string = '\n'.join([l[min_indent:] for l in string.split('\n')])
00566 
00567         output = []
00568         charno, lineno = 0, 0
00569         # Find all doctest examples in the string:
00570         for m in self._EXAMPLE_RE.finditer(string):
00571             # Add the pre-example text to `output`.
00572             output.append(string[charno:m.start()])
00573             # Update lineno (lines before this example)
00574             lineno += string.count('\n', charno, m.start())
00575             # Extract info from the regexp match.
00576             (source, options, want, exc_msg) = \
00577                      self._parse_example(m, name, lineno)
00578             # Create an Example, and add it to the list.
00579             if not self._IS_BLANK_OR_COMMENT(source):
00580                 output.append( Example(source, want, exc_msg,
00581                                     lineno=lineno,
00582                                     indent=min_indent+len(m.group('indent')),
00583                                     options=options) )
00584             # Update lineno (lines inside this example)
00585             lineno += string.count('\n', m.start(), m.end())
00586             # Update charno.
00587             charno = m.end()
00588         # Add any remaining post-example text to `output`.
00589         output.append(string[charno:])
00590         return output

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

tuple doctest.DocTestParser._EXAMPLE_RE [static, private]
Initial value:
re.compile(r'''
    # Source consists of a PS1 line followed by zero or more PS2 lines.
    (?P<source>
        (?:^(?P<indent> [ ]*) >>>    .*)    # PS1 line
        (?:\n           [ ]*  \.\.\. .*)*)  # PS2 lines
    \n?
    # Want consists of any non-blank lines that do not start with PS1.
    (?P<want> (?:(?![ ]*$)    # Not a blank line
                 (?![ ]*>>>)  # Not a line starting with PS1
                 .*$\n?       # But any other line
              )*)
    ''', re.MULTILINE | re.VERBOSE)

Definition at line 513 of file doctest.py.

tuple doctest.DocTestParser._EXCEPTION_RE = re.compile(r""" # Grab the traceback header. Different versions of Python have # said different things on the first traceback line. ^(?P<hdr> Traceback\ \( (?: most\ recent\ call\ last | innermost\ last ) \) : ) \s* $ # toss trailing whitespace on the header. (?P<stack> .*?) # don't blink: absorb stuff until... ^ (?P<msg> \w+ .*) # a line *starts* with alphanum. """, re.VERBOSE | re.MULTILINE | re.DOTALL) [static, private]

Definition at line 535 of file doctest.py.

tuple doctest.DocTestParser._IS_BLANK_OR_COMMENT = re.compile(r'^[ ]*(#.*)?$') [static, private]

Definition at line 550 of file doctest.py.


The documentation for this class was generated from the following file: