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 m
(self, string, name='<string>')
| 133 | return block |
| 134 | |
| 135 | def parse(self, string, name='<string>'): |
| 136 | """ |
| 137 | Divide the given string into examples and intervening text, |
| 138 | and return them as a list of alternating Examples and strings. |
| 139 | Line numbers for the Examples are 0-based. The optional |
| 140 | argument `name` is a name identifying this string, and is only |
| 141 | used for error messages. |
| 142 | """ |
| 143 | |
| 144 | # print('Parse string:\n',string) # dbg |
| 145 | |
| 146 | string = string.expandtabs() |
| 147 | # If all lines begin with the same indentation, then strip it. |
| 148 | min_indent = self._min_indent(string) |
| 149 | if min_indent > 0: |
| 150 | string = '\n'.join([l[min_indent:] for l in string.split('\n')]) |
| 151 | |
| 152 | output = [] |
| 153 | charno, lineno = 0, 0 |
| 154 | |
| 155 | # We make 'all random' tests by adding the '# random' mark to every |
| 156 | # block of output in the test. |
| 157 | if self._RANDOM_TEST.search(string): |
| 158 | random_marker = '\n# random' |
| 159 | else: |
| 160 | random_marker = '' |
| 161 | |
| 162 | # Whether to convert the input from ipython to python syntax |
| 163 | ip2py = False |
| 164 | # Find all doctest examples in the string. First, try them as Python |
| 165 | # examples, then as IPython ones |
| 166 | terms = list(self._EXAMPLE_RE_PY.finditer(string)) |
| 167 | if terms: |
| 168 | # Normal Python example |
| 169 | Example = doctest.Example |
| 170 | else: |
| 171 | # It's an ipython example. |
| 172 | terms = list(self._EXAMPLE_RE_IP.finditer(string)) |
| 173 | Example = IPExample |
| 174 | ip2py = True |
| 175 | |
| 176 | for m in terms: |
| 177 | # Add the pre-example text to `output`. |
| 178 | output.append(string[charno:m.start()]) |
| 179 | # Update lineno (lines before this example) |
| 180 | lineno += string.count('\n', charno, m.start()) |
| 181 | # Extract info from the regexp match. |
| 182 | (source, options, want, exc_msg) = \ |
| 183 | self._parse_example(m, name, lineno,ip2py) |
| 184 | |
| 185 | # Append the random-output marker (it defaults to empty in most |
| 186 | # cases, it's only non-empty for 'all-random' tests): |
| 187 | want += random_marker |
| 188 | |
| 189 | # Create an Example, and add it to the list. |
| 190 | if not self._IS_BLANK_OR_COMMENT(source): |
| 191 | output.append(Example(source, want, exc_msg, |
| 192 | lineno=lineno, |
no test coverage detected