For %load, strip indent from lines until finding an unindented line. https://github.com/ipython/ipython/issues/9775
(lines)
| 134 | return blocks, not_found |
| 135 | |
| 136 | def strip_initial_indent(lines): |
| 137 | """For %load, strip indent from lines until finding an unindented line. |
| 138 | |
| 139 | https://github.com/ipython/ipython/issues/9775 |
| 140 | """ |
| 141 | indent_re = re.compile(r'\s+') |
| 142 | |
| 143 | it = iter(lines) |
| 144 | first_line = next(it) |
| 145 | indent_match = indent_re.match(first_line) |
| 146 | |
| 147 | if indent_match: |
| 148 | # First line was indented |
| 149 | indent = indent_match.group() |
| 150 | yield first_line[len(indent):] |
| 151 | |
| 152 | for line in it: |
| 153 | if line.startswith(indent): |
| 154 | yield line[len(indent):] |
| 155 | else: |
| 156 | # Less indented than the first line - stop dedenting |
| 157 | yield line |
| 158 | break |
| 159 | else: |
| 160 | yield first_line |
| 161 | |
| 162 | # Pass the remaining lines through without dedenting |
| 163 | for line in it: |
| 164 | yield line |
| 165 | |
| 166 | |
| 167 | class InteractivelyDefined(Exception): |